1 package org.apache.maven.tools.plugin.generator;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
43
44
45
46
47
48
49 public class PluginHelpGenerator
50 extends AbstractLogEnabled
51 {
52
53
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
64
65 public PluginHelpGenerator()
66 {
67 this.enableLogging( new ConsoleLogger( Logger.LEVEL_INFO, "PluginHelpGenerator" ) );
68 }
69
70
71
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
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
139 context.put( "goalPrefix", goalPrefix );
140 context.put( "useAnnotations", useAnnotations );
141
142 StringWriter stringWriter = new StringWriter();
143
144
145 try ( InputStream is =
146 Thread.currentThread().getContextClassLoader().getResourceAsStream( "help-class-source.vm" );
147 InputStreamReader isReader = new InputStreamReader( is, UTF_8 ) )
148 {
149
150 velocityComponent.getEngine().evaluate( context, stringWriter, "", isReader );
151 }
152
153
154 return stringWriter.toString().replaceAll( "(\r\n|\n|\r)", System.lineSeparator() );
155 }
156
157
158
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 }