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