View Javadoc
1   package org.apache.maven.plugins.dependency;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.commons.lang3.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   * Displays all ancestor POMs of the project. This may be useful in a continuous integration system where you want to
37   * know all parent poms of the project.
38   *
39   * @author Mirko Friedenhagen
40   * @since 2.9
41   */
42  @Mojo( name = "display-ancestors", threadSafe = true, requiresProject = true, defaultPhase = LifecyclePhase.VALIDATE )
43  public class DisplayAncestorsMojo
44      extends AbstractMojo
45  {
46  
47      /**
48       * POM
49       */
50      @Parameter( defaultValue = "${project}", readonly = true )
51      private MavenProject project;
52  
53      @Override
54      public void execute()
55          throws MojoExecutionException, MojoFailureException
56      {
57          final List<String> ancestors = collectAncestors();
58  
59          if ( ancestors.isEmpty() )
60          {
61              getLog().info( "No Ancestor POMs!" );
62          }
63          else
64          {
65              getLog().info( String.format( Locale.US, "Ancestor POMs: %s", StringUtils.join( ancestors, " <- " ) ) );
66          }
67  
68      }
69  
70      private ArrayList<String> collectAncestors()
71      {
72          final ArrayList<String> ancestors = new ArrayList<>();
73  
74          MavenProject currentAncestor = project.getParent();
75          while ( currentAncestor != null )
76          {
77              final String gav = String.format( Locale.US, "%s:%s:%s", currentAncestor.getGroupId(),
78                                                currentAncestor.getArtifactId(), currentAncestor.getVersion() );
79  
80              ancestors.add( gav );
81  
82              currentAncestor = currentAncestor.getParent();
83          }
84  
85          return ancestors;
86      }
87  
88  }