1 package org.apache.maven.plugins.dependency;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.commons.lang3.StringUtils;
23 import org.apache.maven.plugin.AbstractMojo;
24 import org.apache.maven.plugin.MojoExecutionException;
25 import org.apache.maven.plugin.MojoFailureException;
26 import org.apache.maven.plugins.annotations.LifecyclePhase;
27 import org.apache.maven.plugins.annotations.Mojo;
28 import org.apache.maven.plugins.annotations.Parameter;
29 import org.apache.maven.project.MavenProject;
30
31 import java.util.ArrayList;
32 import java.util.List;
33 import java.util.Locale;
34
35
36
37
38
39
40
41
42 @Mojo( name = "display-ancestors", threadSafe = true, requiresProject = true, defaultPhase = LifecyclePhase.VALIDATE )
43 public class DisplayAncestorsMojo
44 extends AbstractMojo
45 {
46
47
48
49
50 @Parameter( defaultValue = "${project}", readonly = true )
51 private MavenProject project;
52
53 @Override
54 public void execute()
55 throws MojoExecutionException, MojoFailureException
56 {
57 final List<String> ancestors = collectAncestors();
58
59 if ( ancestors.isEmpty() )
60 {
61 getLog().info( "No Ancestor POMs!" );
62 }
63 else
64 {
65 getLog().info( String.format( Locale.US, "Ancestor POMs: %s", StringUtils.join( ancestors, " <- " ) ) );
66 }
67
68 }
69
70 private ArrayList<String> collectAncestors()
71 {
72 final ArrayList<String> ancestors = new ArrayList<>();
73
74 MavenProject currentAncestor = project.getParent();
75 while ( currentAncestor != null )
76 {
77 final String gav = String.format( Locale.US, "%s:%s:%s", currentAncestor.getGroupId(),
78 currentAncestor.getArtifactId(), currentAncestor.getVersion() );
79
80 ancestors.add( gav );
81
82 currentAncestor = currentAncestor.getParent();
83 }
84
85 return ancestors;
86 }
87
88 }