1 package org.apache.maven.plugin.dependency.tree;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.artifact.repository.ArtifactRepository;
23 import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
24 import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter;
25 import org.apache.maven.artifact.versioning.ArtifactVersion;
26 import org.apache.maven.artifact.versioning.Restriction;
27 import org.apache.maven.artifact.versioning.VersionRange;
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.plugin.dependency.utils.DependencyUtil;
32 import org.apache.maven.plugins.annotations.Component;
33 import org.apache.maven.plugins.annotations.Mojo;
34 import org.apache.maven.plugins.annotations.Parameter;
35 import org.apache.maven.plugins.annotations.ResolutionScope;
36 import org.apache.maven.project.MavenProject;
37 import org.apache.maven.shared.artifact.filter.StrictPatternExcludesArtifactFilter;
38 import org.apache.maven.shared.artifact.filter.StrictPatternIncludesArtifactFilter;
39 import org.apache.maven.shared.dependency.graph.DependencyGraphBuilder;
40 import org.apache.maven.shared.dependency.graph.DependencyGraphBuilderException;
41 import org.apache.maven.shared.dependency.graph.DependencyNode;
42 import org.apache.maven.shared.dependency.graph.filter.AncestorOrSelfDependencyNodeFilter;
43 import org.apache.maven.shared.dependency.graph.filter.AndDependencyNodeFilter;
44 import org.apache.maven.shared.dependency.graph.filter.ArtifactDependencyNodeFilter;
45 import org.apache.maven.shared.dependency.graph.filter.DependencyNodeFilter;
46 import org.apache.maven.shared.dependency.graph.traversal.BuildingDependencyNodeVisitor;
47 import org.apache.maven.shared.dependency.graph.traversal.CollectingDependencyNodeVisitor;
48 import org.apache.maven.shared.dependency.graph.traversal.DependencyNodeVisitor;
49 import org.apache.maven.shared.dependency.graph.traversal.FilteringDependencyNodeVisitor;
50 import org.apache.maven.shared.dependency.graph.traversal.SerializingDependencyNodeVisitor;
51 import org.apache.maven.shared.dependency.graph.traversal.SerializingDependencyNodeVisitor.GraphTokens;
52 import org.apache.maven.shared.dependency.tree.DependencyTreeBuilder;
53 import org.apache.maven.shared.dependency.tree.DependencyTreeBuilderException;
54 import org.apache.maven.shared.dependency.tree.traversal.SerializingDependencyNodeVisitor.TreeTokens;
55
56 import java.io.File;
57 import java.io.IOException;
58 import java.io.StringWriter;
59 import java.io.Writer;
60 import java.util.ArrayList;
61 import java.util.Arrays;
62 import java.util.List;
63
64
65
66
67
68
69
70
71 @Mojo( name = "tree", requiresDependencyResolution = ResolutionScope.TEST, threadSafe = true )
72 public class TreeMojo
73 extends AbstractMojo
74 {
75
76
77
78
79
80 @Parameter( defaultValue = "${project}", readonly = true, required = true )
81 private MavenProject project;
82
83
84
85
86 @Component( hint = "default" )
87 private DependencyGraphBuilder dependencyGraphBuilder;
88
89
90
91
92 @Component
93 private DependencyTreeBuilder dependencyTreeBuilder;
94
95
96
97
98
99
100
101 @Parameter( property = "output" )
102 private File output;
103
104
105
106
107
108
109
110 @Parameter( property = "outputFile" )
111 private File outputFile;
112
113
114
115
116
117
118
119
120
121
122 @Parameter( property = "outputType", defaultValue = "text" )
123 private String outputType;
124
125
126
127
128
129
130
131
132 @Parameter( property = "scope" )
133 private String scope;
134
135
136
137
138
139
140
141
142 @Parameter( property = "verbose", defaultValue = "false" )
143 private boolean verbose;
144
145
146
147
148
149
150
151
152 @Parameter( property = "tokens", defaultValue = "standard" )
153 private String tokens;
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171 @Parameter( property = "includes" )
172 private String includes;
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190 @Parameter( property = "excludes" )
191 private String excludes;
192
193
194
195
196 private DependencyNode rootNode;
197
198
199
200
201
202
203 @Parameter( property = "appendOutput", defaultValue = "false" )
204 private boolean appendOutput;
205
206
207
208
209
210
211 @Parameter( property = "skip", defaultValue = "false" )
212 private boolean skip;
213
214 @Parameter( defaultValue = "${localRepository}", readonly = true )
215 private ArtifactRepository localRepository;
216
217
218
219
220
221
222 public void execute()
223 throws MojoExecutionException, MojoFailureException
224 {
225 if ( isSkip() )
226 {
227 getLog().info( "Skipping plugin execution" );
228 return;
229 }
230
231 if ( output != null )
232 {
233 getLog().warn( "The parameter output is deprecated. Use outputFile instead." );
234 this.outputFile = output;
235 }
236
237 try
238 {
239 String dependencyTreeString;
240
241
242
243 ArtifactFilter artifactFilter = createResolvingArtifactFilter();
244
245 if ( verbose )
246 {
247
248 if ( ! isMaven2x() )
249 {
250 getLog().warn( "Using Maven 2 dependency tree to get verbose output, "
251 + "which may be inconsistent with actual Maven 3 resolution" );
252 }
253 dependencyTreeString =
254 serializeVerboseDependencyTree( dependencyTreeBuilder.buildDependencyTree( project,
255 localRepository,
256 artifactFilter ) );
257 }
258 else
259 {
260
261
262 rootNode = dependencyGraphBuilder.buildDependencyGraph( project, artifactFilter );
263
264 dependencyTreeString = serializeDependencyTree( rootNode );
265 }
266
267 if ( outputFile != null )
268 {
269 DependencyUtil.write( dependencyTreeString, outputFile, this.appendOutput, getLog() );
270
271 getLog().info( "Wrote dependency tree to: " + outputFile );
272 }
273 else
274 {
275 DependencyUtil.log( dependencyTreeString, getLog() );
276 }
277 }
278 catch ( DependencyGraphBuilderException exception )
279 {
280 throw new MojoExecutionException( "Cannot build project dependency graph", exception );
281 }
282 catch ( DependencyTreeBuilderException exception )
283 {
284 throw new MojoExecutionException( "Cannot build project dependency tree", exception );
285 }
286 catch ( IOException exception )
287 {
288 throw new MojoExecutionException( "Cannot serialise project dependency graph", exception );
289 }
290 }
291
292
293
294
295
296
297
298
299 public MavenProject getProject()
300 {
301 return project;
302 }
303
304
305
306
307
308
309 public DependencyNode getDependencyGraph()
310 {
311 return rootNode;
312 }
313
314 public boolean isSkip()
315 {
316 return skip;
317 }
318
319 public void setSkip( boolean skip )
320 {
321 this.skip = skip;
322 }
323
324
325
326
327
328
329
330
331 private ArtifactFilter createResolvingArtifactFilter()
332 {
333 ArtifactFilter filter;
334
335
336 if ( scope != null )
337 {
338 getLog().debug( "+ Resolving dependency tree for scope '" + scope + "'" );
339
340 filter = new ScopeArtifactFilter( scope );
341 }
342 else
343 {
344 filter = null;
345 }
346
347 return filter;
348 }
349
350
351
352
353
354
355
356 private String serializeDependencyTree( DependencyNode rootNode )
357 {
358 StringWriter writer = new StringWriter();
359
360 DependencyNodeVisitor visitor = getSerializingDependencyNodeVisitor( writer );
361
362
363 visitor = new BuildingDependencyNodeVisitor( visitor );
364
365 DependencyNodeFilter filter = createDependencyNodeFilter();
366
367 if ( filter != null )
368 {
369 CollectingDependencyNodeVisitor collectingVisitor = new CollectingDependencyNodeVisitor();
370 DependencyNodeVisitor firstPassVisitor = new FilteringDependencyNodeVisitor( collectingVisitor, filter );
371 rootNode.accept( firstPassVisitor );
372
373 DependencyNodeFilter secondPassFilter =
374 new AncestorOrSelfDependencyNodeFilter( collectingVisitor.getNodes() );
375 visitor = new FilteringDependencyNodeVisitor( visitor, secondPassFilter );
376 }
377
378 rootNode.accept( visitor );
379
380 return writer.toString();
381 }
382
383
384
385
386
387
388
389 private String serializeVerboseDependencyTree( org.apache.maven.shared.dependency.tree.DependencyNode rootNode )
390 {
391 StringWriter writer = new StringWriter();
392
393 org.apache.maven.shared.dependency.tree.traversal.DependencyNodeVisitor visitor =
394 new org.apache.maven.shared.dependency.tree.traversal.SerializingDependencyNodeVisitor( writer,
395 toTreeTokens( tokens ) );
396
397
398 visitor = new org.apache.maven.shared.dependency.tree.traversal.BuildingDependencyNodeVisitor( visitor );
399
400 org.apache.maven.shared.dependency.tree.filter.DependencyNodeFilter filter = createVerboseDependencyNodeFilter();
401
402 if ( filter != null )
403 {
404 org.apache.maven.shared.dependency.tree.traversal.CollectingDependencyNodeVisitor collectingVisitor =
405 new org.apache.maven.shared.dependency.tree.traversal.CollectingDependencyNodeVisitor();
406 org.apache.maven.shared.dependency.tree.traversal.DependencyNodeVisitor firstPassVisitor =
407 new org.apache.maven.shared.dependency.tree.traversal.FilteringDependencyNodeVisitor( collectingVisitor,
408 filter );
409 rootNode.accept( firstPassVisitor );
410
411 org.apache.maven.shared.dependency.tree.filter.DependencyNodeFilter secondPassFilter =
412 new org.apache.maven.shared.dependency.tree.filter.AncestorOrSelfDependencyNodeFilter( collectingVisitor.getNodes() );
413 visitor =
414 new org.apache.maven.shared.dependency.tree.traversal.FilteringDependencyNodeVisitor( visitor,
415 secondPassFilter );
416 }
417
418 rootNode.accept( visitor );
419
420 return writer.toString();
421 }
422
423 public DependencyNodeVisitor getSerializingDependencyNodeVisitor( Writer writer )
424 {
425 if ( "graphml".equals( outputType ) )
426 {
427 return new GraphmlDependencyNodeVisitor( writer );
428 }
429 else if ( "tgf".equals( outputType ) )
430 {
431 return new TGFDependencyNodeVisitor( writer );
432 }
433 else if ( "dot".equals( outputType ) )
434 {
435 return new DOTDependencyNodeVisitor( writer );
436 }
437 else
438 {
439 return new SerializingDependencyNodeVisitor( writer, toGraphTokens( tokens ) );
440 }
441 }
442
443
444
445
446
447
448
449 private GraphTokens toGraphTokens( String tokens )
450 {
451 GraphTokens graphTokens;
452
453 if ( "whitespace".equals( tokens ) )
454 {
455 getLog().debug( "+ Using whitespace tree tokens" );
456
457 graphTokens = SerializingDependencyNodeVisitor.WHITESPACE_TOKENS;
458 }
459 else if ( "extended".equals( tokens ) )
460 {
461 getLog().debug( "+ Using extended tree tokens" );
462
463 graphTokens = SerializingDependencyNodeVisitor.EXTENDED_TOKENS;
464 }
465 else
466 {
467 graphTokens = SerializingDependencyNodeVisitor.STANDARD_TOKENS;
468 }
469
470 return graphTokens;
471 }
472
473
474
475
476
477
478
479 private TreeTokens toTreeTokens( String tokens )
480 {
481 TreeTokens treeTokens;
482
483 if ( "whitespace".equals( tokens ) )
484 {
485 getLog().debug( "+ Using whitespace tree tokens" );
486
487 treeTokens =
488 org.apache.maven.shared.dependency.tree.traversal.SerializingDependencyNodeVisitor.WHITESPACE_TOKENS;
489 }
490 else if ( "extended".equals( tokens ) )
491 {
492 getLog().debug( "+ Using extended tree tokens" );
493
494 treeTokens =
495 org.apache.maven.shared.dependency.tree.traversal.SerializingDependencyNodeVisitor.EXTENDED_TOKENS;
496 }
497 else
498 {
499 treeTokens =
500 org.apache.maven.shared.dependency.tree.traversal.SerializingDependencyNodeVisitor.STANDARD_TOKENS;
501 }
502
503 return treeTokens;
504 }
505
506
507
508
509
510
511 private DependencyNodeFilter createDependencyNodeFilter()
512 {
513 List<DependencyNodeFilter> filters = new ArrayList<DependencyNodeFilter>();
514
515
516 if ( includes != null )
517 {
518 List<String> patterns = Arrays.asList( includes.split( "," ) );
519
520 getLog().debug( "+ Filtering dependency tree by artifact include patterns: " + patterns );
521
522 ArtifactFilter artifactFilter = new StrictPatternIncludesArtifactFilter( patterns );
523 filters.add( new ArtifactDependencyNodeFilter( artifactFilter ) );
524 }
525
526
527 if ( excludes != null )
528 {
529 List<String> patterns = Arrays.asList( excludes.split( "," ) );
530
531 getLog().debug( "+ Filtering dependency tree by artifact exclude patterns: " + patterns );
532
533 ArtifactFilter artifactFilter = new StrictPatternExcludesArtifactFilter( patterns );
534 filters.add( new ArtifactDependencyNodeFilter( artifactFilter ) );
535 }
536
537 return filters.isEmpty() ? null : new AndDependencyNodeFilter( filters );
538 }
539
540
541
542
543
544
545 private org.apache.maven.shared.dependency.tree.filter.DependencyNodeFilter createVerboseDependencyNodeFilter()
546 {
547 List<org.apache.maven.shared.dependency.tree.filter.DependencyNodeFilter> filters =
548 new ArrayList<org.apache.maven.shared.dependency.tree.filter.DependencyNodeFilter>();
549
550
551 if ( includes != null )
552 {
553 List<String> patterns = Arrays.asList( includes.split( "," ) );
554
555 getLog().debug( "+ Filtering dependency tree by artifact include patterns: " + patterns );
556
557 ArtifactFilter artifactFilter = new StrictPatternIncludesArtifactFilter( patterns );
558 filters.add( new org.apache.maven.shared.dependency.tree.filter.ArtifactDependencyNodeFilter(
559 artifactFilter ) );
560 }
561
562
563 if ( excludes != null )
564 {
565 List<String> patterns = Arrays.asList( excludes.split( "," ) );
566
567 getLog().debug( "+ Filtering dependency tree by artifact exclude patterns: " + patterns );
568
569 ArtifactFilter artifactFilter = new StrictPatternExcludesArtifactFilter( patterns );
570 filters.add( new org.apache.maven.shared.dependency.tree.filter.ArtifactDependencyNodeFilter(
571 artifactFilter ) );
572 }
573
574 return filters.isEmpty() ? null
575 : new org.apache.maven.shared.dependency.tree.filter.AndDependencyNodeFilter( filters );
576 }
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591 public static boolean containsVersion( VersionRange allowedRange, ArtifactVersion theVersion )
592 {
593 ArtifactVersion recommendedVersion = allowedRange.getRecommendedVersion();
594 if ( recommendedVersion == null )
595 {
596 @SuppressWarnings( "unchecked" )
597 List<Restriction> restrictions = allowedRange.getRestrictions();
598 for ( Restriction restriction : restrictions )
599 {
600 if ( restriction.containsVersion( theVersion ) )
601 {
602 return true;
603 }
604 }
605 }
606
607
608 return recommendedVersion.compareTo( theVersion ) <= 0;
609 }
610
611
612
613
614 protected static boolean isMaven2x()
615 {
616 return !canFindCoreClass( "org.apache.maven.project.DependencyResolutionRequest" );
617 }
618
619 private static boolean canFindCoreClass( String className)
620 {
621 try
622 {
623 Thread.currentThread().getContextClassLoader().loadClass( className );
624
625 return true;
626 }
627 catch ( ClassNotFoundException e )
628 {
629 return false;
630 }
631 }
632 }