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