View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * Displays all ancestor POMs of the project. This may be useful in a continuous integration system where you want to
35   * know all parent poms of the project.
36   *
37   * @author Mirko Friedenhagen
38   * @since 2.9
39   */
40  @Mojo(name = "display-ancestors", threadSafe = true, requiresProject = true, defaultPhase = LifecyclePhase.VALIDATE)
41  public class DisplayAncestorsMojo extends AbstractMojo {
42  
43      /**
44       * POM
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  }