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.tools.plugin.generator;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.io.InputStreamReader;
25  import java.io.OutputStreamWriter;
26  import java.io.StringWriter;
27  import java.io.Writer;
28  
29  import org.apache.maven.project.MavenProject;
30  import org.apache.velocity.VelocityContext;
31  import org.codehaus.plexus.logging.AbstractLogEnabled;
32  import org.codehaus.plexus.logging.Logger;
33  import org.codehaus.plexus.logging.console.ConsoleLogger;
34  import org.codehaus.plexus.util.StringUtils;
35  import org.codehaus.plexus.util.io.CachingOutputStream;
36  import org.codehaus.plexus.velocity.VelocityComponent;
37  
38  import static java.nio.charset.StandardCharsets.UTF_8;
39  
40  /**
41   * Generates an <code>HelpMojo</code> class from <code>help-class-source.vm</code> template.
42   * The generated mojo reads help content from <code>META-INF/maven/${groupId}/${artifactId}/plugin-help.xml</code>
43   * resource, which is generated by this {@link PluginDescriptorFilesGenerator}.
44   *
45   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
46   * @since 2.4
47   */
48  public class PluginHelpGenerator extends AbstractLogEnabled {
49      /**
50       * Default generated class name
51       */
52      private static final String HELP_MOJO_CLASS_NAME = "HelpMojo";
53  
54      private String helpPackageName;
55      private String goalPrefix;
56      private MavenProject mavenProject;
57      private boolean useMaven4Api;
58      private VelocityComponent velocityComponent;
59  
60      /**
61       * Default constructor
62       */
63      public PluginHelpGenerator() {
64          this.enableLogging(new ConsoleLogger(Logger.LEVEL_INFO, "PluginHelpGenerator"));
65      }
66  
67      // ----------------------------------------------------------------------
68      // Public methods
69      // ----------------------------------------------------------------------
70  
71      public void execute(File destinationDirectory) throws GeneratorException {
72          String helpImplementation = getImplementation();
73  
74          useMaven4Api = mavenProject.getDependencies().stream()
75                  .anyMatch(dep ->
76                          "org.apache.maven".equals(dep.getGroupId()) && "maven-api-core".equals(dep.getArtifactId()));
77  
78          try {
79              String sourcePath = helpImplementation.replace('.', File.separatorChar) + ".java";
80  
81              File helpClass = new File(destinationDirectory, sourcePath);
82              helpClass.getParentFile().mkdirs();
83  
84              String helpClassSources = getHelpClassSources(getPluginHelpPath(mavenProject));
85  
86              try (Writer w = new OutputStreamWriter(new CachingOutputStream(helpClass), UTF_8)) {
87                  w.write(helpClassSources);
88              }
89          } catch (IOException e) {
90              throw new GeneratorException(e.getMessage(), e);
91          }
92      }
93  
94      public PluginHelpGenerator setHelpPackageName(String helpPackageName) {
95          this.helpPackageName = helpPackageName;
96          return this;
97      }
98  
99      public PluginHelpGenerator setVelocityComponent(VelocityComponent velocityComponent) {
100         this.velocityComponent = velocityComponent;
101         return this;
102     }
103 
104     public PluginHelpGenerator setGoalPrefix(String goalPrefix) {
105         this.goalPrefix = goalPrefix;
106         return this;
107     }
108 
109     public PluginHelpGenerator setMavenProject(MavenProject mavenProject) {
110         this.mavenProject = mavenProject;
111         return this;
112     }
113 
114     // ----------------------------------------------------------------------
115     // Private methods
116     // ----------------------------------------------------------------------
117 
118     private String getHelpClassSources(String pluginHelpPath) throws IOException {
119         VelocityContext context = new VelocityContext();
120         boolean useAnnotations =
121                 mavenProject.getArtifactMap().containsKey("org.apache.maven.plugin-tools:maven-plugin-annotations");
122 
123         context.put("helpPackageName", helpPackageName);
124         context.put("pluginHelpPath", pluginHelpPath);
125         context.put("artifactId", mavenProject.getArtifactId());
126         // TODO: evaluate prefix from deserialized plugin
127         context.put("goalPrefix", goalPrefix);
128         context.put("useAnnotations", useAnnotations);
129 
130         StringWriter stringWriter = new StringWriter();
131 
132         // plugin-tools sources are UTF-8 (and even ASCII in this case))
133         try (InputStream is = Thread.currentThread()
134                         .getContextClassLoader()
135                         .getResourceAsStream(useMaven4Api ? "help-class-source-v4.vm" : "help-class-source.vm"); //
136                 InputStreamReader isReader = new InputStreamReader(is, UTF_8)) {
137             // isReader =
138             velocityComponent.getEngine().evaluate(context, stringWriter, "", isReader);
139         }
140         // Apply OS lineSeparator instead of template's lineSeparator to have consistent separators for
141         // all source files.
142         return stringWriter.toString().replaceAll("(\r\n|\n|\r)", System.lineSeparator());
143     }
144 
145     /**
146      * @return The implementation.
147      */
148     private String getImplementation() {
149         return StringUtils.isEmpty(helpPackageName)
150                 ? HELP_MOJO_CLASS_NAME
151                 : helpPackageName + '.' + HELP_MOJO_CLASS_NAME;
152     }
153 
154     static String getPluginHelpPath(MavenProject mavenProject) {
155         return mavenProject.getGroupId() + "/" + mavenProject.getArtifactId() + "/plugin-help.xml";
156     }
157 }