View Javadoc
1   package org.apache.maven.plugin.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.lang.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
37   * where you want to 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      public void execute()
54          throws MojoExecutionException, MojoFailureException
55      {
56          final List<String> ancestors = collectAncestors();
57  
58          if ( ancestors.isEmpty() )
59          {
60              getLog().info( "No Ancestor POMs!" );
61          }
62          else
63          {
64              getLog().info( String.format( Locale.US, "Ancestor POMs: %s", StringUtils.join( ancestors, " <- " ) ) );
65          }
66  
67      }
68  
69      private ArrayList<String> collectAncestors()
70      {
71          final ArrayList<String> ancestors = new ArrayList<String>();
72  
73          MavenProject currentAncestor = project.getParent();
74          while ( currentAncestor != null )
75          {
76              final String gav = String.format( Locale.US, "%s:%s:%s",
77                      currentAncestor.getGroupId(), currentAncestor.getArtifactId(), currentAncestor.getVersion() );
78  
79              ancestors.add( gav );
80  
81              currentAncestor = currentAncestor.getParent();
82          }
83  
84          return ancestors;
85      }
86  
87  }