1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.jadira.dependencynavigator.controller;
19
20 import org.apache.maven.model.Dependency;
21 import org.apache.maven.model.Exclusion;
22 import org.apache.maven.model.Model;
23 import org.jadira.dependencynavigator.assembler.Assembler;
24 import org.jadira.dependencynavigator.assembler.AssemblerException;
25 import org.jadira.dependencynavigator.controller.model.DuplicatesListManager;
26 import org.jadira.dependencynavigator.implementations.IProgressMeter;
27 import org.jadira.dependencynavigator.implementations.Repository;
28 import org.jadira.dependencynavigator.model.Artifact;
29 import org.jadira.dependencynavigator.model.ArtifactInitialisationException;
30 import org.jadira.dependencynavigator.model.LeafDependency;
31 import org.jadira.dependencynavigator.model.PomFile;
32
33 import java.util.Stack;
34
35 public class ArtifactResolver {
36
37 public static final String ROLE = "org.jadira.dependencynavigator.controller.ArtifactResolver";
38
39 private Controller controller;
40 private Assembler assembler;
41 private DuplicatesListManager duplicatesListManager;
42 private Repository repository;
43
44
45
46
47
48
49
50
51
52
53 public Artifact resolveArtifact(PomFile dependencyHeirarchyParent, Dependency dependency, IProgressMeter progress, Stack<LeafDependency> path) throws DependencyResolutionException {
54 String groupId = dependency.getGroupId() == null ? dependencyHeirarchyParent.getGroupId() : dependency.getGroupId();
55 String artifactId = dependency.getArtifactId();
56 String version = dependency.getVersion() == null ? dependencyHeirarchyParent.getGroupId() : dependency.getGroupId();
57
58
59 String scope = dependency.getScope();
60 if (isNullOrEmpty(scope)) {
61 scope = Artifact.DEFAULT_SCOPE;
62 }
63 if (!controller.includeScope(scope)) {
64 return null;
65 }
66 if (controller.getResolveExclusions().getValue()) {
67 if (dependencyHeirarchyParent.isExcluded(groupId, artifactId, new Stack<Artifact>())) {
68 return null;
69 }
70 }
71
72
73 try {
74 Model model = repository.resolveFile(groupId, artifactId, version);
75 if (model == null) {
76
77
78 LeafDependency leafDependency = new LeafDependency(dependencyHeirarchyParent, dependency, scope);
79 duplicatesListManager.add(leafDependency);
80 return leafDependency;
81 }
82
83 PomFile pomFile = new PomFile(assembler.assemble(model, repository), dependencyHeirarchyParent, scope);
84 addPomToDuplicatesList(pomFile);
85 if (controller.getResolveExclusions().getValue()) {
86 addExclusions(pomFile, dependency);
87 }
88 pomFile.resolveDependencies(this, progress, path);
89 return pomFile;
90 } catch (ArtifactInitialisationException e) {
91 throw new DependencyResolutionException(e);
92 } catch (AssemblerException e) {
93 throw new DependencyResolutionException(e);
94 }
95 }
96
97 public void addPomToDuplicatesList(PomFile pomFile) {
98 duplicatesListManager.add(pomFile);
99 }
100
101 private void addExclusions(Artifact artifact, Dependency dependency) {
102 for (Object o : dependency.getExclusions()) {
103 Exclusion exclusion = (Exclusion) o;
104 artifact.addExclusion(exclusion.getGroupId(), exclusion.getArtifactId());
105 }
106 }
107
108 private boolean isNullOrEmpty(String value) {
109 return value == null || value.trim().length() == 0;
110 }
111 }