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