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