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 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 }