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
24 import org.apache.maven.plugin.AbstractMojo;
25 import org.apache.maven.plugin.MojoExecutionException;
26 import org.apache.maven.plugin.MojoFailureException;
27 import org.apache.maven.plugins.annotations.Component;
28 import org.apache.maven.plugins.annotations.LifecyclePhase;
29 import org.apache.maven.plugins.annotations.Mojo;
30 import org.apache.maven.project.MavenProject;
31
32
33
34
35
36
37
38
39 @Mojo(name = "display-ancestors", threadSafe = true, requiresProject = true, defaultPhase = LifecyclePhase.VALIDATE)
40 public class DisplayAncestorsMojo extends AbstractMojo {
41
42
43
44
45 @Component
46 private MavenProject project;
47
48 @Override
49 public void execute() throws MojoExecutionException, MojoFailureException {
50 final List<String> ancestors = collectAncestors();
51
52 if (ancestors.isEmpty()) {
53 getLog().info("No Ancestor POMs!");
54 } else {
55 getLog().info("Ancestor POMs: " + String.join(" <- ", ancestors));
56 }
57 }
58
59 private ArrayList<String> collectAncestors() {
60 final ArrayList<String> ancestors = new ArrayList<>();
61
62 MavenProject currentAncestor = project.getParent();
63 while (currentAncestor != null) {
64 ancestors.add(currentAncestor.getGroupId() + ":" + currentAncestor.getArtifactId() + ":"
65 + currentAncestor.getVersion());
66
67 currentAncestor = currentAncestor.getParent();
68 }
69
70 return ancestors;
71 }
72 }