View Javadoc
1   package org.apache.maven.plugin.plugin;
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 java.util.Collections;
23  import java.util.List;
24  
25  import org.apache.maven.plugin.AbstractMojo;
26  import org.apache.maven.plugin.MojoExecutionException;
27  import org.apache.maven.plugin.descriptor.PluginDescriptor;
28  import org.apache.maven.plugins.annotations.Parameter;
29  import org.apache.maven.project.MavenProject;
30  
31  /**
32   * Abstract class for this Plugin.
33   *
34   * @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
35   *
36   */
37  public abstract class AbstractGeneratorMojo
38      extends AbstractMojo
39  {
40      /**
41       * The project currently being built.
42       */
43      @Parameter( defaultValue = "${project}", readonly = true )
44      protected MavenProject project;
45  
46      /**
47       * The goal prefix that will appear before the ":".
48       */
49      @Parameter
50      protected String goalPrefix;
51  
52      /**
53       * Set this to "true" to skip invoking any goals or reports of the plugin.
54       *
55       * @since 2.8
56       */
57      @Parameter( defaultValue = "false", property = "maven.plugin.skip" )
58      private boolean skip;
59  
60      /**
61       * Maven plugin packaging types. Default is single "maven-plugin".
62       * 
63       * @since 3.3
64       */
65      @Parameter
66      private List<String> packagingTypes = Collections.singletonList( "maven-plugin" );
67  
68      /**
69       * System/OS line separator: used to format console messages.
70       */
71      protected static final String LS = System.lineSeparator();
72  
73      protected abstract void generate() throws MojoExecutionException;
74  
75      @Override
76      public void execute()
77          throws MojoExecutionException
78      {
79          if ( !packagingTypes.contains( project.getPackaging() ) )
80          {
81              getLog().info( "Unsupported packaging type " + project.getPackaging() + ", execution skipped" );
82              return;
83          }
84  
85          if ( skip )
86          {
87              getLog().warn( "Execution skipped" );
88              return;
89          }
90  
91          String defaultGoalPrefix = getDefaultGoalPrefix( project );
92  
93          if ( goalPrefix == null )
94          {
95              goalPrefix = defaultGoalPrefix;
96          }
97          else if ( !goalPrefix.equals( defaultGoalPrefix ) )
98          {
99              getLog().warn(
100                 LS + LS + "Goal prefix is specified as: '" + goalPrefix + "'. " + "Maven currently expects it to be '"
101                     + defaultGoalPrefix + "'." + LS );
102         }
103 
104         generate();
105     }
106 
107     static String getDefaultGoalPrefix( MavenProject project )
108     {
109         String defaultGoalPrefix;
110         if ( "maven-plugin-report-plugin".equalsIgnoreCase( project.getArtifactId() ) )
111         {
112             defaultGoalPrefix = "plugin-report";
113         }
114         else if ( "maven-plugin".equalsIgnoreCase( project.getArtifactId() ) )
115         {
116             defaultGoalPrefix = project.getGroupId().substring( project.getGroupId().lastIndexOf( '.' ) + 1 );
117         }
118         else
119         {
120             defaultGoalPrefix = PluginDescriptor.getGoalPrefixFromArtifactId( project.getArtifactId() );
121         }
122         return defaultGoalPrefix;
123     }
124 }