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 javax.inject.Inject;
22  import javax.lang.model.SourceVersion;
23  
24  import java.io.File;
25  import java.util.Arrays;
26  import java.util.stream.Collectors;
27  
28  import org.apache.maven.plugin.MojoExecutionException;
29  import org.apache.maven.plugins.annotations.LifecyclePhase;
30  import org.apache.maven.plugins.annotations.Mojo;
31  import org.apache.maven.plugins.annotations.Parameter;
32  import org.apache.maven.plugins.annotations.ResolutionScope;
33  import org.apache.maven.project.MavenProject;
34  import org.apache.maven.tools.plugin.generator.GeneratorException;
35  import org.apache.maven.tools.plugin.generator.PluginHelpGenerator;
36  import org.codehaus.plexus.util.StringUtils;
37  import org.codehaus.plexus.velocity.VelocityComponent;
38  
39  /**
40   * Generates a <code>HelpMojo</code> class.
41   * Relies at runtime on one output file from {@link DescriptorGeneratorMojo}.
42   *
43   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
44   * @since 2.4
45   */
46  @Mojo(
47          name = "helpmojo",
48          defaultPhase = LifecyclePhase.GENERATE_SOURCES,
49          threadSafe = true,
50          requiresDependencyResolution = ResolutionScope.COMPILE)
51  public class HelpGeneratorMojo extends AbstractGeneratorMojo {
52      /**
53       * The directory where the generated <code>HelpMojo</code> file will be put.
54       */
55      @Parameter(defaultValue = "${project.build.directory}/generated-sources/plugin")
56      protected File outputDirectory;
57  
58      /**
59       * The name of the package for the generated <code>HelpMojo</code>.
60       * <p>
61       * By default, the package name will be calculated as <code>groupId + "." + artifactId</code> with additional
62       * <ul>
63       * <li><code>-</code> (dashes) will be replaced by <code>_</code> (underscores)</li>
64       * <li><code>_</code> (underscore) will be added before each number or Java keyword at the beginning of name</li>
65       * </ul>
66       *
67       * @since 2.6
68       */
69      @Parameter
70      private String helpPackageName;
71  
72      /**
73       * Velocity component.
74       */
75      private final VelocityComponent velocity;
76  
77      @Inject
78      public HelpGeneratorMojo(MavenProject project, VelocityComponent velocity) {
79          super(project);
80          this.velocity = velocity;
81      }
82  
83      String getHelpPackageName() {
84          String packageName = null;
85          if (StringUtils.isNotBlank(helpPackageName)) {
86              packageName = helpPackageName;
87          }
88  
89          if (packageName == null) {
90              packageName = project.getGroupId() + "." + project.getArtifactId();
91              packageName = packageName.replace("-", "_");
92  
93              String[] packageItems = packageName.split("\\.");
94              packageName =
95                      Arrays.stream(packageItems).map(this::prefixSpecialCase).collect(Collectors.joining("."));
96          }
97  
98          return packageName;
99      }
100 
101     private String prefixSpecialCase(String name) {
102         if (SourceVersion.isKeyword(name) || !Character.isJavaIdentifierStart(name.charAt(0))) {
103             name = "_" + name;
104         }
105         return name;
106     }
107 
108     @Override
109     protected void generate() throws MojoExecutionException {
110         PluginHelpGenerator pluginHelpGenerator = new PluginHelpGenerator()
111                 .setMavenProject(project)
112                 .setHelpPackageName(getHelpPackageName())
113                 .setGoalPrefix(goalPrefix)
114                 .setVelocityComponent(velocity);
115 
116         try {
117             pluginHelpGenerator.execute(outputDirectory);
118         } catch (GeneratorException e) {
119             throw new MojoExecutionException(e.getMessage(), e);
120         }
121 
122         if (!project.getCompileSourceRoots().contains(outputDirectory.getAbsolutePath())) {
123             project.addCompileSourceRoot(outputDirectory.getAbsolutePath());
124         }
125     }
126 }