| 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 | |
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.artifact.ArtifactUtils; |
| 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 | |
public class DependencyVersionMap |
| 33 | |
implements DependencyNodeVisitor |
| 34 | |
{ |
| 35 | |
private Log log; |
| 36 | |
|
| 37 | |
private boolean uniqueVersions; |
| 38 | |
|
| 39 | |
private Map<String, List<DependencyNode>> idsToNode; |
| 40 | |
|
| 41 | |
public DependencyVersionMap( Log log ) |
| 42 | 0 | { |
| 43 | 0 | this.log = log; |
| 44 | 0 | idsToNode = new HashMap<String, List<DependencyNode>>(); |
| 45 | 0 | } |
| 46 | |
|
| 47 | |
public void setUniqueVersions( boolean uniqueVersions ) |
| 48 | |
{ |
| 49 | 0 | this.uniqueVersions = uniqueVersions; |
| 50 | 0 | } |
| 51 | |
|
| 52 | |
public boolean visit( DependencyNode node ) |
| 53 | |
{ |
| 54 | 0 | addDependency( node ); |
| 55 | 0 | return !containsConflicts( node ); |
| 56 | |
} |
| 57 | |
|
| 58 | |
public boolean endVisit( DependencyNode node ) |
| 59 | |
{ |
| 60 | 0 | return true; |
| 61 | |
} |
| 62 | |
|
| 63 | |
private String constructKey( DependencyNode node ) |
| 64 | |
{ |
| 65 | 0 | return constructKey( node.getArtifact() ); |
| 66 | |
} |
| 67 | |
|
| 68 | |
private String constructKey( Artifact artifact ) |
| 69 | |
{ |
| 70 | 0 | return artifact.getGroupId() + ":" + artifact.getArtifactId(); |
| 71 | |
} |
| 72 | |
|
| 73 | |
public void addDependency( DependencyNode node ) |
| 74 | |
{ |
| 75 | 0 | String key = constructKey( node ); |
| 76 | 0 | List<DependencyNode> nodes = idsToNode.get( key ); |
| 77 | 0 | if ( nodes == null ) |
| 78 | |
{ |
| 79 | 0 | nodes = new ArrayList<DependencyNode>(); |
| 80 | 0 | idsToNode.put( key, nodes ); |
| 81 | |
} |
| 82 | 0 | nodes.add( node ); |
| 83 | 0 | } |
| 84 | |
|
| 85 | |
private String getVersion( Artifact artifact ) |
| 86 | |
{ |
| 87 | 0 | return uniqueVersions ? artifact.getVersion() : artifact.getBaseVersion(); |
| 88 | |
} |
| 89 | |
|
| 90 | |
private boolean containsConflicts( DependencyNode node ) |
| 91 | |
{ |
| 92 | 0 | return containsConflicts( node.getArtifact() ); |
| 93 | |
} |
| 94 | |
|
| 95 | |
private boolean containsConflicts( Artifact artifact ) |
| 96 | |
{ |
| 97 | 0 | return containsConflicts( idsToNode.get( constructKey( artifact ) ) ); |
| 98 | |
} |
| 99 | |
|
| 100 | |
private boolean containsConflicts( List<DependencyNode> nodes ) |
| 101 | |
{ |
| 102 | 0 | String version = null; |
| 103 | 0 | for ( DependencyNode node : nodes ) |
| 104 | |
{ |
| 105 | 0 | if ( version == null ) |
| 106 | |
{ |
| 107 | 0 | version = getVersion( node.getArtifact() ); |
| 108 | |
} |
| 109 | |
else |
| 110 | |
{ |
| 111 | 0 | if ( version.compareTo( getVersion( node.getArtifact() ) ) != 0 ) |
| 112 | |
{ |
| 113 | 0 | return true; |
| 114 | |
} |
| 115 | |
} |
| 116 | |
} |
| 117 | 0 | return false; |
| 118 | |
} |
| 119 | |
|
| 120 | |
public List<List<DependencyNode>> getConflictedVersionNumbers() |
| 121 | |
{ |
| 122 | 0 | List<List<DependencyNode>> output = new ArrayList<List<DependencyNode>>(); |
| 123 | 0 | for ( List<DependencyNode> nodes : idsToNode.values() ) |
| 124 | |
{ |
| 125 | 0 | if ( containsConflicts( nodes ) ) |
| 126 | |
{ |
| 127 | 0 | output.add( nodes ); |
| 128 | |
} |
| 129 | |
} |
| 130 | 0 | return output; |
| 131 | |
} |
| 132 | |
} |