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