View Javadoc

1   package org.apache.maven.report.projectinfo.dependencies;
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.io.IOException;
23  import java.util.ArrayList;
24  import java.util.HashMap;
25  import java.util.Iterator;
26  import java.util.List;
27  import java.util.Map;
28  
29  import org.apache.maven.artifact.Artifact;
30  import org.apache.maven.artifact.ArtifactUtils;
31  import org.apache.maven.project.MavenProject;
32  import org.apache.maven.shared.dependency.tree.DependencyNode;
33  import org.apache.maven.shared.jar.JarAnalyzer;
34  import org.apache.maven.shared.jar.JarData;
35  import org.apache.maven.shared.jar.classes.JarClassesAnalysis;
36  
37  /**
38   * @version $Id: Dependencies.java 1038048 2010-11-23 10:52:14Z vsiveton $
39   * @since 2.1
40   */
41  public class Dependencies
42  {
43      private final MavenProject project;
44  
45      private final DependencyNode dependencyTreeNode;
46  
47      private final JarClassesAnalysis classesAnalyzer;
48  
49      /**
50       * @since 2.1
51       */
52      private List<Artifact> projectDependencies;
53  
54      /**
55       * @since 2.1
56       */
57      private List<Artifact> projectTransitiveDependencies;
58  
59      /**
60       * @since 2.1
61       */
62      private List<Artifact> allDependencies;
63  
64      /**
65       * @since 2.1
66       */
67      private Map<String, List<Artifact>> dependenciesByScope;
68  
69      /**
70       * @since 2.1
71       */
72      private Map<String, List<Artifact>> transitiveDependenciesByScope;
73  
74      /**
75       * @since 2.1
76       */
77      private Map<String, JarData> dependencyDetails;
78  
79      /**
80       * Default constructor
81       *
82       * @param project the MavenProject.
83       * @param dependencyTreeNode the DependencyNode.
84       * @param classesAnalyzer the JarClassesAnalysis.
85       */
86      public Dependencies( MavenProject project, DependencyNode dependencyTreeNode,
87                           JarClassesAnalysis classesAnalyzer )
88      {
89          this.project = project;
90          this.dependencyTreeNode = dependencyTreeNode;
91          this.classesAnalyzer = classesAnalyzer;
92  
93          /*
94           * Workaround to ensure proper File objects in the Artifacts from the ReportResolutionListener
95           */
96          mapArtifactFiles( this.dependencyTreeNode );
97      }
98  
99      /**
100      * Getter for the project
101      *
102      * @return the project
103      */
104     public MavenProject getProject()
105     {
106         return project;
107     }
108 
109     /**
110      * @return <code>true</code> if getProjectDependencies() is not empty, <code>false</code> otherwise.
111      */
112     public boolean hasDependencies()
113     {
114         return ( getProjectDependencies() != null ) && ( !getProjectDependencies().isEmpty() );
115     }
116 
117     /**
118      * @return a list of <code>Artifact</code> from the project.
119      */
120     public List<Artifact> getProjectDependencies()
121     {
122         if ( projectDependencies != null )
123         {
124             return projectDependencies;
125         }
126 
127         projectDependencies = new ArrayList<Artifact>();
128         for ( @SuppressWarnings( "unchecked" )
129         Iterator<DependencyNode> i = dependencyTreeNode.getChildren().iterator(); i.hasNext(); )
130         {
131             DependencyNode dependencyNode = i.next();
132 
133             projectDependencies.add( dependencyNode.getArtifact() );
134         }
135 
136         return projectDependencies;
137     }
138 
139     /**
140      * @return a list of transitive <code>Artifact</code> from the project.
141      */
142     public List<Artifact> getTransitiveDependencies()
143     {
144         if ( projectTransitiveDependencies != null )
145         {
146             return projectTransitiveDependencies;
147         }
148 
149         projectTransitiveDependencies = new ArrayList<Artifact>( getAllDependencies() );
150         projectTransitiveDependencies.removeAll( getProjectDependencies() );
151 
152         return projectTransitiveDependencies;
153     }
154 
155     /**
156      * @return a list of included <code>Artifact</code> returned by the dependency tree.
157      */
158     public List<Artifact> getAllDependencies()
159     {
160         if ( allDependencies != null )
161         {
162             return allDependencies;
163         }
164 
165         allDependencies = new ArrayList<Artifact>();
166         for ( @SuppressWarnings( "unchecked" )
167         Iterator<DependencyNode> i = dependencyTreeNode.getChildren().iterator(); i.hasNext(); )
168         {
169             DependencyNode dependencyNode = i.next();
170 
171             if ( dependencyNode.getState() != DependencyNode.INCLUDED )
172             {
173                 continue;
174             }
175 
176             if ( dependencyNode.getArtifact().getGroupId().equals( project.getGroupId() )
177                 && dependencyNode.getArtifact().getArtifactId().equals( project.getArtifactId() )
178                 && dependencyNode.getArtifact().getVersion().equals( project.getVersion() ) )
179             {
180                 continue;
181             }
182 
183             if ( !allDependencies.contains( dependencyNode.getArtifact() ) )
184             {
185                 allDependencies.add( dependencyNode.getArtifact() );
186             }
187             getAllDependencies( dependencyNode );
188         }
189 
190         return allDependencies;
191     }
192 
193     /**
194      * @param isTransitively <code>true</code> to return transitive dependencies, <code>false</code> otherwise.
195      * @return a map with supported scopes as key and a list of <code>Artifact</code> as values.
196      * @see Artifact#SCOPE_COMPILE
197      * @see Artifact#SCOPE_PROVIDED
198      * @see Artifact#SCOPE_RUNTIME
199      * @see Artifact#SCOPE_SYSTEM
200      * @see Artifact#SCOPE_TEST
201      */
202     public Map<String, List<Artifact>> getDependenciesByScope( boolean isTransitively )
203     {
204         if ( isTransitively )
205         {
206             if ( transitiveDependenciesByScope != null )
207             {
208                 return transitiveDependenciesByScope;
209             }
210 
211             transitiveDependenciesByScope = new HashMap<String, List<Artifact>>();
212             for ( Artifact artifact : getTransitiveDependencies() )
213             {
214                 List<Artifact> multiValue = transitiveDependenciesByScope.get( artifact.getScope() );
215                 if ( multiValue == null )
216                 {
217                     multiValue = new ArrayList<Artifact>();
218                 }
219 
220                 if ( !multiValue.contains( artifact ) )
221                 {
222                     multiValue.add( artifact );
223                 }
224                 transitiveDependenciesByScope.put( artifact.getScope(), multiValue );
225             }
226 
227             return transitiveDependenciesByScope;
228         }
229 
230         if ( dependenciesByScope != null )
231         {
232             return dependenciesByScope;
233         }
234 
235         dependenciesByScope = new HashMap<String, List<Artifact>>();
236         for ( Artifact artifact : getProjectDependencies() )
237         {
238             List<Artifact> multiValue = dependenciesByScope.get( artifact.getScope() );
239             if ( multiValue == null )
240             {
241                 multiValue = new ArrayList<Artifact>();
242             }
243 
244             if ( !multiValue.contains( artifact ) )
245             {
246                 multiValue.add( artifact );
247             }
248             dependenciesByScope.put( artifact.getScope(), multiValue );
249         }
250 
251         return dependenciesByScope;
252     }
253 
254     /**
255      * @param artifact the artifact.
256      * @return the jardata object from the artifact
257      * @throws IOException if any
258      */
259     public JarData getJarDependencyDetails( Artifact artifact )
260         throws IOException
261     {
262         if ( dependencyDetails == null )
263         {
264             dependencyDetails = new HashMap<String, JarData>();
265         }
266 
267         JarData old = dependencyDetails.get( artifact.getId() );
268         if ( dependencyDetails.get( artifact.getId() ) != null )
269         {
270             return old;
271         }
272 
273         JarAnalyzer jarAnalyzer = new JarAnalyzer( artifact.getFile() );
274         try
275         {
276             classesAnalyzer.analyze( jarAnalyzer );
277         }
278         finally
279         {
280             jarAnalyzer.closeQuietly();
281         }
282 
283         dependencyDetails.put( artifact.getId(), jarAnalyzer.getJarData() );
284 
285         return jarAnalyzer.getJarData();
286     }
287 
288     // ----------------------------------------------------------------------
289     // Private methods
290     // ----------------------------------------------------------------------
291 
292     private void mapArtifactFiles( DependencyNode node )
293     {
294         @SuppressWarnings( "unchecked" )
295         List<DependencyNode> childs = node.getChildren();
296         if ( ( childs == null ) || childs.isEmpty() )
297         {
298             return;
299         }
300 
301         Iterator<DependencyNode> it = childs.iterator();
302         while ( it.hasNext() )
303         {
304             DependencyNode anode = it.next();
305             String key = ArtifactUtils.versionlessKey( anode.getArtifact() );
306             Artifact projartifact = (Artifact) project.getArtifactMap().get( key );
307             if ( projartifact != null )
308             {
309                 anode.getArtifact().setFile( projartifact.getFile() );
310             }
311 
312             mapArtifactFiles( anode );
313         }
314     }
315 
316     /**
317      * Recursive method to get all dependencies from a given <code>dependencyNode</code>
318      *
319      * @param dependencyNode not null
320      */
321     private void getAllDependencies( DependencyNode dependencyNode )
322     {
323         if ( dependencyNode == null || dependencyNode.getChildren() == null )
324         {
325             if ( !allDependencies.contains( dependencyNode.getArtifact() ) )
326             {
327                 allDependencies.add( dependencyNode.getArtifact() );
328             }
329             return;
330         }
331 
332         for ( @SuppressWarnings( "unchecked" )
333         Iterator<DependencyNode> i = dependencyNode.getChildren().iterator(); i.hasNext(); )
334         {
335             DependencyNode subdependencyNode = i.next();
336 
337             if ( subdependencyNode.getState() != DependencyNode.INCLUDED )
338             {
339                 continue;
340             }
341 
342             if ( subdependencyNode.getArtifact().getGroupId().equals( project.getGroupId() )
343                 && subdependencyNode.getArtifact().getArtifactId().equals( project.getArtifactId() )
344                 && subdependencyNode.getArtifact().getVersion().equals( project.getVersion() ) )
345             {
346                 continue;
347             }
348 
349             if ( !allDependencies.contains( subdependencyNode.getArtifact() ) )
350             {
351                 allDependencies.add( subdependencyNode.getArtifact() );
352             }
353             getAllDependencies( subdependencyNode );
354         }
355     }
356 }