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