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.List;
26  import java.util.Map;
27  import java.util.jar.JarEntry;
28  
29  import org.apache.maven.artifact.Artifact;
30  import org.apache.maven.project.MavenProject;
31  import org.apache.maven.shared.dependency.graph.DependencyNode;
32  import org.apache.maven.shared.jar.JarAnalyzer;
33  import org.apache.maven.shared.jar.JarData;
34  import org.apache.maven.shared.jar.classes.JarClasses;
35  import org.apache.maven.shared.jar.classes.JarClassesAnalysis;
36  
37  /**
38   * @version $Id: Dependencies.html 861765 2013-05-12 18:46:29Z hboutemy $
39   * @since 2.1
40   */
41  public class Dependencies
42  {
43      private final MavenProject project;
44  
45      private final DependencyNode dependencyNode;
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, JarClassesAnalysis classesAnalyzer )
87      {
88          this.project = project;
89          this.dependencyNode = dependencyTreeNode;
90          this.classesAnalyzer = classesAnalyzer;
91      }
92  
93      /**
94       * Getter for the project
95       *
96       * @return the project
97       */
98      public MavenProject getProject()
99      {
100         return project;
101     }
102 
103     /**
104      * @return <code>true</code> if getProjectDependencies() is not empty, <code>false</code> otherwise.
105      */
106     public boolean hasDependencies()
107     {
108         return ( getProjectDependencies() != null ) && ( !getProjectDependencies().isEmpty() );
109     }
110 
111     /**
112      * @return a list of <code>Artifact</code> from the project.
113      */
114     public List<Artifact> getProjectDependencies()
115     {
116         if ( projectDependencies != null )
117         {
118             return projectDependencies;
119         }
120 
121         projectDependencies = new ArrayList<Artifact>();
122         for ( DependencyNode dep : dependencyNode.getChildren() )
123         {
124             projectDependencies.add( dep.getArtifact() );
125         }
126 
127         return projectDependencies;
128     }
129 
130     /**
131      * @return a list of transitive <code>Artifact</code> from the project.
132      */
133     public List<Artifact> getTransitiveDependencies()
134     {
135         if ( projectTransitiveDependencies != null )
136         {
137             return projectTransitiveDependencies;
138         }
139 
140         projectTransitiveDependencies = new ArrayList<Artifact>( getAllDependencies() );
141         projectTransitiveDependencies.removeAll( getProjectDependencies() );
142 
143         return projectTransitiveDependencies;
144     }
145 
146     /**
147      * @return a list of included <code>Artifact</code> returned by the dependency tree.
148      */
149     public List<Artifact> getAllDependencies()
150     {
151         if ( allDependencies != null )
152         {
153             return allDependencies;
154         }
155 
156         allDependencies = new ArrayList<Artifact>();
157 
158         addAllChildrenDependencies( dependencyNode );
159 
160         return allDependencies;
161     }
162 
163     /**
164      * @param isTransitively <code>true</code> to return transitive dependencies, <code>false</code> otherwise.
165      * @return a map with supported scopes as key and a list of <code>Artifact</code> as values.
166      * @see Artifact#SCOPE_COMPILE
167      * @see Artifact#SCOPE_PROVIDED
168      * @see Artifact#SCOPE_RUNTIME
169      * @see Artifact#SCOPE_SYSTEM
170      * @see Artifact#SCOPE_TEST
171      */
172     public Map<String, List<Artifact>> getDependenciesByScope( boolean isTransitively )
173     {
174         if ( isTransitively )
175         {
176             if ( transitiveDependenciesByScope != null )
177             {
178                 return transitiveDependenciesByScope;
179             }
180 
181             transitiveDependenciesByScope = new HashMap<String, List<Artifact>>();
182             for ( Artifact artifact : getTransitiveDependencies() )
183             {
184                 List<Artifact> multiValue = transitiveDependenciesByScope.get( artifact.getScope() );
185                 if ( multiValue == null )
186                 {
187                     multiValue = new ArrayList<Artifact>();
188                 }
189 
190                 if ( !multiValue.contains( artifact ) )
191                 {
192                     multiValue.add( artifact );
193                 }
194                 transitiveDependenciesByScope.put( artifact.getScope(), multiValue );
195             }
196 
197             return transitiveDependenciesByScope;
198         }
199 
200         if ( dependenciesByScope != null )
201         {
202             return dependenciesByScope;
203         }
204 
205         dependenciesByScope = new HashMap<String, List<Artifact>>();
206         for ( Artifact artifact : getProjectDependencies() )
207         {
208             List<Artifact> multiValue = dependenciesByScope.get( artifact.getScope() );
209             if ( multiValue == null )
210             {
211                 multiValue = new ArrayList<Artifact>();
212             }
213 
214             if ( !multiValue.contains( artifact ) )
215             {
216                 multiValue.add( artifact );
217             }
218             dependenciesByScope.put( artifact.getScope(), multiValue );
219         }
220 
221         return dependenciesByScope;
222     }
223 
224     /**
225      * @param artifact the artifact.
226      * @return the jardata object from the artifact
227      * @throws IOException if any
228      */
229     public JarData getJarDependencyDetails( Artifact artifact )
230         throws IOException
231     {
232         if ( dependencyDetails == null )
233         {
234             dependencyDetails = new HashMap<String, JarData>();
235         }
236 
237         JarData jarData = dependencyDetails.get( artifact.getId() );
238         if ( jarData != null )
239         {
240             return jarData;
241         }
242 
243         if ( artifact.getFile().isDirectory() )
244         {
245             jarData = new JarData( artifact.getFile(), null, new ArrayList<JarEntry>() );
246 
247             jarData.setJarClasses( new JarClasses() );
248         }
249         else
250         {
251             JarAnalyzer jarAnalyzer = new JarAnalyzer( artifact.getFile() );
252 
253             try
254             {
255                 classesAnalyzer.analyze( jarAnalyzer );
256             }
257             finally
258             {
259                 jarAnalyzer.closeQuietly();
260             }
261             
262             jarData = jarAnalyzer.getJarData();
263         }
264 
265         dependencyDetails.put( artifact.getId(), jarData );
266     
267         return jarData;
268     }
269 
270     // ----------------------------------------------------------------------
271     // Private methods
272     // ----------------------------------------------------------------------
273 
274     /**
275      * Recursive method to get all dependencies from a given <code>dependencyNode</code>
276      *
277      * @param dependencyNode not null
278      */
279     private void addAllChildrenDependencies( DependencyNode dependencyNode )
280     {
281         for ( DependencyNode subdependencyNode : dependencyNode.getChildren() )
282         {
283             Artifact artifact = subdependencyNode.getArtifact();
284 
285             if ( artifact.getGroupId().equals( project.getGroupId() )
286                 && artifact.getArtifactId().equals( project.getArtifactId() )
287                 && artifact.getVersion().equals( project.getVersion() ) )
288             {
289                 continue;
290             }
291 
292             if ( !allDependencies.contains( artifact ) )
293             {
294                 allDependencies.add( artifact );
295             }
296 
297             addAllChildrenDependencies( subdependencyNode );
298         }
299     }
300 }