View Javadoc
1   package org.apache.maven.plugins.dependency.tree;
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.execution.MavenSession;
28  import org.apache.maven.plugin.AbstractMojo;
29  import org.apache.maven.plugin.MojoExecutionException;
30  import org.apache.maven.plugin.MojoFailureException;
31  import org.apache.maven.plugins.annotations.Component;
32  import org.apache.maven.plugins.annotations.Mojo;
33  import org.apache.maven.plugins.annotations.Parameter;
34  import org.apache.maven.plugins.annotations.ResolutionScope;
35  import org.apache.maven.plugins.dependency.utils.DependencyUtil;
36  import org.apache.maven.project.DefaultProjectBuildingRequest;
37  import org.apache.maven.project.MavenProject;
38  import org.apache.maven.project.ProjectBuildingRequest;
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.DependencyCollectorBuilder;
42  import org.apache.maven.shared.dependency.graph.DependencyCollectorBuilderException;
43  import org.apache.maven.shared.dependency.graph.DependencyGraphBuilder;
44  import org.apache.maven.shared.dependency.graph.DependencyGraphBuilderException;
45  import org.apache.maven.shared.dependency.graph.DependencyNode;
46  import org.apache.maven.shared.dependency.graph.filter.AncestorOrSelfDependencyNodeFilter;
47  import org.apache.maven.shared.dependency.graph.filter.AndDependencyNodeFilter;
48  import org.apache.maven.shared.dependency.graph.filter.ArtifactDependencyNodeFilter;
49  import org.apache.maven.shared.dependency.graph.filter.DependencyNodeFilter;
50  import org.apache.maven.shared.dependency.graph.traversal.CollectingDependencyNodeVisitor;
51  import org.apache.maven.shared.dependency.graph.traversal.DependencyNodeVisitor;
52  import org.apache.maven.shared.dependency.graph.traversal.FilteringDependencyNodeVisitor;
53  import org.apache.maven.shared.dependency.graph.traversal.SerializingDependencyNodeVisitor;
54  import org.apache.maven.shared.dependency.graph.traversal.SerializingDependencyNodeVisitor.GraphTokens;
55  import org.eclipse.aether.RepositorySystem;
56  import org.eclipse.aether.RepositorySystemSession;
57  import org.eclipse.aether.repository.RemoteRepository;
58  
59  import java.io.File;
60  import java.io.IOException;
61  import java.io.StringWriter;
62  import java.io.Writer;
63  import java.util.ArrayList;
64  import java.util.Arrays;
65  import java.util.List;
66  import java.util.Objects;
67  
68  /**
69   * Displays the dependency tree for this project. Multiple formats are supported: text (by default), but also
70   * <a href="https://en.wikipedia.org/wiki/DOT_language">DOT</a>,
71   * <a href="https://en.wikipedia.org/wiki/GraphML">GraphML</a>, and
72   * <a href="https://en.wikipedia.org/wiki/Trivial_Graph_Format">TGF</a>.
73   *
74   * @author <a href="mailto:markhobson@gmail.com">Mark Hobson</a>
75   * @since 2.0-alpha-5
76   */
77  @Mojo( name = "tree", requiresDependencyCollection = ResolutionScope.TEST, threadSafe = true )
78  public class TreeMojo
79      extends AbstractMojo
80  {
81      // fields -----------------------------------------------------------------
82  
83      /**
84       * The Maven project.
85       */
86      @Parameter( defaultValue = "${project}", readonly = true, required = true )
87      private MavenProject project;
88  
89      @Parameter( defaultValue = "${session}", readonly = true, required = true )
90      private MavenSession session;
91      
92      @Parameter( property = "outputEncoding", defaultValue = "${project.reporting.outputEncoding}" )
93      private String outputEncoding;
94  
95      /**
96       * Contains the full list of projects in the reactor.
97       */
98      @Parameter( defaultValue = "${reactorProjects}", readonly = true, required = true )
99      private List<MavenProject> reactorProjects;
100 
101     @Component
102     private RepositorySystem repositorySystem;
103 
104     @Parameter ( defaultValue = "${repositorySystem}" )
105     RepositorySystem repositorySystemParam;
106 
107     /**
108      * The current repository/network configuration of Maven.
109      */
110     @Parameter( defaultValue = "${repositorySystemSession}" )
111     private RepositorySystemSession repoSession;
112 
113     /**
114      * The project's remote repositories to use for the resolution of project dependencies.
115      */
116     @Parameter( defaultValue = "${project.remoteProjectRepositories}" )
117     private List<RemoteRepository> projectRepos;
118 
119     /**
120      * The dependency collector builder to use.
121      */
122     @Component( hint = "default" )
123     private DependencyCollectorBuilder dependencyCollectorBuilder;
124 
125     /**
126      * The dependency graph builder to use.
127      */
128     @Component( hint = "default" )
129     private DependencyGraphBuilder dependencyGraphBuilder;
130 
131     /**
132      * If specified, this parameter will cause the dependency tree to be written to the path specified, instead of
133      * writing to the console.
134      *
135      * @since 2.0-alpha-5
136      */
137     @Parameter( property = "outputFile" )
138     private File outputFile;
139 
140     /**
141      * If specified, this parameter will cause the dependency tree to be written using the specified format. Currently
142      * supported format are: <code>text</code> (default), <code>dot</code>, <code>graphml</code> and <code>tgf</code>.
143      * These additional formats can be plotted to image files.
144      *
145      * @since 2.2
146      */
147     @Parameter( property = "outputType", defaultValue = "text" )
148     private String outputType;
149 
150     /**
151      * The scope to filter by when resolving the dependency tree, or <code>null</code> to include dependencies from all
152      * scopes.
153      *
154      * @since 2.0-alpha-5
155      */
156     @Parameter( property = "scope" )
157     private String scope;
158 
159     /**
160      * Whether to include omitted nodes in the serialized dependency tree.
161      *
162      * @since 2.0-alpha-6
163      */
164     @Parameter( property = "verbose", defaultValue = "false" )
165     private boolean verbose;
166 
167     /**
168      * The token set name to use when outputting the dependency tree. Possible values are <code>whitespace</code>,
169      * <code>standard</code> or <code>extended</code>, which use whitespace, standard (ie ASCII) or extended character
170      * sets respectively.
171      *
172      * @since 2.0-alpha-6
173      */
174     @Parameter( property = "tokens", defaultValue = "standard" )
175     private String tokens;
176 
177     /**
178      * A comma-separated list of artifacts to filter the serialized dependency tree by, or <code>null</code> not to
179      * filter the dependency tree. The filter syntax is:
180      * 
181      * <pre>
182      * [groupId]:[artifactId]:[type]:[version]
183      * </pre>
184      * 
185      * where each pattern segment is optional and supports full and partial <code>*</code> wildcards. An empty pattern
186      * segment is treated as an implicit wildcard.
187      * <p>
188      * For example, <code>org.apache.*</code> will match all artifacts whose group id starts with
189      * <code>org.apache.</code>, and <code>:::*-SNAPSHOT</code> will match all snapshot artifacts.
190      * </p>
191      * 
192      * @see StrictPatternIncludesArtifactFilter
193      * @since 2.0-alpha-6
194      */
195     @Parameter( property = "includes" )
196     private String includes;
197 
198     /**
199      * A comma-separated list of artifacts to filter from the serialized dependency tree, or <code>null</code> not to
200      * filter any artifacts from the dependency tree. The filter syntax is:
201      * 
202      * <pre>
203      * [groupId]:[artifactId]:[type]:[version]
204      * </pre>
205      * 
206      * where each pattern segment is optional and supports full and partial <code>*</code> wildcards. An empty pattern
207      * segment is treated as an implicit wildcard.
208      * <p>
209      * For example, <code>org.apache.*</code> will match all artifacts whose group id starts with
210      * <code>org.apache.</code>, and <code>:::*-SNAPSHOT</code> will match all snapshot artifacts.
211      * </p>
212      *
213      * @see StrictPatternExcludesArtifactFilter
214      * @since 2.0-alpha-6
215      */
216     @Parameter( property = "excludes" )
217     private String excludes;
218 
219     /**
220      * The computed dependency tree root node of the Maven project.
221      */
222     private DependencyNode rootNode;
223 
224     /**
225      * Whether to append outputs into the output file or overwrite it.
226      *
227      * @since 2.2
228      */
229     @Parameter( property = "appendOutput", defaultValue = "false" )
230     private boolean appendOutput;
231 
232     /**
233      * Skip plugin execution completely.
234      *
235      * @since 2.7
236      */
237     @Parameter( property = "skip", defaultValue = "false" )
238     private boolean skip;
239     // Mojo methods -----------------------------------------------------------
240 
241     /*
242      * @see org.apache.maven.plugin.Mojo#execute()
243      */
244     @Override
245     public void execute()
246         throws MojoExecutionException, MojoFailureException
247     {
248         if ( isSkip() )
249         {
250             getLog().info( "Skipping plugin execution" );
251             return;
252         }
253 
254         try
255         {
256             String dependencyTreeString;
257 
258             // TODO: note that filter does not get applied due to MSHARED-4
259             ArtifactFilter artifactFilter = createResolvingArtifactFilter();
260 
261             ProjectBuildingRequest buildingRequest =
262                     new DefaultProjectBuildingRequest( session.getProjectBuildingRequest() );
263 
264             buildingRequest.setProject( project );
265 
266             if ( verbose )
267             {
268                 rootNode = dependencyCollectorBuilder.collectDependencyGraph( buildingRequest, artifactFilter );
269                 dependencyTreeString = serializeDependencyTree( rootNode );
270             }
271             else
272             {
273                 // non-verbose mode use dependency graph component, which gives consistent results with Maven version
274                 // running
275                 rootNode = dependencyGraphBuilder.buildDependencyGraph( buildingRequest, artifactFilter );
276 
277                 dependencyTreeString = serializeDependencyTree( rootNode );
278             }
279 
280             if ( outputFile != null )
281             {
282                 String encoding = Objects.toString( outputEncoding, "UTF-8" );
283                 DependencyUtil.write( dependencyTreeString, outputFile, this.appendOutput, encoding );
284 
285                 getLog().info( "Wrote dependency tree to: " + outputFile );
286             }
287             else
288             {
289                 DependencyUtil.log( dependencyTreeString, getLog() );
290             }
291         }
292         catch ( DependencyGraphBuilderException | DependencyCollectorBuilderException exception )
293         {
294             throw new MojoExecutionException( "Cannot build project dependency graph", exception );
295         }
296         catch ( IOException exception )
297         {
298             throw new MojoExecutionException( "Cannot serialize project dependency graph", exception );
299         }
300     }
301 
302     // public methods ---------------------------------------------------------
303 
304     /**
305      * Gets the Maven project used by this mojo.
306      *
307      * @return the Maven project
308      */
309     public MavenProject getProject()
310     {
311         return project;
312     }
313 
314     /**
315      * Gets the computed dependency graph root node for the Maven project.
316      *
317      * @return the dependency tree root node
318      */
319     public DependencyNode getDependencyGraph()
320     {
321         return rootNode;
322     }
323 
324     /**
325      * @return {@link #skip}
326      */
327     public boolean isSkip()
328     {
329         return skip;
330     }
331 
332     /**
333      * @param skip {@link #skip}
334      */
335     public void setSkip( boolean skip )
336     {
337         this.skip = skip;
338     }
339 
340     // private methods --------------------------------------------------------
341 
342     /**
343      * Gets the artifact filter to use when resolving the dependency tree.
344      *
345      * @return the artifact filter
346      */
347     private ArtifactFilter createResolvingArtifactFilter()
348     {
349         ArtifactFilter filter;
350 
351         // filter scope
352         if ( scope != null )
353         {
354             getLog().debug( "+ Resolving dependency tree for scope '" + scope + "'" );
355 
356             filter = new ScopeArtifactFilter( scope );
357         }
358         else
359         {
360             filter = null;
361         }
362 
363         return filter;
364     }
365 
366     /**
367      * Serializes the specified dependency tree to a string.
368      *
369      * @param theRootNode the dependency tree root node to serialize
370      * @return the serialized dependency tree
371      */
372     private String serializeDependencyTree( DependencyNode theRootNode )
373     {
374         StringWriter writer = new StringWriter();
375 
376         DependencyNodeVisitor visitor = getSerializingDependencyNodeVisitor( writer );
377 
378         // TODO: remove the need for this when the serializer can calculate last nodes from visitor calls only
379         visitor = new BuildingDependencyNodeVisitor( visitor );
380 
381         DependencyNodeFilter filter = createDependencyNodeFilter();
382 
383         if ( filter != null )
384         {
385             CollectingDependencyNodeVisitor collectingVisitor = new CollectingDependencyNodeVisitor();
386             DependencyNodeVisitor firstPassVisitor = new FilteringDependencyNodeVisitor( collectingVisitor, filter );
387             theRootNode.accept( firstPassVisitor );
388 
389             DependencyNodeFilter secondPassFilter =
390                 new AncestorOrSelfDependencyNodeFilter( collectingVisitor.getNodes() );
391             visitor = new FilteringDependencyNodeVisitor( visitor, secondPassFilter );
392         }
393 
394         theRootNode.accept( visitor );
395 
396         return writer.toString();
397     }
398 
399     /**
400      * @param writer {@link Writer}
401      * @return {@link DependencyNodeVisitor}
402      */
403     public DependencyNodeVisitor getSerializingDependencyNodeVisitor( Writer writer )
404     {
405         if ( "graphml".equals( outputType ) )
406         {
407             return new GraphmlDependencyNodeVisitor( writer );
408         }
409         else if ( "tgf".equals( outputType ) )
410         {
411             return new TGFDependencyNodeVisitor( writer );
412         }
413         else if ( "dot".equals( outputType ) )
414         {
415             return new DOTDependencyNodeVisitor( writer );
416         }
417         else
418         {
419             return new SerializingDependencyNodeVisitor( writer, toGraphTokens( tokens ) );
420         }
421     }
422 
423     /**
424      * Gets the graph tokens instance for the specified name.
425      *
426      * @param theTokens the graph tokens name
427      * @return the <code>GraphTokens</code> instance
428      */
429     private GraphTokens toGraphTokens( String theTokens )
430     {
431         GraphTokens graphTokens;
432 
433         if ( "whitespace".equals( theTokens ) )
434         {
435             getLog().debug( "+ Using whitespace tree tokens" );
436 
437             graphTokens = SerializingDependencyNodeVisitor.WHITESPACE_TOKENS;
438         }
439         else if ( "extended".equals( theTokens ) )
440         {
441             getLog().debug( "+ Using extended tree tokens" );
442 
443             graphTokens = SerializingDependencyNodeVisitor.EXTENDED_TOKENS;
444         }
445         else
446         {
447             graphTokens = SerializingDependencyNodeVisitor.STANDARD_TOKENS;
448         }
449 
450         return graphTokens;
451     }
452 
453     /**
454      * Gets the dependency node filter to use when serializing the dependency graph.
455      *
456      * @return the dependency node filter, or <code>null</code> if none required
457      */
458     private DependencyNodeFilter createDependencyNodeFilter()
459     {
460         List<DependencyNodeFilter> filters = new ArrayList<>();
461 
462         // filter includes
463         if ( includes != null )
464         {
465             List<String> patterns = Arrays.asList( includes.split( "," ) );
466 
467             getLog().debug( "+ Filtering dependency tree by artifact include patterns: " + patterns );
468 
469             ArtifactFilter artifactFilter = new StrictPatternIncludesArtifactFilter( patterns );
470             filters.add( new ArtifactDependencyNodeFilter( artifactFilter ) );
471         }
472 
473         // filter excludes
474         if ( excludes != null )
475         {
476             List<String> patterns = Arrays.asList( excludes.split( "," ) );
477 
478             getLog().debug( "+ Filtering dependency tree by artifact exclude patterns: " + patterns );
479 
480             ArtifactFilter artifactFilter = new StrictPatternExcludesArtifactFilter( patterns );
481             filters.add( new ArtifactDependencyNodeFilter( artifactFilter ) );
482         }
483 
484         return filters.isEmpty() ? null : new AndDependencyNodeFilter( filters );
485     }
486 
487     // following is required because the version handling in maven code
488     // doesn't work properly. I ripped it out of the enforcer rules.
489 
490     /**
491      * Copied from Artifact.VersionRange. This is tweaked to handle singular ranges properly. Currently the default
492      * containsVersion method assumes a singular version means allow everything. This method assumes that "2.0.4" ==
493      * "[2.0.4,)"
494      *
495      * @param allowedRange range of allowed versions.
496      * @param theVersion the version to be checked.
497      * @return true if the version is contained by the range.
498      * @deprecated This method is unused in this project and will be removed in the future.
499      */
500     @Deprecated
501     public static boolean containsVersion( VersionRange allowedRange, ArtifactVersion theVersion )
502     {
503         ArtifactVersion recommendedVersion = allowedRange.getRecommendedVersion();
504         if ( recommendedVersion == null )
505         {
506             List<Restriction> restrictions = allowedRange.getRestrictions();
507             for ( Restriction restriction : restrictions )
508             {
509                 if ( restriction.containsVersion( theVersion ) )
510                 {
511                     return true;
512                 }
513             }
514             return false;
515         }
516         else
517         {
518             // only singular versions ever have a recommendedVersion
519             return recommendedVersion.compareTo( theVersion ) <= 0;
520         }
521     }
522 }