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  
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   * Displays all ancestor POMs of the project. This may be useful in a continuous integration system where you want to
34   * know all parent poms of the project.
35   *
36   * @author Mirko Friedenhagen
37   * @since 2.9
38   */
39  @Mojo(name = "display-ancestors", threadSafe = true, requiresProject = true, defaultPhase = LifecyclePhase.VALIDATE)
40  public class DisplayAncestorsMojo extends AbstractMojo {
41  
42      /**
43       * POM
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  }