1 | |
package org.apache.maven.plugins.help; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.io.IOException; |
23 | |
import java.lang.reflect.InvocationTargetException; |
24 | |
import java.lang.reflect.Method; |
25 | |
import java.util.Collections; |
26 | |
import java.util.Comparator; |
27 | |
import java.util.Iterator; |
28 | |
import java.util.List; |
29 | |
import java.util.StringTokenizer; |
30 | |
|
31 | |
import org.apache.maven.artifact.Artifact; |
32 | |
import org.apache.maven.artifact.factory.ArtifactFactory; |
33 | |
import org.apache.maven.artifact.repository.ArtifactRepository; |
34 | |
import org.apache.maven.artifact.resolver.ArtifactNotFoundException; |
35 | |
import org.apache.maven.artifact.resolver.ArtifactResolutionException; |
36 | |
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException; |
37 | |
import org.apache.maven.artifact.versioning.VersionRange; |
38 | |
import org.apache.maven.execution.MavenSession; |
39 | |
import org.apache.maven.lifecycle.DefaultLifecycleExecutor; |
40 | |
import org.apache.maven.lifecycle.Lifecycle; |
41 | |
import org.apache.maven.lifecycle.LifecycleExecutionException; |
42 | |
import org.apache.maven.lifecycle.LifecycleExecutor; |
43 | |
import org.apache.maven.lifecycle.mapping.LifecycleMapping; |
44 | |
import org.apache.maven.model.Plugin; |
45 | |
import org.apache.maven.plugin.InvalidPluginException; |
46 | |
import org.apache.maven.plugin.MojoExecutionException; |
47 | |
import org.apache.maven.plugin.MojoFailureException; |
48 | |
import org.apache.maven.plugin.PluginManager; |
49 | |
import org.apache.maven.plugin.PluginManagerException; |
50 | |
import org.apache.maven.plugin.PluginNotFoundException; |
51 | |
import org.apache.maven.plugin.descriptor.MojoDescriptor; |
52 | |
import org.apache.maven.plugin.descriptor.Parameter; |
53 | |
import org.apache.maven.plugin.descriptor.PluginDescriptor; |
54 | |
import org.apache.maven.plugin.version.PluginVersionNotFoundException; |
55 | |
import org.apache.maven.plugin.version.PluginVersionResolutionException; |
56 | |
import org.apache.maven.project.MavenProject; |
57 | |
import org.apache.maven.project.MavenProjectBuilder; |
58 | |
import org.apache.maven.project.ProjectBuildingException; |
59 | |
import org.apache.maven.settings.Settings; |
60 | |
import org.apache.maven.tools.plugin.util.PluginUtils; |
61 | |
import org.codehaus.plexus.component.repository.exception.ComponentLookupException; |
62 | |
import org.codehaus.plexus.util.StringUtils; |
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | 0 | public class DescribeMojo |
75 | |
extends AbstractHelpMojo |
76 | |
{ |
77 | |
|
78 | |
private static final int INDENT_SIZE = 2; |
79 | |
|
80 | |
|
81 | |
private static final String UNKNOWN = "Unknown"; |
82 | |
|
83 | |
|
84 | |
private static final String NOT_DEFINED = "Not defined"; |
85 | |
|
86 | |
|
87 | |
private static final String NO_REASON = "No reason given"; |
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
private ArtifactFactory artifactFactory; |
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
private PluginManager pluginManager; |
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
private MavenProjectBuilder projectBuilder; |
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
private MavenProject project; |
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
private Settings settings; |
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
private MavenSession session; |
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
private ArtifactRepository localRepository; |
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
private List remoteRepositories; |
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
private String plugin; |
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
private String groupId; |
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
private String artifactId; |
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
private String version; |
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
private String goal; |
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
private boolean detail; |
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | |
private boolean medium; |
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | |
|
242 | |
|
243 | |
|
244 | |
private boolean minimal; |
245 | |
|
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | |
|
251 | |
|
252 | |
|
253 | |
|
254 | |
private String cmd; |
255 | |
|
256 | |
|
257 | |
|
258 | |
|
259 | |
|
260 | |
|
261 | |
public void execute() |
262 | |
throws MojoExecutionException, MojoFailureException |
263 | |
{ |
264 | 0 | validateParameters(); |
265 | |
|
266 | 0 | if ( project == null ) |
267 | |
{ |
268 | |
try |
269 | |
{ |
270 | 0 | project = projectBuilder.buildStandaloneSuperProject( localRepository ); |
271 | |
} |
272 | 0 | catch ( ProjectBuildingException e ) |
273 | |
{ |
274 | 0 | throw new MojoExecutionException( "Error while retrieving the super-project.", e ); |
275 | 0 | } |
276 | |
} |
277 | |
|
278 | 0 | StringBuffer descriptionBuffer = new StringBuffer(); |
279 | |
|
280 | 0 | boolean describePlugin = true; |
281 | 0 | if ( StringUtils.isNotEmpty( cmd ) ) |
282 | |
{ |
283 | 0 | describePlugin = describeCommand( descriptionBuffer ); |
284 | |
} |
285 | |
|
286 | 0 | if ( describePlugin ) |
287 | |
{ |
288 | 0 | PluginInfo pi = new PluginInfo(); |
289 | |
|
290 | 0 | parsePluginLookupInfo( pi ); |
291 | |
|
292 | 0 | PluginDescriptor descriptor = lookupPluginDescriptor( pi ); |
293 | |
|
294 | 0 | if ( StringUtils.isNotEmpty( goal ) ) |
295 | |
{ |
296 | 0 | MojoDescriptor mojo = descriptor.getMojo( goal ); |
297 | 0 | if ( mojo == null ) |
298 | |
{ |
299 | 0 | throw new MojoFailureException( |
300 | |
"The mojo '" + goal + "' does not exist in the plugin '" + pi.getPrefix() + "'" ); |
301 | |
} |
302 | |
|
303 | 0 | describeMojo( mojo, descriptionBuffer ); |
304 | 0 | } |
305 | |
else |
306 | |
{ |
307 | 0 | describePlugin( descriptor, descriptionBuffer ); |
308 | |
} |
309 | |
} |
310 | |
|
311 | 0 | writeDescription( descriptionBuffer ); |
312 | 0 | } |
313 | |
|
314 | |
|
315 | |
|
316 | |
|
317 | |
|
318 | |
|
319 | |
|
320 | |
|
321 | |
private void validateParameters() |
322 | |
{ |
323 | |
|
324 | 0 | if ( goal == null && session.getExecutionProperties().get( "mojo" ) != null ) |
325 | |
{ |
326 | 0 | goal = session.getExecutionProperties().getProperty( "mojo" ); |
327 | |
} |
328 | |
|
329 | 0 | if ( !detail && session.getExecutionProperties().get( "full" ) != null ) |
330 | |
{ |
331 | 0 | String full = session.getExecutionProperties().getProperty( "full" ); |
332 | 0 | detail = new Boolean( full ).booleanValue(); |
333 | |
} |
334 | |
|
335 | 0 | if ( detail || minimal ) |
336 | |
{ |
337 | 0 | medium = false; |
338 | |
} |
339 | 0 | } |
340 | |
|
341 | |
|
342 | |
|
343 | |
|
344 | |
|
345 | |
|
346 | |
|
347 | |
private void writeDescription( StringBuffer descriptionBuffer ) |
348 | |
throws MojoExecutionException |
349 | |
{ |
350 | 0 | if ( output != null ) |
351 | |
{ |
352 | |
try |
353 | |
{ |
354 | 0 | writeFile( output, descriptionBuffer ); |
355 | |
} |
356 | 0 | catch ( IOException e ) |
357 | |
{ |
358 | 0 | throw new MojoExecutionException( "Cannot write plugin/mojo description to output: " + output, e ); |
359 | 0 | } |
360 | |
|
361 | 0 | if ( getLog().isInfoEnabled() ) |
362 | |
{ |
363 | 0 | getLog().info( "Wrote descriptions to: " + output ); |
364 | |
} |
365 | |
} |
366 | |
else |
367 | |
{ |
368 | 0 | if ( getLog().isInfoEnabled() ) |
369 | |
{ |
370 | 0 | getLog().info( descriptionBuffer.toString() ); |
371 | |
} |
372 | |
} |
373 | 0 | } |
374 | |
|
375 | |
|
376 | |
|
377 | |
|
378 | |
|
379 | |
|
380 | |
|
381 | |
|
382 | |
|
383 | |
private PluginDescriptor lookupPluginDescriptor( PluginInfo pi ) |
384 | |
throws MojoExecutionException, MojoFailureException |
385 | |
{ |
386 | 0 | PluginDescriptor descriptor = null; |
387 | |
|
388 | 0 | Plugin forLookup = null; |
389 | |
|
390 | 0 | if ( StringUtils.isNotEmpty( pi.getPrefix() ) ) |
391 | |
{ |
392 | 0 | descriptor = pluginManager.getPluginDescriptorForPrefix( pi.getPrefix() ); |
393 | 0 | if ( descriptor == null ) |
394 | |
{ |
395 | 0 | forLookup = pluginManager.getPluginDefinitionForPrefix( pi.getPrefix(), session, project ); |
396 | |
} |
397 | |
} |
398 | 0 | else if ( StringUtils.isNotEmpty( pi.getGroupId() ) && StringUtils.isNotEmpty( pi.getArtifactId() ) ) |
399 | |
{ |
400 | 0 | forLookup = new Plugin(); |
401 | |
|
402 | 0 | forLookup.setGroupId( pi.getGroupId() ); |
403 | 0 | forLookup.setArtifactId( pi.getArtifactId() ); |
404 | |
|
405 | 0 | if ( StringUtils.isNotEmpty( pi.getVersion() ) ) |
406 | |
{ |
407 | 0 | forLookup.setVersion( pi.getVersion() ); |
408 | |
} |
409 | |
} |
410 | |
else |
411 | |
{ |
412 | 0 | StringBuffer msg = new StringBuffer(); |
413 | 0 | msg.append( "You must specify either: both 'groupId' and 'artifactId' parameters OR a 'plugin' parameter" |
414 | |
+ " OR a 'cmd' parameter. For instance:\n" ); |
415 | 0 | msg.append( " # mvn help:describe -Dcmd=install\n" ); |
416 | 0 | msg.append( "or\n" ); |
417 | 0 | msg.append( " # mvn help:describe -Dcmd=help:describe\n" ); |
418 | 0 | msg.append( "or\n" ); |
419 | 0 | msg.append( " # mvn help:describe -Dplugin=org.apache.maven.plugins:maven-help-plugin\n" ); |
420 | 0 | msg.append( "or\n" ); |
421 | 0 | msg.append( " # mvn help:describe -DgroupId=org.apache.maven.plugins -DartifactId=maven-help-plugin\n\n" ); |
422 | 0 | msg.append( "Try 'mvn help:help -Ddetail=true' for more information." ); |
423 | |
|
424 | 0 | throw new MojoFailureException( msg.toString() ); |
425 | |
} |
426 | |
|
427 | 0 | if ( descriptor == null && forLookup != null ) |
428 | |
{ |
429 | |
try |
430 | |
{ |
431 | 0 | descriptor = pluginManager.verifyPlugin( forLookup, project, settings, localRepository ); |
432 | |
} |
433 | 0 | catch ( ArtifactResolutionException e ) |
434 | |
{ |
435 | 0 | throw new MojoExecutionException( "Error retrieving plugin descriptor for:\n\ngroupId: '" |
436 | |
+ groupId + "'\nartifactId: '" + artifactId + "'\nversion: '" + version + "'\n\n", e ); |
437 | |
} |
438 | 0 | catch ( PluginManagerException e ) |
439 | |
{ |
440 | 0 | throw new MojoExecutionException( "Error retrieving plugin descriptor for:\n\ngroupId: '" |
441 | |
+ groupId + "'\nartifactId: '" + artifactId + "'\nversion: '" + version + "'\n\n", e ); |
442 | |
} |
443 | 0 | catch ( PluginVersionResolutionException e ) |
444 | |
{ |
445 | 0 | throw new MojoExecutionException( "Error retrieving plugin descriptor for:\n\ngroupId: '" |
446 | |
+ groupId + "'\nartifactId: '" + artifactId + "'\nversion: '" + version + "'\n\n", e ); |
447 | |
} |
448 | 0 | catch ( ArtifactNotFoundException e ) |
449 | |
{ |
450 | 0 | throw new MojoExecutionException( "Plugin dependency does not exist: " + e.getMessage(), e ); |
451 | |
} |
452 | 0 | catch ( InvalidVersionSpecificationException e ) |
453 | |
{ |
454 | 0 | throw new MojoExecutionException( "Error retrieving plugin descriptor for:\n\ngroupId: '" |
455 | |
+ groupId + "'\nartifactId: '" + artifactId + "'\nversion: '" + version + "'\n\n", e ); |
456 | |
} |
457 | 0 | catch ( InvalidPluginException e ) |
458 | |
{ |
459 | 0 | throw new MojoExecutionException( "Error retrieving plugin descriptor for:\n\ngroupId: '" |
460 | |
+ groupId + "'\nartifactId: '" + artifactId + "'\nversion: '" + version + "'\n\n", e ); |
461 | |
} |
462 | 0 | catch ( PluginNotFoundException e ) |
463 | |
{ |
464 | 0 | if ( getLog().isDebugEnabled() ) |
465 | |
{ |
466 | 0 | getLog().debug( "Unable to find plugin", e ); |
467 | |
} |
468 | 0 | throw new MojoFailureException( "Plugin does not exist: " + e.getMessage() ); |
469 | |
} |
470 | 0 | catch ( PluginVersionNotFoundException e ) |
471 | |
{ |
472 | 0 | if ( getLog().isDebugEnabled() ) |
473 | |
{ |
474 | 0 | getLog().debug( "Unable to find plugin version", e ); |
475 | |
} |
476 | 0 | throw new MojoFailureException( e.getMessage() ); |
477 | 0 | } |
478 | |
} |
479 | |
|
480 | 0 | if ( descriptor == null ) |
481 | |
{ |
482 | 0 | throw new MojoFailureException( "Plugin could not be found. If you believe it is correct," |
483 | |
+ " check your pluginGroups setting, and run with -U to update the remote configuration" ); |
484 | |
} |
485 | |
|
486 | 0 | return descriptor; |
487 | |
} |
488 | |
|
489 | |
|
490 | |
|
491 | |
|
492 | |
|
493 | |
|
494 | |
|
495 | |
|
496 | |
private void parsePluginLookupInfo( PluginInfo pi ) |
497 | |
throws MojoFailureException |
498 | |
{ |
499 | 0 | if ( StringUtils.isNotEmpty( plugin ) ) |
500 | |
{ |
501 | 0 | if ( plugin.indexOf( ":" ) > -1 ) |
502 | |
{ |
503 | 0 | String[] pluginParts = plugin.split( ":" ); |
504 | |
|
505 | 0 | switch ( pluginParts.length ) |
506 | |
{ |
507 | |
case ( 1 ): |
508 | 0 | pi.setPrefix( pluginParts[0] ); |
509 | 0 | break; |
510 | |
case ( 2 ): |
511 | 0 | pi.setGroupId( pluginParts[0] ); |
512 | 0 | pi.setArtifactId( pluginParts[1] ); |
513 | 0 | break; |
514 | |
case ( 3 ): |
515 | 0 | pi.setGroupId( pluginParts[0] ); |
516 | 0 | pi.setArtifactId( pluginParts[1] ); |
517 | 0 | pi.setVersion( pluginParts[2] ); |
518 | 0 | break; |
519 | |
default: |
520 | 0 | throw new MojoFailureException( "plugin parameter must be a plugin prefix," |
521 | |
+ " or conform to: 'groupId:artifactId[:version]'." ); |
522 | |
} |
523 | 0 | } |
524 | |
else |
525 | |
{ |
526 | 0 | pi.setPrefix( plugin ); |
527 | |
} |
528 | |
} |
529 | |
else |
530 | |
{ |
531 | 0 | pi.setGroupId( groupId ); |
532 | 0 | pi.setArtifactId( artifactId ); |
533 | 0 | pi.setVersion( version ); |
534 | |
} |
535 | 0 | } |
536 | |
|
537 | |
|
538 | |
|
539 | |
|
540 | |
|
541 | |
|
542 | |
|
543 | |
|
544 | |
|
545 | |
private void describePlugin( PluginDescriptor pd, StringBuffer buffer ) |
546 | |
throws MojoFailureException, MojoExecutionException |
547 | |
{ |
548 | 0 | append( buffer, pd.getId(), 0 ); |
549 | 0 | buffer.append( "\n" ); |
550 | |
|
551 | 0 | String name = pd.getName(); |
552 | 0 | if ( name == null ) |
553 | |
{ |
554 | |
|
555 | |
|
556 | |
try |
557 | |
{ |
558 | 0 | Artifact artifact = |
559 | |
artifactFactory.createPluginArtifact( pd.getGroupId(), pd.getArtifactId(), |
560 | |
VersionRange.createFromVersion( pd.getVersion() ) ); |
561 | 0 | MavenProject pluginProject = |
562 | |
projectBuilder.buildFromRepository( artifact, remoteRepositories, localRepository ); |
563 | |
|
564 | 0 | name = pluginProject.getName(); |
565 | |
} |
566 | 0 | catch ( ProjectBuildingException e ) |
567 | |
{ |
568 | |
|
569 | 0 | name = pd.getId(); |
570 | 0 | } |
571 | |
} |
572 | 0 | append( buffer, "Name", name, 0 ); |
573 | 0 | appendAsParagraph( buffer, "Description", toDescription( pd.getDescription() ), 0 ); |
574 | 0 | append( buffer, "Group Id", pd.getGroupId(), 0 ); |
575 | 0 | append( buffer, "Artifact Id", pd.getArtifactId(), 0 ); |
576 | 0 | append( buffer, "Version", pd.getVersion(), 0 ); |
577 | 0 | append( buffer, "Goal Prefix", pd.getGoalPrefix(), 0 ); |
578 | 0 | buffer.append( "\n" ); |
579 | |
|
580 | 0 | List mojos = pd.getMojos(); |
581 | |
|
582 | 0 | if ( mojos == null ) |
583 | |
{ |
584 | 0 | append( buffer, "This plugin has no goals.", 0 ); |
585 | 0 | return; |
586 | |
} |
587 | |
|
588 | 0 | if ( ( detail || medium ) && !minimal ) |
589 | |
{ |
590 | 0 | append( buffer, "This plugin has " + pd.getMojos().size() + " goal" |
591 | |
+ ( pd.getMojos().size() > 1 ? "s" : "" ) + ":", 0 ); |
592 | 0 | buffer.append( "\n" ); |
593 | |
|
594 | 0 | PluginUtils.sortMojos( mojos ); |
595 | |
|
596 | 0 | for ( Iterator it = mojos.iterator(); it.hasNext(); ) |
597 | |
{ |
598 | 0 | MojoDescriptor md = (MojoDescriptor) it.next(); |
599 | |
|
600 | 0 | if ( detail ) |
601 | |
{ |
602 | 0 | describeMojoGuts( md, buffer, true ); |
603 | |
} |
604 | |
else |
605 | |
{ |
606 | 0 | describeMojoGuts( md, buffer, false ); |
607 | |
} |
608 | |
|
609 | 0 | buffer.append( "\n" ); |
610 | 0 | } |
611 | |
} |
612 | |
|
613 | 0 | if ( !detail ) |
614 | |
{ |
615 | 0 | buffer.append( "For more information, run 'mvn help:describe [...] -Ddetail'" ); |
616 | 0 | buffer.append( "\n" ); |
617 | |
} |
618 | 0 | } |
619 | |
|
620 | |
|
621 | |
|
622 | |
|
623 | |
|
624 | |
|
625 | |
|
626 | |
|
627 | |
|
628 | |
private void describeMojo( MojoDescriptor md, StringBuffer buffer ) |
629 | |
throws MojoFailureException, MojoExecutionException |
630 | |
{ |
631 | 0 | buffer.append( "Mojo: '" ).append( md.getFullGoalName() ).append( "'" ); |
632 | 0 | buffer.append( '\n' ); |
633 | |
|
634 | 0 | describeMojoGuts( md, buffer, detail ); |
635 | 0 | buffer.append( "\n" ); |
636 | |
|
637 | 0 | if ( !detail ) |
638 | |
{ |
639 | 0 | buffer.append( "For more information, run 'mvn help:describe [...] -Ddetail'" ); |
640 | 0 | buffer.append( "\n" ); |
641 | |
} |
642 | 0 | } |
643 | |
|
644 | |
|
645 | |
|
646 | |
|
647 | |
|
648 | |
|
649 | |
|
650 | |
|
651 | |
|
652 | |
|
653 | |
private void describeMojoGuts( MojoDescriptor md, StringBuffer buffer, boolean fullDescription ) |
654 | |
throws MojoFailureException, MojoExecutionException |
655 | |
{ |
656 | 0 | append( buffer, md.getFullGoalName(), 0 ); |
657 | |
|
658 | |
|
659 | 0 | appendAsParagraph( buffer, "Description", toDescription( md.getDescription() ), 1 ); |
660 | |
|
661 | 0 | String deprecation = md.getDeprecated(); |
662 | 0 | if ( deprecation != null && deprecation.length() <= 0 ) |
663 | |
{ |
664 | 0 | deprecation = NO_REASON; |
665 | |
} |
666 | |
|
667 | 0 | if ( StringUtils.isNotEmpty( deprecation ) ) |
668 | |
{ |
669 | 0 | append( buffer, "Deprecated. " + deprecation, 1 ); |
670 | |
} |
671 | |
|
672 | 0 | if ( !fullDescription ) |
673 | |
{ |
674 | 0 | return; |
675 | |
} |
676 | |
|
677 | 0 | append( buffer, "Implementation", md.getImplementation(), 1 ); |
678 | 0 | append( buffer, "Language", md.getLanguage(), 1 ); |
679 | |
|
680 | 0 | String phase = md.getPhase(); |
681 | 0 | if ( StringUtils.isNotEmpty( phase ) ) |
682 | |
{ |
683 | 0 | append( buffer, "Bound to phase", phase, 1 ); |
684 | |
} |
685 | |
|
686 | 0 | String eGoal = md.getExecuteGoal(); |
687 | 0 | String eLife = md.getExecuteLifecycle(); |
688 | 0 | String ePhase = md.getExecutePhase(); |
689 | |
|
690 | 0 | if ( StringUtils.isNotEmpty( eGoal ) || StringUtils.isNotEmpty( ePhase ) ) |
691 | |
{ |
692 | 0 | append( buffer, "Before this mojo executes, it will call:", 1 ); |
693 | |
|
694 | 0 | if ( StringUtils.isNotEmpty( eGoal ) ) |
695 | |
{ |
696 | 0 | append( buffer, "Single mojo", "'" + eGoal + "'", 2 ); |
697 | |
} |
698 | |
|
699 | 0 | if ( StringUtils.isNotEmpty( ePhase ) ) |
700 | |
{ |
701 | 0 | String s = "Phase: '" + ePhase + "'"; |
702 | |
|
703 | 0 | if ( StringUtils.isNotEmpty( eLife ) ) |
704 | |
{ |
705 | 0 | s += " in Lifecycle Overlay: '" + eLife + "'"; |
706 | |
} |
707 | |
|
708 | 0 | append( buffer, s, 2 ); |
709 | |
} |
710 | |
} |
711 | |
|
712 | 0 | buffer.append( "\n" ); |
713 | |
|
714 | 0 | describeMojoParameters( md, buffer ); |
715 | 0 | } |
716 | |
|
717 | |
|
718 | |
|
719 | |
|
720 | |
|
721 | |
|
722 | |
|
723 | |
|
724 | |
|
725 | |
private void describeMojoParameters( MojoDescriptor md, StringBuffer buffer ) |
726 | |
throws MojoFailureException, MojoExecutionException |
727 | |
{ |
728 | 0 | List params = md.getParameters(); |
729 | |
|
730 | 0 | if ( params == null || params.isEmpty() ) |
731 | |
{ |
732 | 0 | append( buffer, "This mojo doesn't use any parameters.", 1 ); |
733 | 0 | return; |
734 | |
} |
735 | |
|
736 | |
|
737 | 0 | Collections.sort( params, new Comparator() |
738 | 0 | { |
739 | |
|
740 | |
public int compare( Object o1, Object o2 ) |
741 | |
{ |
742 | 0 | Parameter parameter1 = (Parameter) o1; |
743 | 0 | Parameter parameter2 = (Parameter) o2; |
744 | |
|
745 | 0 | return parameter1.getName().compareToIgnoreCase( parameter2.getName() ); |
746 | |
} |
747 | |
} ); |
748 | |
|
749 | 0 | append( buffer, "Available parameters:", 1 ); |
750 | |
|
751 | |
|
752 | 0 | for ( Iterator it = params.iterator(); it.hasNext(); ) |
753 | |
{ |
754 | 0 | Parameter parameter = (Parameter) it.next(); |
755 | 0 | if ( !parameter.isEditable() ) |
756 | |
{ |
757 | 0 | continue; |
758 | |
} |
759 | |
|
760 | 0 | buffer.append( "\n" ); |
761 | |
|
762 | |
|
763 | 0 | String defaultVal = parameter.getDefaultValue(); |
764 | 0 | if ( defaultVal == null ) |
765 | |
{ |
766 | |
|
767 | 0 | defaultVal = |
768 | |
md.getMojoConfiguration().getChild( parameter.getName() ).getAttribute( "default-value", null ); |
769 | |
} |
770 | |
|
771 | 0 | if ( StringUtils.isNotEmpty( defaultVal ) ) |
772 | |
{ |
773 | 0 | defaultVal = " (Default: " + defaultVal + ")"; |
774 | |
} |
775 | |
else |
776 | |
{ |
777 | 0 | defaultVal = ""; |
778 | |
} |
779 | 0 | append( buffer, parameter.getName() + defaultVal, 2 ); |
780 | |
|
781 | 0 | String expression = parameter.getExpression(); |
782 | 0 | if ( StringUtils.isNotEmpty( expression ) ) |
783 | |
{ |
784 | 0 | append( buffer, "Expression", expression, 3 ); |
785 | |
} |
786 | |
|
787 | 0 | append( buffer, toDescription( parameter.getDescription() ), 3 ); |
788 | |
|
789 | 0 | String deprecation = parameter.getDeprecated(); |
790 | 0 | if ( deprecation != null && deprecation.length() <= 0 ) |
791 | |
{ |
792 | 0 | deprecation = NO_REASON; |
793 | |
} |
794 | |
|
795 | 0 | if ( StringUtils.isNotEmpty( deprecation ) ) |
796 | |
{ |
797 | 0 | append( buffer, "Deprecated. " + deprecation, 3 ); |
798 | |
} |
799 | 0 | } |
800 | 0 | } |
801 | |
|
802 | |
|
803 | |
|
804 | |
|
805 | |
|
806 | |
|
807 | |
|
808 | |
|
809 | |
|
810 | |
private boolean describeCommand( StringBuffer descriptionBuffer ) |
811 | |
throws MojoFailureException, MojoExecutionException |
812 | |
{ |
813 | 0 | if ( cmd.indexOf( ":" ) == -1 ) |
814 | |
{ |
815 | |
|
816 | |
try |
817 | |
{ |
818 | 0 | DefaultLifecycleExecutor lifecycleExecutor = |
819 | |
(DefaultLifecycleExecutor) session.lookup( LifecycleExecutor.ROLE ); |
820 | |
|
821 | 0 | Lifecycle lifecycle = (Lifecycle) lifecycleExecutor.getPhaseToLifecycleMap().get( cmd ); |
822 | 0 | if ( lifecycle == null ) |
823 | |
{ |
824 | 0 | throw new MojoExecutionException( "The given phase '" + cmd + "' is an unknown phase." ); |
825 | |
} |
826 | |
|
827 | 0 | LifecycleMapping lifecycleMapping = |
828 | |
(LifecycleMapping) session.lookup( LifecycleMapping.ROLE, project.getPackaging() ); |
829 | 0 | if ( lifecycle.getDefaultPhases() == null ) |
830 | |
{ |
831 | 0 | descriptionBuffer.append( "'" + cmd + "' is a phase corresponding to this plugin:\n" ); |
832 | 0 | for ( Iterator it = lifecycle.getPhases().iterator(); it.hasNext(); ) |
833 | |
{ |
834 | 0 | String key = (String) it.next(); |
835 | |
|
836 | 0 | if ( !key.equals( cmd ) ) |
837 | |
{ |
838 | 0 | continue; |
839 | |
} |
840 | |
|
841 | 0 | if ( lifecycleMapping.getPhases( "default" ).get( key ) != null ) |
842 | |
{ |
843 | 0 | descriptionBuffer.append( lifecycleMapping.getPhases( "default" ).get( key ) ); |
844 | 0 | descriptionBuffer.append( "\n" ); |
845 | |
} |
846 | 0 | } |
847 | |
|
848 | 0 | descriptionBuffer.append( "\n" ); |
849 | 0 | descriptionBuffer.append( "It is a part of the lifecycle for the POM packaging '" |
850 | |
+ project.getPackaging() + "'. This lifecycle includes the following phases:" ); |
851 | 0 | descriptionBuffer.append( "\n" ); |
852 | 0 | for ( Iterator it = lifecycle.getPhases().iterator(); it.hasNext(); ) |
853 | |
{ |
854 | 0 | String key = (String) it.next(); |
855 | |
|
856 | 0 | descriptionBuffer.append( "* " + key + ": " ); |
857 | 0 | String value = (String) lifecycleMapping.getPhases( "default" ).get( key ); |
858 | 0 | if ( StringUtils.isNotEmpty( value ) ) |
859 | |
{ |
860 | 0 | for ( StringTokenizer tok = new StringTokenizer( value, "," ); tok.hasMoreTokens(); ) |
861 | |
{ |
862 | 0 | descriptionBuffer.append( tok.nextToken().trim() ); |
863 | |
|
864 | 0 | if ( !tok.hasMoreTokens() ) |
865 | |
{ |
866 | 0 | descriptionBuffer.append( "\n" ); |
867 | |
} |
868 | |
else |
869 | |
{ |
870 | 0 | descriptionBuffer.append( ", " ); |
871 | |
} |
872 | |
} |
873 | |
} |
874 | |
else |
875 | |
{ |
876 | 0 | descriptionBuffer.append( NOT_DEFINED ).append( "\n" ); |
877 | |
} |
878 | 0 | } |
879 | |
} |
880 | |
else |
881 | |
{ |
882 | 0 | descriptionBuffer.append( "'" + cmd + "' is a lifecycle with the following phases: " ); |
883 | 0 | descriptionBuffer.append( "\n" ); |
884 | |
|
885 | 0 | for ( Iterator it = lifecycle.getPhases().iterator(); it.hasNext(); ) |
886 | |
{ |
887 | 0 | String key = (String) it.next(); |
888 | |
|
889 | 0 | descriptionBuffer.append( "* " + key + ": " ); |
890 | 0 | if ( lifecycle.getDefaultPhases().get( key ) != null ) |
891 | |
{ |
892 | 0 | descriptionBuffer.append( lifecycle.getDefaultPhases().get( key ) ).append( "\n" ); |
893 | |
} |
894 | |
else |
895 | |
{ |
896 | 0 | descriptionBuffer.append( NOT_DEFINED ).append( "\n" ); |
897 | |
} |
898 | 0 | } |
899 | |
} |
900 | |
} |
901 | 0 | catch ( ComponentLookupException e ) |
902 | |
{ |
903 | 0 | throw new MojoFailureException( "ComponentLookupException: " + e.getMessage() ); |
904 | |
} |
905 | 0 | catch ( LifecycleExecutionException e ) |
906 | |
{ |
907 | 0 | throw new MojoFailureException( "LifecycleExecutionException: " + e.getMessage() ); |
908 | 0 | } |
909 | |
|
910 | 0 | return false; |
911 | |
} |
912 | |
|
913 | |
|
914 | 0 | MojoDescriptor mojoDescriptor = HelpUtil.getMojoDescriptor( cmd, session, project, cmd, true, false ); |
915 | |
|
916 | 0 | descriptionBuffer.append( "'" + cmd + "' is a plugin goal (aka mojo)" ).append( ".\n" ); |
917 | 0 | plugin = mojoDescriptor.getPluginDescriptor().getId(); |
918 | 0 | goal = mojoDescriptor.getGoal(); |
919 | |
|
920 | 0 | return true; |
921 | |
} |
922 | |
|
923 | |
|
924 | |
|
925 | |
|
926 | |
|
927 | |
|
928 | |
|
929 | |
|
930 | |
|
931 | |
|
932 | |
|
933 | |
|
934 | |
|
935 | |
|
936 | |
private static List toLines( String text, int indent, int indentSize, int lineLength ) |
937 | |
throws MojoFailureException, MojoExecutionException |
938 | |
{ |
939 | |
try |
940 | |
{ |
941 | 0 | Method m = |
942 | 0 | HelpMojo.class.getDeclaredMethod( "toLines", new Class[] { String.class, Integer.TYPE, |
943 | |
Integer.TYPE, Integer.TYPE } ); |
944 | 0 | m.setAccessible( true ); |
945 | 0 | List output = |
946 | |
(List) m.invoke( HelpMojo.class, new Object[] { text, new Integer( indent ), |
947 | |
new Integer( indentSize ), new Integer( lineLength ) } ); |
948 | |
|
949 | 0 | if ( output == null ) |
950 | |
{ |
951 | 0 | throw new MojoExecutionException( "No output was specified." ); |
952 | |
} |
953 | |
|
954 | 0 | return output; |
955 | |
} |
956 | 0 | catch ( SecurityException e ) |
957 | |
{ |
958 | 0 | throw new MojoFailureException( "SecurityException: " + e.getMessage() ); |
959 | |
} |
960 | 0 | catch ( IllegalArgumentException e ) |
961 | |
{ |
962 | 0 | throw new MojoFailureException( "IllegalArgumentException: " + e.getMessage() ); |
963 | |
} |
964 | 0 | catch ( NoSuchMethodException e ) |
965 | |
{ |
966 | 0 | throw new MojoFailureException( "NoSuchMethodException: " + e.getMessage() ); |
967 | |
} |
968 | 0 | catch ( IllegalAccessException e ) |
969 | |
{ |
970 | 0 | throw new MojoFailureException( "IllegalAccessException: " + e.getMessage() ); |
971 | |
} |
972 | 0 | catch ( InvocationTargetException e ) |
973 | |
{ |
974 | 0 | Throwable cause = e.getCause(); |
975 | |
|
976 | 0 | if ( cause instanceof NegativeArraySizeException ) |
977 | |
{ |
978 | 0 | throw new MojoFailureException( "NegativeArraySizeException: " + cause.getMessage() ); |
979 | |
} |
980 | |
|
981 | 0 | throw new MojoFailureException( "InvocationTargetException: " + e.getMessage() ); |
982 | |
} |
983 | |
} |
984 | |
|
985 | |
|
986 | |
|
987 | |
|
988 | |
|
989 | |
|
990 | |
|
991 | |
|
992 | |
|
993 | |
|
994 | |
|
995 | |
|
996 | |
private static void append( StringBuffer sb, String description, int indent ) |
997 | |
throws MojoFailureException, MojoExecutionException |
998 | |
{ |
999 | 0 | if ( StringUtils.isEmpty( description ) ) |
1000 | |
{ |
1001 | 0 | sb.append( UNKNOWN ).append( '\n' ); |
1002 | 0 | return; |
1003 | |
} |
1004 | |
|
1005 | 0 | for ( Iterator it = toLines( description, indent, INDENT_SIZE, LINE_LENGTH ).iterator(); it.hasNext(); ) |
1006 | |
{ |
1007 | 0 | sb.append( it.next().toString() ).append( '\n' ); |
1008 | |
} |
1009 | 0 | } |
1010 | |
|
1011 | |
|
1012 | |
|
1013 | |
|
1014 | |
|
1015 | |
|
1016 | |
|
1017 | |
|
1018 | |
|
1019 | |
|
1020 | |
|
1021 | |
|
1022 | |
|
1023 | |
private static void append( StringBuffer sb, String key, String value, int indent ) |
1024 | |
throws MojoFailureException, MojoExecutionException |
1025 | |
{ |
1026 | 0 | if ( StringUtils.isEmpty( key ) ) |
1027 | |
{ |
1028 | 0 | throw new IllegalArgumentException( "Key is required!" ); |
1029 | |
} |
1030 | |
|
1031 | 0 | if ( StringUtils.isEmpty( value ) ) |
1032 | |
{ |
1033 | 0 | value = UNKNOWN; |
1034 | |
} |
1035 | |
|
1036 | 0 | String description = key + ": " + value; |
1037 | 0 | for ( Iterator it = toLines( description, indent, INDENT_SIZE, LINE_LENGTH ).iterator(); it.hasNext(); ) |
1038 | |
{ |
1039 | 0 | sb.append( it.next().toString() ).append( '\n' ); |
1040 | |
} |
1041 | 0 | } |
1042 | |
|
1043 | |
|
1044 | |
|
1045 | |
|
1046 | |
|
1047 | |
|
1048 | |
|
1049 | |
|
1050 | |
|
1051 | |
|
1052 | |
|
1053 | |
|
1054 | |
|
1055 | |
|
1056 | |
private static void appendAsParagraph( StringBuffer sb, String key, String value, int indent ) |
1057 | |
throws MojoFailureException, MojoExecutionException |
1058 | |
{ |
1059 | 0 | if ( StringUtils.isEmpty( value ) ) |
1060 | |
{ |
1061 | 0 | value = UNKNOWN; |
1062 | |
} |
1063 | |
|
1064 | |
String description; |
1065 | 0 | if ( key == null ) |
1066 | |
{ |
1067 | 0 | description = value; |
1068 | |
} |
1069 | |
else |
1070 | |
{ |
1071 | 0 | description = key + ": " + value; |
1072 | |
} |
1073 | |
|
1074 | 0 | List l1 = toLines( description, indent, INDENT_SIZE, LINE_LENGTH - INDENT_SIZE ); |
1075 | 0 | List l2 = toLines( description, indent + 1, INDENT_SIZE, LINE_LENGTH ); |
1076 | 0 | l2.set( 0, l1.get( 0 ) ); |
1077 | 0 | for ( Iterator it = l2.iterator(); it.hasNext(); ) |
1078 | |
{ |
1079 | 0 | sb.append( it.next().toString() ).append( '\n' ); |
1080 | |
} |
1081 | 0 | } |
1082 | |
|
1083 | |
|
1084 | |
|
1085 | |
|
1086 | |
|
1087 | |
|
1088 | |
|
1089 | |
private static String toDescription( String description ) |
1090 | |
{ |
1091 | 0 | if ( StringUtils.isNotEmpty( description ) ) |
1092 | |
{ |
1093 | 0 | return PluginUtils.toText( description ); |
1094 | |
} |
1095 | |
|
1096 | 0 | return "(no description available)"; |
1097 | |
} |
1098 | |
|
1099 | |
|
1100 | |
|
1101 | |
|
1102 | 0 | static class PluginInfo |
1103 | |
{ |
1104 | |
private String prefix; |
1105 | |
|
1106 | |
private String groupId; |
1107 | |
|
1108 | |
private String artifactId; |
1109 | |
|
1110 | |
private String version; |
1111 | |
|
1112 | |
private String mojo; |
1113 | |
|
1114 | |
private Plugin plugin; |
1115 | |
|
1116 | |
private PluginDescriptor pluginDescriptor; |
1117 | |
|
1118 | |
|
1119 | |
|
1120 | |
|
1121 | |
public String getPrefix() |
1122 | |
{ |
1123 | 0 | return prefix; |
1124 | |
} |
1125 | |
|
1126 | |
|
1127 | |
|
1128 | |
|
1129 | |
public void setPrefix( String prefix ) |
1130 | |
{ |
1131 | 0 | this.prefix = prefix; |
1132 | 0 | } |
1133 | |
|
1134 | |
|
1135 | |
|
1136 | |
|
1137 | |
public String getGroupId() |
1138 | |
{ |
1139 | 0 | return groupId; |
1140 | |
} |
1141 | |
|
1142 | |
|
1143 | |
|
1144 | |
|
1145 | |
public void setGroupId( String groupId ) |
1146 | |
{ |
1147 | 0 | this.groupId = groupId; |
1148 | 0 | } |
1149 | |
|
1150 | |
|
1151 | |
|
1152 | |
|
1153 | |
public String getArtifactId() |
1154 | |
{ |
1155 | 0 | return artifactId; |
1156 | |
} |
1157 | |
|
1158 | |
|
1159 | |
|
1160 | |
|
1161 | |
public void setArtifactId( String artifactId ) |
1162 | |
{ |
1163 | 0 | this.artifactId = artifactId; |
1164 | 0 | } |
1165 | |
|
1166 | |
|
1167 | |
|
1168 | |
|
1169 | |
public String getVersion() |
1170 | |
{ |
1171 | 0 | return version; |
1172 | |
} |
1173 | |
|
1174 | |
|
1175 | |
|
1176 | |
|
1177 | |
public void setVersion( String version ) |
1178 | |
{ |
1179 | 0 | this.version = version; |
1180 | 0 | } |
1181 | |
|
1182 | |
|
1183 | |
|
1184 | |
|
1185 | |
public String getMojo() |
1186 | |
{ |
1187 | 0 | return mojo; |
1188 | |
} |
1189 | |
|
1190 | |
|
1191 | |
|
1192 | |
|
1193 | |
public void setMojo( String mojo ) |
1194 | |
{ |
1195 | 0 | this.mojo = mojo; |
1196 | 0 | } |
1197 | |
|
1198 | |
|
1199 | |
|
1200 | |
|
1201 | |
public Plugin getPlugin() |
1202 | |
{ |
1203 | 0 | return plugin; |
1204 | |
} |
1205 | |
|
1206 | |
|
1207 | |
|
1208 | |
|
1209 | |
public void setPlugin( Plugin plugin ) |
1210 | |
{ |
1211 | 0 | this.plugin = plugin; |
1212 | 0 | } |
1213 | |
|
1214 | |
|
1215 | |
|
1216 | |
|
1217 | |
public PluginDescriptor getPluginDescriptor() |
1218 | |
{ |
1219 | 0 | return pluginDescriptor; |
1220 | |
} |
1221 | |
|
1222 | |
|
1223 | |
|
1224 | |
|
1225 | |
public void setPluginDescriptor( PluginDescriptor pluginDescriptor ) |
1226 | |
{ |
1227 | 0 | this.pluginDescriptor = pluginDescriptor; |
1228 | 0 | } |
1229 | |
} |
1230 | |
} |