View Javadoc
1   package org.apache.maven.tools.plugin.generator;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.io.InputStreamReader;
26  import java.io.OutputStreamWriter;
27  import java.io.StringWriter;
28  import java.io.Writer;
29  
30  import org.apache.maven.project.MavenProject;
31  import org.apache.velocity.VelocityContext;
32  import org.codehaus.plexus.logging.AbstractLogEnabled;
33  import org.codehaus.plexus.logging.Logger;
34  import org.codehaus.plexus.logging.console.ConsoleLogger;
35  import org.codehaus.plexus.util.StringUtils;
36  import org.codehaus.plexus.util.io.CachingOutputStream;
37  import org.codehaus.plexus.velocity.VelocityComponent;
38  
39  import static java.nio.charset.StandardCharsets.UTF_8;
40  
41  /**
42   * Generates an <code>HelpMojo</code> class from <code>help-class-source.vm</code> template.
43   * The generated mojo reads help content from <code>META-INF/maven/${groupId}/${artifactId}/plugin-help.xml</code>
44   * resource, which is generated by this {@link PluginDescriptorFilesGenerator}.
45   *
46   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
47   * @since 2.4
48   */
49  public class PluginHelpGenerator
50      extends AbstractLogEnabled
51  {
52      /**
53       * Default generated class name
54       */
55      private static final String HELP_MOJO_CLASS_NAME = "HelpMojo";
56  
57      private String helpPackageName;
58      private String goalPrefix;
59      private MavenProject mavenProject;
60      private VelocityComponent velocityComponent;
61  
62      /**
63       * Default constructor
64       */
65      public PluginHelpGenerator()
66      {
67          this.enableLogging( new ConsoleLogger( Logger.LEVEL_INFO, "PluginHelpGenerator" ) );
68      }
69  
70      // ----------------------------------------------------------------------
71      // Public methods
72      // ----------------------------------------------------------------------
73  
74      public void execute( File destinationDirectory )
75          throws GeneratorException
76      {
77          String helpImplementation = getImplementation();
78  
79          try
80          {
81              String sourcePath = helpImplementation.replace( '.', File.separatorChar ) + ".java";
82  
83              File helpClass = new File( destinationDirectory, sourcePath );
84              helpClass.getParentFile().mkdirs();
85  
86              String helpClassSources =
87                  getHelpClassSources( getPluginHelpPath( mavenProject ) );
88  
89              try ( Writer w = new OutputStreamWriter( new CachingOutputStream( helpClass ), UTF_8 ) )
90              {
91                  w.write( helpClassSources );
92              }
93          }
94          catch ( IOException e )
95          {
96              throw new GeneratorException( e.getMessage(), e );
97          }
98      }
99  
100     public PluginHelpGenerator setHelpPackageName( String helpPackageName )
101     {
102         this.helpPackageName = helpPackageName;
103         return this;
104     }
105 
106     public PluginHelpGenerator setVelocityComponent( VelocityComponent velocityComponent )
107     {
108         this.velocityComponent = velocityComponent;
109         return this;
110     }
111 
112     public PluginHelpGenerator setGoalPrefix( String goalPrefix )
113     {
114         this.goalPrefix = goalPrefix;
115         return this;
116     }
117 
118     public PluginHelpGenerator setMavenProject( MavenProject mavenProject )
119     {
120         this.mavenProject = mavenProject;
121         return this;
122     }
123 
124     // ----------------------------------------------------------------------
125     // Private methods
126     // ----------------------------------------------------------------------
127 
128     private String getHelpClassSources( String pluginHelpPath )
129         throws IOException
130     {
131         VelocityContext context = new VelocityContext();
132         boolean useAnnotations = mavenProject.getArtifactMap().containsKey(
133             "org.apache.maven.plugin-tools:maven-plugin-annotations" );
134 
135         context.put( "helpPackageName", helpPackageName );
136         context.put( "pluginHelpPath", pluginHelpPath );
137         context.put( "artifactId", mavenProject.getArtifactId() );
138         // TODO: evaluate prefix from deserialized plugin
139         context.put( "goalPrefix", goalPrefix );
140         context.put( "useAnnotations", useAnnotations );
141 
142         StringWriter stringWriter = new StringWriter();
143 
144         // plugin-tools sources are UTF-8 (and even ASCII in this case))
145         try ( InputStream is = //
146                  Thread.currentThread().getContextClassLoader().getResourceAsStream( "help-class-source.vm" ); //
147              InputStreamReader isReader = new InputStreamReader( is, UTF_8 ) )
148         {
149             //isReader =
150             velocityComponent.getEngine().evaluate( context, stringWriter, "", isReader );
151         }
152         // Apply OS lineSeparator instead of template's lineSeparator to have consistent separators for
153         // all source files.
154         return stringWriter.toString().replaceAll( "(\r\n|\n|\r)", System.lineSeparator() );
155     }
156 
157     /**
158      * @return The implementation.
159      */
160     private String getImplementation( )
161     {
162         return StringUtils.isEmpty( helpPackageName )
163             ? HELP_MOJO_CLASS_NAME
164             : helpPackageName + '.' + HELP_MOJO_CLASS_NAME;
165     }
166 
167     static String getPluginHelpPath( MavenProject mavenProject )
168     {
169         return mavenProject.getGroupId() + "/" + mavenProject.getArtifactId() + "/plugin-help.xml";
170     }
171 }