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