Coverage Report - org.apache.maven.plugin.dependency.TreeMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
TreeMojo
0%
0/70
0%
0/32
3.556
 
 1  
 package org.apache.maven.plugin.dependency;
 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 org.apache.maven.artifact.resolver.filter.ArtifactFilter;
 23  
 import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter;
 24  
 import org.apache.maven.artifact.versioning.ArtifactVersion;
 25  
 import org.apache.maven.artifact.versioning.Restriction;
 26  
 import org.apache.maven.artifact.versioning.VersionRange;
 27  
 import org.apache.maven.plugin.AbstractMojo;
 28  
 import org.apache.maven.plugin.MojoExecutionException;
 29  
 import org.apache.maven.plugin.MojoFailureException;
 30  
 import org.apache.maven.plugin.dependency.treeSerializers.DOTDependencyNodeVisitor;
 31  
 import org.apache.maven.plugin.dependency.treeSerializers.GraphmlDependencyNodeVisitor;
 32  
 import org.apache.maven.plugin.dependency.treeSerializers.TGFDependencyNodeVisitor;
 33  
 import org.apache.maven.plugin.dependency.utils.DependencyUtil;
 34  
 import org.apache.maven.plugins.annotations.Component;
 35  
 import org.apache.maven.plugins.annotations.Mojo;
 36  
 import org.apache.maven.plugins.annotations.Parameter;
 37  
 import org.apache.maven.plugins.annotations.ResolutionScope;
 38  
 import org.apache.maven.project.MavenProject;
 39  
 import org.apache.maven.shared.artifact.filter.StrictPatternExcludesArtifactFilter;
 40  
 import org.apache.maven.shared.artifact.filter.StrictPatternIncludesArtifactFilter;
 41  
 import org.apache.maven.shared.dependency.graph.DependencyGraphBuilder;
 42  
 import org.apache.maven.shared.dependency.graph.DependencyGraphBuilderException;
 43  
 import org.apache.maven.shared.dependency.graph.DependencyNode;
 44  
 import org.apache.maven.shared.dependency.graph.filter.AncestorOrSelfDependencyNodeFilter;
 45  
 import org.apache.maven.shared.dependency.graph.filter.AndDependencyNodeFilter;
 46  
 import org.apache.maven.shared.dependency.graph.filter.ArtifactDependencyNodeFilter;
 47  
 import org.apache.maven.shared.dependency.graph.filter.DependencyNodeFilter;
 48  
 import org.apache.maven.shared.dependency.graph.traversal.BuildingDependencyNodeVisitor;
 49  
 import org.apache.maven.shared.dependency.graph.traversal.CollectingDependencyNodeVisitor;
 50  
 import org.apache.maven.shared.dependency.graph.traversal.DependencyNodeVisitor;
 51  
 import org.apache.maven.shared.dependency.graph.traversal.FilteringDependencyNodeVisitor;
 52  
 import org.apache.maven.shared.dependency.graph.traversal.SerializingDependencyNodeVisitor;
 53  
 import org.apache.maven.shared.dependency.graph.traversal.SerializingDependencyNodeVisitor.TreeTokens;
 54  
 
 55  
 import java.io.File;
 56  
 import java.io.IOException;
 57  
 import java.io.StringWriter;
 58  
 import java.io.Writer;
 59  
 import java.util.ArrayList;
 60  
 import java.util.Arrays;
 61  
 import java.util.List;
 62  
 
 63  
 /**
 64  
  * Displays the dependency tree for this project.
 65  
  *
 66  
  * @author <a href="mailto:markhobson@gmail.com">Mark Hobson</a>
 67  
  * @version $Id: TreeMojo.java 1400739 2012-10-21 23:05:22Z hboutemy $
 68  
  * @since 2.0-alpha-5
 69  
  */
 70  
 @Mojo( name = "tree", requiresDependencyResolution = ResolutionScope.TEST, threadSafe = true )
 71  0
 public class TreeMojo
 72  
     extends AbstractMojo
 73  
 {
 74  
     // fields -----------------------------------------------------------------
 75  
 
 76  
     /**
 77  
      * The Maven project.
 78  
      */
 79  
     @Component
 80  
     private MavenProject project;
 81  
 
 82  
     /**
 83  
      * The dependency tree builder to use.
 84  
      */
 85  
     @Component( hint = "default" )
 86  
     private DependencyGraphBuilder dependencyGraphBuilder;
 87  
 
 88  
     /**
 89  
      * If specified, this parameter will cause the dependency tree to be written to the path specified, instead of
 90  
      * writing to the console.
 91  
      *
 92  
      * @deprecated use outputFile instead.
 93  
      */
 94  
     @Parameter( property = "output" )
 95  
     private File output;
 96  
 
 97  
     /**
 98  
      * If specified, this parameter will cause the dependency tree to be written to the path specified, instead of
 99  
      * writing to the console.
 100  
      *
 101  
      * @since 2.0-alpha-5
 102  
      */
 103  
     @Parameter( property = "outputFile" )
 104  
     private File outputFile;
 105  
 
 106  
     /**
 107  
      * If specified, this parameter will cause the dependency tree to be written using the specified format. Currently
 108  
      * supported format are text, dot, graphml and tgf.
 109  
      * <p/>
 110  
      * These formats can be plotted to image files. An example of how to plot a dot file using
 111  
      * pygraphviz can be found <a href="http://networkx.lanl.gov/pygraphviz/tutorial.html#layout-and-drawing">here</a>
 112  
      *
 113  
      * @since 2.2
 114  
      */
 115  
     @Parameter( property = "outputType", defaultValue = "text" )
 116  
     private String outputType;
 117  
 
 118  
     /**
 119  
      * The scope to filter by when resolving the dependency tree, or <code>null</code> to include dependencies from
 120  
      * all scopes. Note that this feature does not currently work due to MNG-3236.
 121  
      *
 122  
      * @see <a href="http://jira.codehaus.org/browse/MNG-3236">MNG-3236</a>
 123  
      * @since 2.0-alpha-5
 124  
      */
 125  
     @Parameter( property = "scope" )
 126  
     private String scope;
 127  
 
 128  
     /**
 129  
      * Whether to include omitted nodes in the serialized dependency tree.
 130  
      *
 131  
      * @since 2.0-alpha-6
 132  
      * @deprecated in 2.5
 133  
      */
 134  
     @Parameter( property = "verbose", defaultValue = "false" )
 135  
     private boolean verbose;
 136  
 
 137  
     /**
 138  
      * The token set name to use when outputting the dependency tree. Possible values are <code>whitespace</code>,
 139  
      * <code>standard</code> or <code>extended</code>, which use whitespace, standard or extended ASCII sets
 140  
      * respectively.
 141  
      *
 142  
      * @since 2.0-alpha-6
 143  
      */
 144  
     @Parameter( property = "tokens", defaultValue = "standard" )
 145  
     private String tokens;
 146  
 
 147  
     /**
 148  
      * A comma-separated list of artifacts to filter the serialized dependency tree by, or <code>null</code> not to
 149  
      * filter the dependency tree. The artifact syntax is defined by <code>StrictPatternIncludesArtifactFilter</code>.
 150  
      *
 151  
      * @see StrictPatternIncludesArtifactFilter
 152  
      * @since 2.0-alpha-6
 153  
      */
 154  
     @Parameter( property = "includes" )
 155  
     private String includes;
 156  
 
 157  
     /**
 158  
      * A comma-separated list of artifacts to filter from the serialized dependency tree, or <code>null</code> not to
 159  
      * filter any artifacts from the dependency tree. The artifact syntax is defined by
 160  
      * <code>StrictPatternExcludesArtifactFilter</code>.
 161  
      *
 162  
      * @see StrictPatternExcludesArtifactFilter
 163  
      * @since 2.0-alpha-6
 164  
      */
 165  
     @Parameter( property = "excludes" )
 166  
     private String excludes;
 167  
 
 168  
     /**
 169  
      * The computed dependency tree root node of the Maven project.
 170  
      */
 171  
     private DependencyNode rootNode;
 172  
 
 173  
     /**
 174  
      * Whether to append outputs into the output file or overwrite it.
 175  
      *
 176  
      * @since 2.2
 177  
      */
 178  
     @Parameter( property = "appendOutput", defaultValue = "false" )
 179  
     private boolean appendOutput;
 180  
 
 181  
     // Mojo methods -----------------------------------------------------------
 182  
 
 183  
     /*
 184  
      * @see org.apache.maven.plugin.Mojo#execute()
 185  
      */
 186  
     public void execute()
 187  
         throws MojoExecutionException, MojoFailureException
 188  
     {
 189  
 
 190  0
         if ( output != null )
 191  
         {
 192  0
             getLog().warn( "The parameter output is deprecated. Use outputFile instead." );
 193  0
             this.outputFile = output;
 194  
         }
 195  
 
 196  0
         ArtifactFilter artifactFilter = createResolvingArtifactFilter();
 197  
 
 198  
         try
 199  
         {
 200  
             // TODO: note that filter does not get applied due to MNG-3236
 201  
 
 202  0
             rootNode = dependencyGraphBuilder.buildDependencyGraph( project, artifactFilter );
 203  
 
 204  0
             String dependencyTreeString = serializeDependencyTree( rootNode );
 205  
 
 206  0
             if ( outputFile != null )
 207  
             {
 208  0
                 DependencyUtil.write( dependencyTreeString, outputFile, this.appendOutput, getLog() );
 209  
 
 210  0
                 getLog().info( "Wrote dependency tree to: " + outputFile );
 211  
             }
 212  
             else
 213  
             {
 214  0
                 DependencyUtil.log( dependencyTreeString, getLog() );
 215  
             }
 216  
         }
 217  0
         catch ( DependencyGraphBuilderException exception )
 218  
         {
 219  0
             throw new MojoExecutionException( "Cannot build project dependency graph", exception );
 220  
         }
 221  0
         catch ( IOException exception )
 222  
         {
 223  0
             throw new MojoExecutionException( "Cannot serialise project dependency graph", exception );
 224  0
         }
 225  0
     }
 226  
 
 227  
     // public methods ---------------------------------------------------------
 228  
 
 229  
     /**
 230  
      * Gets the Maven project used by this mojo.
 231  
      *
 232  
      * @return the Maven project
 233  
      */
 234  
     public MavenProject getProject()
 235  
     {
 236  0
         return project;
 237  
     }
 238  
 
 239  
     /**
 240  
      * Gets the computed dependency graph root node for the Maven project.
 241  
      *
 242  
      * @return the dependency tree root node
 243  
      */
 244  
     public DependencyNode getDependencyGraph()
 245  
     {
 246  0
         return rootNode;
 247  
     }
 248  
 
 249  
     // private methods --------------------------------------------------------
 250  
 
 251  
     /**
 252  
      * Gets the artifact filter to use when resolving the dependency tree.
 253  
      *
 254  
      * @return the artifact filter
 255  
      */
 256  
     private ArtifactFilter createResolvingArtifactFilter()
 257  
     {
 258  
         ArtifactFilter filter;
 259  
 
 260  
         // filter scope
 261  0
         if ( scope != null )
 262  
         {
 263  0
             getLog().debug( "+ Resolving dependency tree for scope '" + scope + "'" );
 264  
 
 265  0
             filter = new ScopeArtifactFilter( scope );
 266  
         }
 267  
         else
 268  
         {
 269  0
             filter = null;
 270  
         }
 271  
 
 272  0
         return filter;
 273  
     }
 274  
 
 275  
     /**
 276  
      * Serializes the specified dependency tree to a string.
 277  
      *
 278  
      * @param rootNode the dependency tree root node to serialize
 279  
      * @return the serialized dependency tree
 280  
      */
 281  
     private String serializeDependencyTree( DependencyNode rootNode )
 282  
     {
 283  0
         StringWriter writer = new StringWriter();
 284  
 
 285  0
         DependencyNodeVisitor visitor = getSerializingDependencyNodeVisitor( writer );
 286  
 
 287  
         // TODO: remove the need for this when the serializer can calculate last nodes from visitor calls only
 288  0
         visitor = new BuildingDependencyNodeVisitor( visitor );
 289  
 
 290  0
         DependencyNodeFilter filter = createDependencyNodeFilter();
 291  
 
 292  0
         if ( filter != null )
 293  
         {
 294  0
             CollectingDependencyNodeVisitor collectingVisitor = new CollectingDependencyNodeVisitor();
 295  0
             DependencyNodeVisitor firstPassVisitor = new FilteringDependencyNodeVisitor( collectingVisitor, filter );
 296  0
             rootNode.accept( firstPassVisitor );
 297  
 
 298  0
             DependencyNodeFilter secondPassFilter =
 299  
                 new AncestorOrSelfDependencyNodeFilter( collectingVisitor.getNodes() );
 300  0
             visitor = new FilteringDependencyNodeVisitor( visitor, secondPassFilter );
 301  
         }
 302  
 
 303  0
         rootNode.accept( visitor );
 304  
 
 305  0
         return writer.toString();
 306  
     }
 307  
 
 308  
     public DependencyNodeVisitor getSerializingDependencyNodeVisitor( Writer writer )
 309  
     {
 310  0
         if ( "graphml".equals( outputType ) )
 311  
         {
 312  0
             return new GraphmlDependencyNodeVisitor( writer );
 313  
         }
 314  0
         else if ( "tgf".equals( outputType ) )
 315  
         {
 316  0
             return new TGFDependencyNodeVisitor( writer );
 317  
         }
 318  0
         else if ( "dot".equals( outputType ) )
 319  
         {
 320  0
             return new DOTDependencyNodeVisitor( writer );
 321  
         }
 322  
         else
 323  
         {
 324  0
             return new SerializingDependencyNodeVisitor( writer, toTreeTokens( tokens ) );
 325  
         }
 326  
     }
 327  
 
 328  
     /**
 329  
      * Gets the tree tokens instance for the specified name.
 330  
      *
 331  
      * @param tokens the tree tokens name
 332  
      * @return the <code>TreeTokens</code> instance
 333  
      */
 334  
     private TreeTokens toTreeTokens( String tokens )
 335  
     {
 336  
         TreeTokens treeTokens;
 337  
 
 338  0
         if ( "whitespace".equals( tokens ) )
 339  
         {
 340  0
             getLog().debug( "+ Using whitespace tree tokens" );
 341  
 
 342  0
             treeTokens = SerializingDependencyNodeVisitor.WHITESPACE_TOKENS;
 343  
         }
 344  0
         else if ( "extended".equals( tokens ) )
 345  
         {
 346  0
             getLog().debug( "+ Using extended tree tokens" );
 347  
 
 348  0
             treeTokens = SerializingDependencyNodeVisitor.EXTENDED_TOKENS;
 349  
         }
 350  
         else
 351  
         {
 352  0
             treeTokens = SerializingDependencyNodeVisitor.STANDARD_TOKENS;
 353  
         }
 354  
 
 355  0
         return treeTokens;
 356  
     }
 357  
 
 358  
     /**
 359  
      * Gets the dependency node filter to use when serializing the dependency tree.
 360  
      *
 361  
      * @return the dependency node filter, or <code>null</code> if none required
 362  
      */
 363  
     private DependencyNodeFilter createDependencyNodeFilter()
 364  
     {
 365  0
         List<DependencyNodeFilter> filters = new ArrayList<DependencyNodeFilter>();
 366  
 
 367  
         // filter node states
 368  
         /*if ( !verbose )
 369  
         {
 370  
             getLog().debug( "+ Filtering omitted nodes from dependency tree" );
 371  
 
 372  
             filters.add( StateDependencyNodeFilter.INCLUDED );
 373  
         }*/
 374  
 
 375  
         // filter includes
 376  0
         if ( includes != null )
 377  
         {
 378  0
             List<String> patterns = Arrays.asList( includes.split( "," ) );
 379  
 
 380  0
             getLog().debug( "+ Filtering dependency tree by artifact include patterns: " + patterns );
 381  
 
 382  0
             ArtifactFilter artifactFilter = new StrictPatternIncludesArtifactFilter( patterns );
 383  0
             filters.add( new ArtifactDependencyNodeFilter( artifactFilter ) );
 384  
         }
 385  
 
 386  
         // filter excludes
 387  0
         if ( excludes != null )
 388  
         {
 389  0
             List<String> patterns = Arrays.asList( excludes.split( "," ) );
 390  
 
 391  0
             getLog().debug( "+ Filtering dependency tree by artifact exclude patterns: " + patterns );
 392  
 
 393  0
             ArtifactFilter artifactFilter = new StrictPatternExcludesArtifactFilter( patterns );
 394  0
             filters.add( new ArtifactDependencyNodeFilter( artifactFilter ) );
 395  
         }
 396  
 
 397  0
         return filters.isEmpty() ? null : new AndDependencyNodeFilter( filters );
 398  
     }
 399  
 
 400  
     //following is required because the version handling in maven code
 401  
     //doesn't work properly. I ripped it out of the enforcer rules.
 402  
 
 403  
 
 404  
     /**
 405  
      * Copied from Artifact.VersionRange. This is tweaked to handle singular ranges properly. Currently the default
 406  
      * containsVersion method assumes a singular version means allow everything. This method assumes that "2.0.4" ==
 407  
      * "[2.0.4,)"
 408  
      *
 409  
      * @param allowedRange range of allowed versions.
 410  
      * @param theVersion   the version to be checked.
 411  
      * @return true if the version is contained by the range.
 412  
      */
 413  
     public static boolean containsVersion( VersionRange allowedRange, ArtifactVersion theVersion )
 414  
     {
 415  0
         ArtifactVersion recommendedVersion = allowedRange.getRecommendedVersion();
 416  0
         if ( recommendedVersion == null )
 417  
         {
 418  0
             @SuppressWarnings ("unchecked") List<Restriction> restrictions = allowedRange.getRestrictions();
 419  0
             for ( Restriction restriction : restrictions )
 420  
             {
 421  0
                 if ( restriction.containsVersion( theVersion ) )
 422  
                 {
 423  0
                     return true;
 424  
                 }
 425  
             }
 426  
         }
 427  
 
 428  
         // only singular versions ever have a recommendedVersion
 429  0
         return recommendedVersion.compareTo( theVersion ) <= 0;
 430  
     }
 431  
 
 432  
 }