View Javadoc
1   package org.apache.maven.plugins.enforcer.utils;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * @author Brian Fox
34   *
35   */
36  public class DependencyVersionMap
37      implements DependencyNodeVisitor
38  {
39      private boolean uniqueVersions;
40  
41      private Map<String, List<DependencyNode>> idsToNode;
42  
43      public DependencyVersionMap( Log log )
44      {
45          idsToNode = new HashMap<String, List<DependencyNode>>();
46      }
47      
48      public void setUniqueVersions( boolean uniqueVersions )
49      {
50          this.uniqueVersions = uniqueVersions;
51      }
52  
53      @Override
54      public boolean visit( DependencyNode node )
55      {
56          addDependency( node );
57          return !containsConflicts( node );
58      }
59  
60      @Override
61      public boolean endVisit( DependencyNode node )
62      {
63          return true;
64      }
65  
66      private String constructKey( DependencyNode node )
67      {
68          return constructKey( node.getArtifact() );
69      }
70  
71      private String constructKey( Artifact artifact )
72      {
73          return artifact.getGroupId() + ":" + artifact.getArtifactId();
74      }
75  
76      public void addDependency( DependencyNode node )
77      {
78          String key = constructKey( node );
79          List<DependencyNode> nodes = idsToNode.get( key );
80          if ( nodes == null )
81          {
82              nodes = new ArrayList<DependencyNode>();
83              idsToNode.put( key, nodes );
84          }
85          nodes.add( node );
86      }
87      
88      private String getVersion( Artifact artifact )
89      {
90          return uniqueVersions ? artifact.getVersion() : artifact.getBaseVersion();
91      }
92  
93      private boolean containsConflicts( DependencyNode node )
94      {
95          return containsConflicts( node.getArtifact() );
96      }
97  
98      private boolean containsConflicts( Artifact artifact )
99      {
100         return containsConflicts( idsToNode.get( constructKey( artifact ) ) );
101     }
102 
103     private boolean containsConflicts( List<DependencyNode> nodes )
104     {
105         String version = null;
106         for ( DependencyNode node : nodes )
107         {
108             if ( version == null )
109             {
110                 version = getVersion( node.getArtifact() );
111             }
112             else
113             {
114                 if ( version.compareTo( getVersion( node.getArtifact() ) ) != 0 )
115                 {
116                     return true;
117                 }
118             }
119         }
120         return false;
121     }
122 
123     public List<List<DependencyNode>> getConflictedVersionNumbers()
124     {
125         List<List<DependencyNode>> output = new ArrayList<List<DependencyNode>>();
126         for ( List<DependencyNode> nodes : idsToNode.values() )
127         {
128             if ( containsConflicts( nodes ) )
129             {
130                 output.add( nodes );
131             }
132         }
133         return output;
134     }
135 }