1 package org.apache.maven.plugins.clean;
2
3 import org.apache.maven.plugin.AbstractMojo;
4 import org.apache.maven.plugin.MojoExecutionException;
5 import org.apache.maven.plugins.annotations.Mojo;
6 import org.apache.maven.plugins.annotations.Parameter;
7
8 import org.w3c.dom.Document;
9 import org.w3c.dom.Element;
10 import org.w3c.dom.Node;
11 import org.w3c.dom.NodeList;
12 import org.xml.sax.SAXException;
13
14 import javax.xml.parsers.DocumentBuilder;
15 import javax.xml.parsers.DocumentBuilderFactory;
16 import javax.xml.parsers.ParserConfigurationException;
17 import java.io.IOException;
18 import java.io.InputStream;
19 import java.util.ArrayList;
20 import java.util.List;
21
22
23
24
25
26
27 @Mojo( name = "help", requiresProject = false, threadSafe = true )
28 public class HelpMojo
29 extends AbstractMojo
30 {
31
32
33
34
35 @Parameter( property = "detail", defaultValue = "false" )
36 private boolean detail;
37
38
39
40
41
42 @Parameter( property = "goal" )
43 private java.lang.String goal;
44
45
46
47
48
49 @Parameter( property = "lineLength", defaultValue = "80" )
50 private int lineLength;
51
52
53
54
55
56 @Parameter( property = "indentSize", defaultValue = "2" )
57 private int indentSize;
58
59
60 private static final String PLUGIN_HELP_PATH =
61 "/META-INF/maven/org.apache.maven.plugins/maven-clean-plugin/plugin-help.xml";
62
63 private static final int DEFAULT_LINE_LENGTH = 80;
64
65 private Document build()
66 throws MojoExecutionException
67 {
68 getLog().debug( "load plugin-help.xml: " + PLUGIN_HELP_PATH );
69 try ( InputStream is = getClass().getResourceAsStream( PLUGIN_HELP_PATH ) )
70 {
71 DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
72 DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
73 return dBuilder.parse( is );
74 }
75 catch ( IOException e )
76 {
77 throw new MojoExecutionException( e.getMessage(), e );
78 }
79 catch ( ParserConfigurationException e )
80 {
81 throw new MojoExecutionException( e.getMessage(), e );
82 }
83 catch ( SAXException e )
84 {
85 throw new MojoExecutionException( e.getMessage(), e );
86 }
87 }
88
89
90
91
92 @Override
93 public void execute()
94 throws MojoExecutionException
95 {
96 if ( lineLength <= 0 )
97 {
98 getLog().warn( "The parameter 'lineLength' should be positive, using '80' as default." );
99 lineLength = DEFAULT_LINE_LENGTH;
100 }
101 if ( indentSize <= 0 )
102 {
103 getLog().warn( "The parameter 'indentSize' should be positive, using '2' as default." );
104 indentSize = 2;
105 }
106
107 Document doc = build();
108
109 StringBuilder sb = new StringBuilder();
110 Node plugin = getSingleChild( doc, "plugin" );
111
112
113 String name = getValue( plugin, "name" );
114 String version = getValue( plugin, "version" );
115 String id = getValue( plugin, "groupId" ) + ":" + getValue( plugin, "artifactId" ) + ":" + version;
116 if ( isNotEmpty( name ) && !name.contains( id ) )
117 {
118 append( sb, name + " " + version, 0 );
119 }
120 else
121 {
122 if ( isNotEmpty( name ) )
123 {
124 append( sb, name, 0 );
125 }
126 else
127 {
128 append( sb, id, 0 );
129 }
130 }
131 append( sb, getValue( plugin, "description" ), 1 );
132 append( sb, "", 0 );
133
134
135 String goalPrefix = getValue( plugin, "goalPrefix" );
136
137 Node mojos1 = getSingleChild( plugin, "mojos" );
138
139 List<Node> mojos = findNamedChild( mojos1, "mojo" );
140
141 if ( goal == null || goal.length() <= 0 )
142 {
143 append( sb, "This plugin has " + mojos.size() + ( mojos.size() > 1 ? " goals:" : " goal:" ), 0 );
144 append( sb, "", 0 );
145 }
146
147 for ( Node mojo : mojos )
148 {
149 writeGoal( sb, goalPrefix, (Element) mojo );
150 }
151
152 if ( getLog().isInfoEnabled() )
153 {
154 getLog().info( sb.toString() );
155 }
156 }
157
158
159 private static boolean isNotEmpty( String string )
160 {
161 return string != null && string.length() > 0;
162 }
163
164 private static String getValue( Node node, String elementName )
165 throws MojoExecutionException
166 {
167 return getSingleChild( node, elementName ).getTextContent();
168 }
169
170 private static Node getSingleChild( Node node, String elementName )
171 throws MojoExecutionException
172 {
173 List<Node> namedChild = findNamedChild( node, elementName );
174 if ( namedChild.isEmpty() )
175 {
176 throw new MojoExecutionException( "Could not find " + elementName + " in plugin-help.xml" );
177 }
178 if ( namedChild.size() > 1 )
179 {
180 throw new MojoExecutionException( "Multiple " + elementName + " in plugin-help.xml" );
181 }
182 return namedChild.get( 0 );
183 }
184
185 private static List<Node> findNamedChild( Node node, String elementName )
186 {
187 List<Node> result = new ArrayList<Node>();
188 NodeList childNodes = node.getChildNodes();
189 for ( int i = 0; i < childNodes.getLength(); i++ )
190 {
191 Node item = childNodes.item( i );
192 if ( elementName.equals( item.getNodeName() ) )
193 {
194 result.add( item );
195 }
196 }
197 return result;
198 }
199
200 private static Node findSingleChild( Node node, String elementName )
201 throws MojoExecutionException
202 {
203 List<Node> elementsByTagName = findNamedChild( node, elementName );
204 if ( elementsByTagName.isEmpty() )
205 {
206 return null;
207 }
208 if ( elementsByTagName.size() > 1 )
209 {
210 throw new MojoExecutionException( "Multiple " + elementName + "in plugin-help.xml" );
211 }
212 return elementsByTagName.get( 0 );
213 }
214
215 private void writeGoal( StringBuilder sb, String goalPrefix, Element mojo )
216 throws MojoExecutionException
217 {
218 String mojoGoal = getValue( mojo, "goal" );
219 Node configurationElement = findSingleChild( mojo, "configuration" );
220 Node description = findSingleChild( mojo, "description" );
221 if ( goal == null || goal.length() <= 0 || mojoGoal.equals( goal ) )
222 {
223 append( sb, goalPrefix + ":" + mojoGoal, 0 );
224 Node deprecated = findSingleChild( mojo, "deprecated" );
225 if ( ( deprecated != null ) && isNotEmpty( deprecated.getTextContent() ) )
226 {
227 append( sb, "Deprecated. " + deprecated.getTextContent(), 1 );
228 if ( detail && description != null )
229 {
230 append( sb, "", 0 );
231 append( sb, description.getTextContent(), 1 );
232 }
233 }
234 else if ( description != null )
235 {
236 append( sb, description.getTextContent(), 1 );
237 }
238 append( sb, "", 0 );
239
240 if ( detail )
241 {
242 Node parametersNode = getSingleChild( mojo, "parameters" );
243 List<Node> parameters = findNamedChild( parametersNode, "parameter" );
244 append( sb, "Available parameters:", 1 );
245 append( sb, "", 0 );
246
247 for ( Node parameter : parameters )
248 {
249 writeParameter( sb, parameter, configurationElement );
250 }
251 }
252 }
253 }
254
255 private void writeParameter( StringBuilder sb, Node parameter, Node configurationElement )
256 throws MojoExecutionException
257 {
258 String parameterName = getValue( parameter, "name" );
259 String parameterDescription = getValue( parameter, "description" );
260
261 Element fieldConfigurationElement = null;
262 if ( configurationElement != null )
263 {
264 fieldConfigurationElement = (Element) findSingleChild( configurationElement, parameterName );
265 }
266
267 String parameterDefaultValue = "";
268 if ( fieldConfigurationElement != null && fieldConfigurationElement.hasAttribute( "default-value" ) )
269 {
270 parameterDefaultValue = " (Default: " + fieldConfigurationElement.getAttribute( "default-value" ) + ")";
271 }
272 append( sb, parameterName + parameterDefaultValue, 2 );
273 Node deprecated = findSingleChild( parameter, "deprecated" );
274 if ( ( deprecated != null ) && isNotEmpty( deprecated.getTextContent() ) )
275 {
276 append( sb, "Deprecated. " + deprecated.getTextContent(), 3 );
277 append( sb, "", 0 );
278 }
279 append( sb, parameterDescription, 3 );
280 if ( "true".equals( getValue( parameter, "required" ) ) )
281 {
282 append( sb, "Required: Yes", 3 );
283 }
284 if ( ( fieldConfigurationElement != null ) && isNotEmpty( fieldConfigurationElement.getTextContent() ) )
285 {
286 String property = getPropertyFromExpression( fieldConfigurationElement.getTextContent() );
287 append( sb, "User property: " + property, 3 );
288 }
289
290 append( sb, "", 0 );
291 }
292
293
294
295
296
297
298
299
300
301
302 private static String repeat( String str, int repeat )
303 {
304 StringBuilder buffer = new StringBuilder( repeat * str.length() );
305
306 for ( int i = 0; i < repeat; i++ )
307 {
308 buffer.append( str );
309 }
310
311 return buffer.toString();
312 }
313
314
315
316
317
318
319
320
321
322 private void append( StringBuilder sb, String description, int indent )
323 {
324 for ( String line : toLines( description, indent, indentSize, lineLength ) )
325 {
326 sb.append( line ).append( '\n' );
327 }
328 }
329
330
331
332
333
334
335
336
337
338
339
340 private static List<String> toLines( String text, int indent, int indentSize, int lineLength )
341 {
342 List<String> lines = new ArrayList<String>();
343
344 String ind = repeat( "\t", indent );
345
346 String[] plainLines = text.split( "(\r\n)|(\r)|(\n)" );
347
348 for ( String plainLine : plainLines )
349 {
350 toLines( lines, ind + plainLine, indentSize, lineLength );
351 }
352
353 return lines;
354 }
355
356
357
358
359
360
361
362
363
364 private static void toLines( List<String> lines, String line, int indentSize, int lineLength )
365 {
366 int lineIndent = getIndentLevel( line );
367 StringBuilder buf = new StringBuilder( 256 );
368
369 String[] tokens = line.split( " +" );
370
371 for ( String token : tokens )
372 {
373 if ( buf.length() > 0 )
374 {
375 if ( buf.length() + token.length() >= lineLength )
376 {
377 lines.add( buf.toString() );
378 buf.setLength( 0 );
379 buf.append( repeat( " ", lineIndent * indentSize ) );
380 }
381 else
382 {
383 buf.append( ' ' );
384 }
385 }
386
387 for ( int j = 0; j < token.length(); j++ )
388 {
389 char c = token.charAt( j );
390 if ( c == '\t' )
391 {
392 buf.append( repeat( " ", indentSize - buf.length() % indentSize ) );
393 }
394 else if ( c == '\u00A0' )
395 {
396 buf.append( ' ' );
397 }
398 else
399 {
400 buf.append( c );
401 }
402 }
403 }
404 lines.add( buf.toString() );
405 }
406
407
408
409
410
411
412
413 private static int getIndentLevel( String line )
414 {
415 int level = 0;
416 for ( int i = 0; i < line.length() && line.charAt( i ) == '\t'; i++ )
417 {
418 level++;
419 }
420 for ( int i = level + 1; i <= level + 4 && i < line.length(); i++ )
421 {
422 if ( line.charAt( i ) == '\t' )
423 {
424 level++;
425 break;
426 }
427 }
428 return level;
429 }
430
431 private static String getPropertyFromExpression( String expression )
432 {
433 if ( expression != null && expression.startsWith( "${" ) && expression.endsWith( "}" )
434 && !expression.substring( 2 ).contains( "${" ) )
435 {
436
437 return expression.substring( 2, expression.length() - 1 );
438 }
439
440 return null;
441 }
442 }