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          return uniqueVersions ? artifact.getVersion() : artifact.getBaseVersion();
88      }
89  
90      private boolean containsConflicts( DependencyNode node )
91      {
92          return containsConflicts( node.getArtifact() );
93      }
94  
95      private boolean containsConflicts( Artifact artifact )
96      {
97          return containsConflicts( idsToNode.get( constructKey( artifact ) ) );
98      }
99  
100     private boolean containsConflicts( List<DependencyNode> nodes )
101     {
102         String version = null;
103         for ( DependencyNode node : nodes )
104         {
105             if ( version == null )
106             {
107                 version = getVersion( node.getArtifact() );
108             }
109             else
110             {
111                 if ( version.compareTo( getVersion( node.getArtifact() ) ) != 0 )
112                 {
113                     return true;
114                 }
115             }
116         }
117         return false;
118     }
119 
120     public List<List<DependencyNode>> getConflictedVersionNumbers()
121     {
122         List<List<DependencyNode>> output = new ArrayList<List<DependencyNode>>();
123         for ( List<DependencyNode> nodes : idsToNode.values() )
124         {
125             if ( containsConflicts( nodes ) )
126             {
127                 output.add( nodes );
128             }
129         }
130         return output;
131     }
132 }