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