1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.report.projectinfo.dependencies;
20
21 import java.util.ArrayList;
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Map;
25
26 import org.apache.maven.artifact.Artifact;
27 import org.apache.maven.shared.dependency.graph.DependencyNode;
28 import org.apache.maven.shared.dependency.graph.traversal.DependencyNodeVisitor;
29
30
31
32
33
34 public class DependencyVersionMap implements DependencyNodeVisitor {
35 private boolean uniqueVersions;
36
37 private Map<String, List<DependencyNode>> idsToNode;
38
39
40
41
42
43
44
45
46 public DependencyVersionMap() {
47 idsToNode = new HashMap<>();
48 }
49
50
51
52
53 public void setUniqueVersions(boolean uniqueVersions) {
54 this.uniqueVersions = uniqueVersions;
55 }
56
57
58
59
60 public boolean visit(DependencyNode node) {
61 addDependency(node);
62 return !containsConflicts(node);
63 }
64
65
66
67
68 public boolean endVisit(DependencyNode node) {
69 return true;
70 }
71
72
73
74
75
76
77 public List<List<DependencyNode>> getConflictedVersionNumbers() {
78 List<List<DependencyNode>> output = new ArrayList<>();
79 for (List<DependencyNode> nodes : idsToNode.values()) {
80 if (containsConflicts(nodes)) {
81 output.add(nodes);
82 }
83 }
84 return output;
85 }
86
87
88
89
90
91 private void addDependency(DependencyNode node) {
92 String key = constructKey(node);
93 List<DependencyNode> nodes = idsToNode.get(key);
94 if (nodes == null) {
95 nodes = new ArrayList<>();
96 idsToNode.put(key, nodes);
97 }
98 nodes.add(node);
99 }
100
101 private String constructKey(DependencyNode node) {
102 return constructKey(node.getArtifact());
103 }
104
105 private String constructKey(Artifact artifact) {
106 return artifact.getGroupId() + ":" + artifact.getArtifactId();
107 }
108
109 private String getVersion(Artifact artifact) {
110 return uniqueVersions ? artifact.getVersion() : artifact.getBaseVersion();
111 }
112
113 private boolean containsConflicts(DependencyNode node) {
114 return containsConflicts(node.getArtifact());
115 }
116
117 private boolean containsConflicts(Artifact artifact) {
118 return containsConflicts(idsToNode.get(constructKey(artifact)));
119 }
120
121
122
123
124
125
126
127 private boolean containsConflicts(List<DependencyNode> nodes) {
128 String version = null;
129 for (DependencyNode node : nodes) {
130 if (version == null) {
131 version = getVersion(node.getArtifact());
132 } else {
133 if (version.compareTo(getVersion(node.getArtifact())) != 0) {
134 return true;
135 }
136 }
137 }
138 return false;
139 }
140 }