Coverage Report - org.apache.maven.report.projectinfo.dependencies.Dependencies
 
Classes in this File Line Coverage Branch Coverage Complexity
Dependencies
63 %
62/99
44 %
32/72
0
 
 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  1
     {
 89  1
         this.project = project;
 90  1
         this.dependencyTreeNode = dependencyTreeNode;
 91  1
         this.classesAnalyzer = classesAnalyzer;
 92  
 
 93  
         /*
 94  
          * Workaround to ensure proper File objects in the Artifacts from the ReportResolutionListener
 95  
          */
 96  1
         mapArtifactFiles( this.dependencyTreeNode );
 97  1
     }
 98  
 
 99  
     /**
 100  
      * Getter for the project
 101  
      *
 102  
      * @return the project
 103  
      */
 104  
     public MavenProject getProject()
 105  
     {
 106  0
         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  1
         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  4
         if ( projectDependencies != null )
 123  
         {
 124  3
             return projectDependencies;
 125  
         }
 126  
 
 127  1
         projectDependencies = new ArrayList<Artifact>();
 128  
         for ( @SuppressWarnings( "unchecked" )
 129  1
         Iterator<DependencyNode> i = dependencyTreeNode.getChildren().iterator(); i.hasNext(); )
 130  
         {
 131  1
             DependencyNode dependencyNode = i.next();
 132  
 
 133  1
             projectDependencies.add( dependencyNode.getArtifact() );
 134  1
         }
 135  
 
 136  1
         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  1
         if ( projectTransitiveDependencies != null )
 145  
         {
 146  0
             return projectTransitiveDependencies;
 147  
         }
 148  
 
 149  1
         projectTransitiveDependencies = new ArrayList<Artifact>( getAllDependencies() );
 150  1
         projectTransitiveDependencies.removeAll( getProjectDependencies() );
 151  
 
 152  1
         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  2
         if ( allDependencies != null )
 161  
         {
 162  1
             return allDependencies;
 163  
         }
 164  
 
 165  1
         allDependencies = new ArrayList<Artifact>();
 166  
         for ( @SuppressWarnings( "unchecked" )
 167  1
         Iterator<DependencyNode> i = dependencyTreeNode.getChildren().iterator(); i.hasNext(); )
 168  
         {
 169  1
             DependencyNode dependencyNode = i.next();
 170  
 
 171  1
             if ( dependencyNode.getState() != DependencyNode.INCLUDED )
 172  
             {
 173  0
                 continue;
 174  
             }
 175  
 
 176  1
             if ( dependencyNode.getArtifact().getGroupId().equals( project.getGroupId() )
 177  
                 && dependencyNode.getArtifact().getArtifactId().equals( project.getArtifactId() )
 178  
                 && dependencyNode.getArtifact().getVersion().equals( project.getVersion() ) )
 179  
             {
 180  0
                 continue;
 181  
             }
 182  
 
 183  1
             if ( !allDependencies.contains( dependencyNode.getArtifact() ) )
 184  
             {
 185  1
                 allDependencies.add( dependencyNode.getArtifact() );
 186  
             }
 187  1
             getAllDependencies( dependencyNode );
 188  1
         }
 189  
 
 190  1
         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  2
         if ( isTransitively )
 205  
         {
 206  1
             if ( transitiveDependenciesByScope != null )
 207  
             {
 208  0
                 return transitiveDependenciesByScope;
 209  
             }
 210  
 
 211  1
             transitiveDependenciesByScope = new HashMap<String, List<Artifact>>();
 212  1
             for ( Artifact artifact : getTransitiveDependencies() )
 213  
             {
 214  0
                 List<Artifact> multiValue = transitiveDependenciesByScope.get( artifact.getScope() );
 215  0
                 if ( multiValue == null )
 216  
                 {
 217  0
                     multiValue = new ArrayList<Artifact>();
 218  
                 }
 219  
 
 220  0
                 if ( !multiValue.contains( artifact ) )
 221  
                 {
 222  0
                     multiValue.add( artifact );
 223  
                 }
 224  0
                 transitiveDependenciesByScope.put( artifact.getScope(), multiValue );
 225  0
             }
 226  
 
 227  1
             return transitiveDependenciesByScope;
 228  
         }
 229  
 
 230  1
         if ( dependenciesByScope != null )
 231  
         {
 232  0
             return dependenciesByScope;
 233  
         }
 234  
 
 235  1
         dependenciesByScope = new HashMap<String, List<Artifact>>();
 236  1
         for ( Artifact artifact : getProjectDependencies() )
 237  
         {
 238  1
             List<Artifact> multiValue = dependenciesByScope.get( artifact.getScope() );
 239  1
             if ( multiValue == null )
 240  
             {
 241  1
                 multiValue = new ArrayList<Artifact>();
 242  
             }
 243  
 
 244  1
             if ( !multiValue.contains( artifact ) )
 245  
             {
 246  1
                 multiValue.add( artifact );
 247  
             }
 248  1
             dependenciesByScope.put( artifact.getScope(), multiValue );
 249  1
         }
 250  
 
 251  1
         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  0
         if ( dependencyDetails == null )
 263  
         {
 264  0
             dependencyDetails = new HashMap<String, JarData>();
 265  
         }
 266  
 
 267  0
         JarData old = dependencyDetails.get( artifact.getId() );
 268  0
         if ( dependencyDetails.get( artifact.getId() ) != null )
 269  
         {
 270  0
             return old;
 271  
         }
 272  
 
 273  0
         JarAnalyzer jarAnalyzer = new JarAnalyzer( artifact.getFile() );
 274  
         try
 275  
         {
 276  0
             classesAnalyzer.analyze( jarAnalyzer );
 277  
         }
 278  
         finally
 279  
         {
 280  0
             jarAnalyzer.closeQuietly();
 281  0
         }
 282  
 
 283  0
         dependencyDetails.put( artifact.getId(), jarAnalyzer.getJarData() );
 284  
 
 285  0
         return jarAnalyzer.getJarData();
 286  
     }
 287  
 
 288  
     // ----------------------------------------------------------------------
 289  
     // Private methods
 290  
     // ----------------------------------------------------------------------
 291  
 
 292  
     private void mapArtifactFiles( DependencyNode node )
 293  
     {
 294  
         @SuppressWarnings( "unchecked" )
 295  2
         List<DependencyNode> childs = node.getChildren();
 296  2
         if ( ( childs == null ) || childs.isEmpty() )
 297  
         {
 298  1
             return;
 299  
         }
 300  
 
 301  1
         Iterator<DependencyNode> it = childs.iterator();
 302  2
         while ( it.hasNext() )
 303  
         {
 304  1
             DependencyNode anode = it.next();
 305  1
             String key = ArtifactUtils.versionlessKey( anode.getArtifact() );
 306  1
             Artifact projartifact = (Artifact) project.getArtifactMap().get( key );
 307  1
             if ( projartifact != null )
 308  
             {
 309  0
                 anode.getArtifact().setFile( projartifact.getFile() );
 310  
             }
 311  
 
 312  1
             mapArtifactFiles( anode );
 313  1
         }
 314  1
     }
 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  1
         if ( dependencyNode == null || dependencyNode.getChildren() == null )
 324  
         {
 325  0
             if ( !allDependencies.contains( dependencyNode.getArtifact() ) )
 326  
             {
 327  0
                 allDependencies.add( dependencyNode.getArtifact() );
 328  
             }
 329  0
             return;
 330  
         }
 331  
 
 332  
         for ( @SuppressWarnings( "unchecked" )
 333  1
         Iterator<DependencyNode> i = dependencyNode.getChildren().iterator(); i.hasNext(); )
 334  
         {
 335  0
             DependencyNode subdependencyNode = i.next();
 336  
 
 337  0
             if ( subdependencyNode.getState() != DependencyNode.INCLUDED )
 338  
             {
 339  0
                 continue;
 340  
             }
 341  
 
 342  0
             if ( subdependencyNode.getArtifact().getGroupId().equals( project.getGroupId() )
 343  
                 && subdependencyNode.getArtifact().getArtifactId().equals( project.getArtifactId() )
 344  
                 && subdependencyNode.getArtifact().getVersion().equals( project.getVersion() ) )
 345  
             {
 346  0
                 continue;
 347  
             }
 348  
 
 349  0
             if ( !allDependencies.contains( subdependencyNode.getArtifact() ) )
 350  
             {
 351  0
                 allDependencies.add( subdependencyNode.getArtifact() );
 352  
             }
 353  0
             getAllDependencies( subdependencyNode );
 354  0
         }
 355  1
     }
 356  
 }