1 package org.apache.maven.plugin.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.lang.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 public void execute()
54 throws MojoExecutionException, MojoFailureException
55 {
56 final List<String> ancestors = collectAncestors();
57
58 if ( ancestors.isEmpty() )
59 {
60 getLog().info( "No Ancestor POMs!" );
61 }
62 else
63 {
64 getLog().info( String.format( Locale.US, "Ancestor POMs: %s", StringUtils.join( ancestors, " <- " ) ) );
65 }
66
67 }
68
69 private ArrayList<String> collectAncestors()
70 {
71 final ArrayList<String> ancestors = new ArrayList<String>();
72
73 MavenProject currentAncestor = project.getParent();
74 while ( currentAncestor != null )
75 {
76 final String gav = String.format( Locale.US, "%s:%s:%s",
77 currentAncestor.getGroupId(), currentAncestor.getArtifactId(), currentAncestor.getVersion() );
78
79 ancestors.add( gav );
80
81 currentAncestor = currentAncestor.getParent();
82 }
83
84 return ancestors;
85 }
86
87 }