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 org.apache.maven.plugin.descriptor.DuplicateMojoDescriptorException; |
23 | |
import org.apache.maven.plugin.descriptor.MojoDescriptor; |
24 | |
import org.apache.maven.plugin.descriptor.Parameter; |
25 | |
import org.apache.maven.plugin.descriptor.PluginDescriptor; |
26 | |
import org.apache.maven.plugin.descriptor.Requirement; |
27 | |
import org.apache.maven.project.MavenProject; |
28 | |
import org.apache.maven.tools.plugin.ExtendedMojoDescriptor; |
29 | |
import org.apache.maven.tools.plugin.PluginToolsRequest; |
30 | |
import org.apache.maven.tools.plugin.util.PluginUtils; |
31 | |
import org.codehaus.plexus.util.IOUtil; |
32 | |
import org.codehaus.plexus.util.StringUtils; |
33 | |
import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter; |
34 | |
import org.codehaus.plexus.util.xml.XMLWriter; |
35 | |
|
36 | |
import java.io.File; |
37 | |
import java.io.FileOutputStream; |
38 | |
import java.io.IOException; |
39 | |
import java.io.OutputStreamWriter; |
40 | |
import java.io.Writer; |
41 | |
import java.text.SimpleDateFormat; |
42 | |
import java.util.Date; |
43 | |
import java.util.LinkedHashMap; |
44 | |
import java.util.LinkedHashSet; |
45 | |
import java.util.List; |
46 | |
import java.util.Map; |
47 | |
import java.util.Set; |
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | 2 | public class PluginDescriptorGenerator |
59 | |
implements Generator |
60 | |
{ |
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
public void execute( File destinationDirectory, PluginToolsRequest request ) |
66 | |
throws GeneratorException |
67 | |
{ |
68 | |
|
69 | 2 | PluginHelpGenerator.rewriteHelpMojo( request ); |
70 | |
|
71 | |
try |
72 | |
{ |
73 | |
|
74 | 2 | File f = new File( destinationDirectory, "plugin.xml" ); |
75 | 2 | writeDescriptor( f, request, false ); |
76 | |
|
77 | |
|
78 | 2 | MavenProject mavenProject = request.getProject(); |
79 | 2 | String pluginHelpFilePath = |
80 | |
"META-INF/maven/" + mavenProject.getGroupId() + "/" + mavenProject.getArtifactId() + "/plugin-help.xml"; |
81 | |
|
82 | 2 | f = new File( request.getProject().getBuild().getOutputDirectory(), pluginHelpFilePath ); |
83 | 2 | writeDescriptor( f, request, true ); |
84 | |
} |
85 | 0 | catch ( IOException e ) |
86 | |
{ |
87 | 0 | throw new GeneratorException( e.getMessage(), e ); |
88 | |
} |
89 | 0 | catch ( DuplicateMojoDescriptorException e ) |
90 | |
{ |
91 | 0 | throw new GeneratorException( e.getMessage(), e ); |
92 | 2 | } |
93 | 2 | } |
94 | |
|
95 | |
private String getVersion() |
96 | |
{ |
97 | 4 | Package p = this.getClass().getPackage(); |
98 | 4 | String version = ( p == null ) ? null : p.getSpecificationVersion(); |
99 | 4 | return ( version == null ) ? "SNAPSHOT" : version; |
100 | |
} |
101 | |
|
102 | |
public void writeDescriptor( File destinationFile, PluginToolsRequest request, boolean helpDescriptor ) |
103 | |
throws IOException, DuplicateMojoDescriptorException |
104 | |
{ |
105 | 4 | PluginDescriptor pluginDescriptor = request.getPluginDescriptor(); |
106 | |
|
107 | 4 | if ( destinationFile.exists() ) |
108 | |
{ |
109 | 2 | destinationFile.delete(); |
110 | |
} |
111 | |
else |
112 | |
{ |
113 | 2 | if ( !destinationFile.getParentFile().exists() ) |
114 | |
{ |
115 | 0 | destinationFile.getParentFile().mkdirs(); |
116 | |
} |
117 | |
} |
118 | |
|
119 | 4 | String encoding = "UTF-8"; |
120 | |
|
121 | 4 | Writer writer = null; |
122 | |
try |
123 | |
{ |
124 | 4 | writer = new OutputStreamWriter( new FileOutputStream( destinationFile ), encoding ); |
125 | |
|
126 | 4 | XMLWriter w = new PrettyPrintXMLWriter( writer, encoding, null ); |
127 | |
|
128 | 4 | w.writeMarkup( "\n<!-- Generated by maven-plugin-tools " + getVersion() + " on " + new SimpleDateFormat( |
129 | |
"yyyy-MM-dd" ).format( new Date() ) + " -->\n\n" ); |
130 | |
|
131 | 4 | w.startElement( "plugin" ); |
132 | |
|
133 | 4 | GeneratorUtils.element( w, "name", pluginDescriptor.getName() ); |
134 | |
|
135 | 4 | GeneratorUtils.element( w, "description", pluginDescriptor.getDescription(), helpDescriptor ); |
136 | |
|
137 | 4 | GeneratorUtils.element( w, "groupId", pluginDescriptor.getGroupId() ); |
138 | |
|
139 | 4 | GeneratorUtils.element( w, "artifactId", pluginDescriptor.getArtifactId() ); |
140 | |
|
141 | 4 | GeneratorUtils.element( w, "version", pluginDescriptor.getVersion() ); |
142 | |
|
143 | 4 | GeneratorUtils.element( w, "goalPrefix", pluginDescriptor.getGoalPrefix() ); |
144 | |
|
145 | 4 | if ( !helpDescriptor ) |
146 | |
{ |
147 | 2 | GeneratorUtils.element( w, "isolatedRealm", String.valueOf( pluginDescriptor.isIsolatedRealm() ) ); |
148 | |
|
149 | 2 | GeneratorUtils.element( w, "inheritedByDefault", |
150 | |
String.valueOf( pluginDescriptor.isInheritedByDefault() ) ); |
151 | |
} |
152 | |
|
153 | 4 | w.startElement( "mojos" ); |
154 | |
|
155 | 4 | if ( pluginDescriptor.getMojos() != null ) |
156 | |
{ |
157 | 4 | @SuppressWarnings( "unchecked" ) List<MojoDescriptor> descriptors = pluginDescriptor.getMojos(); |
158 | |
|
159 | 4 | if ( helpDescriptor ) |
160 | |
{ |
161 | 2 | PluginUtils.sortMojos( descriptors ); |
162 | |
} |
163 | |
|
164 | 4 | for ( MojoDescriptor descriptor : descriptors ) |
165 | |
{ |
166 | 4 | processMojoDescriptor( descriptor, w, helpDescriptor ); |
167 | |
} |
168 | |
} |
169 | |
|
170 | 4 | w.endElement(); |
171 | |
|
172 | 4 | if ( !helpDescriptor ) |
173 | |
{ |
174 | 2 | GeneratorUtils.writeDependencies( w, pluginDescriptor ); |
175 | |
} |
176 | |
|
177 | 4 | w.endElement(); |
178 | |
|
179 | 4 | writer.flush(); |
180 | |
|
181 | |
} |
182 | |
finally |
183 | |
{ |
184 | 4 | IOUtil.close( writer ); |
185 | 4 | } |
186 | 4 | } |
187 | |
|
188 | |
protected void processMojoDescriptor( MojoDescriptor mojoDescriptor, XMLWriter w ) |
189 | |
{ |
190 | 0 | processMojoDescriptor( mojoDescriptor, w, false ); |
191 | 0 | } |
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
protected void processMojoDescriptor( MojoDescriptor mojoDescriptor, XMLWriter w, boolean helpDescriptor ) |
199 | |
{ |
200 | 4 | w.startElement( "mojo" ); |
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | 4 | w.startElement( "goal" ); |
207 | 4 | w.writeText( mojoDescriptor.getGoal() ); |
208 | 4 | w.endElement(); |
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | 4 | String description = mojoDescriptor.getDescription(); |
215 | |
|
216 | 4 | if ( description != null ) |
217 | |
{ |
218 | 0 | w.startElement( "description" ); |
219 | 0 | if ( helpDescriptor ) |
220 | |
{ |
221 | 0 | w.writeText( GeneratorUtils.toText( mojoDescriptor.getDescription() ) ); |
222 | |
} |
223 | |
else |
224 | |
{ |
225 | 0 | w.writeText( mojoDescriptor.getDescription() ); |
226 | |
} |
227 | 0 | w.endElement(); |
228 | |
} |
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | 4 | if ( StringUtils.isNotEmpty( mojoDescriptor.isDependencyResolutionRequired() ) ) |
235 | |
{ |
236 | 4 | GeneratorUtils.element( w, "requiresDependencyResolution", |
237 | |
mojoDescriptor.isDependencyResolutionRequired() ); |
238 | |
} |
239 | |
|
240 | |
|
241 | |
|
242 | |
|
243 | |
|
244 | 4 | GeneratorUtils.element( w, "requiresDirectInvocation", |
245 | |
String.valueOf( mojoDescriptor.isDirectInvocationOnly() ) ); |
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | |
|
251 | 4 | GeneratorUtils.element( w, "requiresProject", String.valueOf( mojoDescriptor.isProjectRequired() ) ); |
252 | |
|
253 | |
|
254 | |
|
255 | |
|
256 | |
|
257 | 4 | GeneratorUtils.element( w, "requiresReports", String.valueOf( mojoDescriptor.isRequiresReports() ) ); |
258 | |
|
259 | |
|
260 | |
|
261 | |
|
262 | |
|
263 | 4 | GeneratorUtils.element( w, "aggregator", String.valueOf( mojoDescriptor.isAggregator() ) ); |
264 | |
|
265 | |
|
266 | |
|
267 | |
|
268 | |
|
269 | 4 | GeneratorUtils.element( w, "requiresOnline", String.valueOf( mojoDescriptor.isOnlineRequired() ) ); |
270 | |
|
271 | |
|
272 | |
|
273 | |
|
274 | |
|
275 | 4 | GeneratorUtils.element( w, "inheritedByDefault", String.valueOf( mojoDescriptor.isInheritedByDefault() ) ); |
276 | |
|
277 | |
|
278 | |
|
279 | |
|
280 | |
|
281 | 4 | if ( StringUtils.isNotEmpty( mojoDescriptor.getPhase() ) ) |
282 | |
{ |
283 | 0 | GeneratorUtils.element( w, "phase", mojoDescriptor.getPhase() ); |
284 | |
} |
285 | |
|
286 | |
|
287 | |
|
288 | |
|
289 | |
|
290 | 4 | if ( StringUtils.isNotEmpty( mojoDescriptor.getExecutePhase() ) ) |
291 | |
{ |
292 | 0 | GeneratorUtils.element( w, "executePhase", mojoDescriptor.getExecutePhase() ); |
293 | |
} |
294 | |
|
295 | 4 | if ( StringUtils.isNotEmpty( mojoDescriptor.getExecuteGoal() ) ) |
296 | |
{ |
297 | 0 | GeneratorUtils.element( w, "executeGoal", mojoDescriptor.getExecuteGoal() ); |
298 | |
} |
299 | |
|
300 | 4 | if ( StringUtils.isNotEmpty( mojoDescriptor.getExecuteLifecycle() ) ) |
301 | |
{ |
302 | 0 | GeneratorUtils.element( w, "executeLifecycle", mojoDescriptor.getExecuteLifecycle() ); |
303 | |
} |
304 | |
|
305 | |
|
306 | |
|
307 | |
|
308 | |
|
309 | 4 | w.startElement( "implementation" ); |
310 | 4 | w.writeText( mojoDescriptor.getImplementation() ); |
311 | 4 | w.endElement(); |
312 | |
|
313 | |
|
314 | |
|
315 | |
|
316 | |
|
317 | 4 | w.startElement( "language" ); |
318 | 4 | w.writeText( mojoDescriptor.getLanguage() ); |
319 | 4 | w.endElement(); |
320 | |
|
321 | |
|
322 | |
|
323 | |
|
324 | |
|
325 | 4 | if ( StringUtils.isNotEmpty( mojoDescriptor.getComponentConfigurator() ) ) |
326 | |
{ |
327 | 0 | w.startElement( "configurator" ); |
328 | 0 | w.writeText( mojoDescriptor.getComponentConfigurator() ); |
329 | 0 | w.endElement(); |
330 | |
} |
331 | |
|
332 | |
|
333 | |
|
334 | |
|
335 | |
|
336 | 4 | if ( StringUtils.isNotEmpty( mojoDescriptor.getComponentComposer() ) ) |
337 | |
{ |
338 | 0 | w.startElement( "composer" ); |
339 | 0 | w.writeText( mojoDescriptor.getComponentComposer() ); |
340 | 0 | w.endElement(); |
341 | |
} |
342 | |
|
343 | |
|
344 | |
|
345 | |
|
346 | |
|
347 | 4 | w.startElement( "instantiationStrategy" ); |
348 | 4 | w.writeText( mojoDescriptor.getInstantiationStrategy() ); |
349 | 4 | w.endElement(); |
350 | |
|
351 | |
|
352 | |
|
353 | |
|
354 | |
|
355 | 4 | w.startElement( "executionStrategy" ); |
356 | 4 | w.writeText( mojoDescriptor.getExecutionStrategy() ); |
357 | 4 | w.endElement(); |
358 | |
|
359 | |
|
360 | |
|
361 | |
|
362 | |
|
363 | 4 | if ( mojoDescriptor.getSince() != null ) |
364 | |
{ |
365 | 0 | w.startElement( "since" ); |
366 | |
|
367 | 0 | if ( StringUtils.isEmpty( mojoDescriptor.getSince() ) ) |
368 | |
{ |
369 | 0 | w.writeText( "No version given" ); |
370 | |
} |
371 | |
else |
372 | |
{ |
373 | 0 | w.writeText( mojoDescriptor.getSince() ); |
374 | |
} |
375 | |
|
376 | 0 | w.endElement(); |
377 | |
} |
378 | |
|
379 | |
|
380 | |
|
381 | |
|
382 | |
|
383 | 4 | if ( mojoDescriptor.getDeprecated() != null ) |
384 | |
{ |
385 | 0 | w.startElement( "deprecated" ); |
386 | |
|
387 | 0 | if ( StringUtils.isEmpty( mojoDescriptor.getDeprecated() ) ) |
388 | |
{ |
389 | 0 | w.writeText( "No reason given" ); |
390 | |
} |
391 | |
else |
392 | |
{ |
393 | 0 | w.writeText( mojoDescriptor.getDeprecated() ); |
394 | |
} |
395 | |
|
396 | 0 | w.endElement(); |
397 | |
} |
398 | |
|
399 | |
|
400 | |
|
401 | |
|
402 | |
|
403 | 4 | if ( mojoDescriptor instanceof ExtendedMojoDescriptor ) |
404 | |
{ |
405 | 0 | ExtendedMojoDescriptor extendedMojoDescriptor = (ExtendedMojoDescriptor) mojoDescriptor; |
406 | 0 | if ( extendedMojoDescriptor.getDependencyCollectionRequired() != null ) |
407 | |
{ |
408 | 0 | GeneratorUtils.element( w, "requiresDependencyCollection", |
409 | |
extendedMojoDescriptor.getDependencyCollectionRequired() ); |
410 | |
} |
411 | |
|
412 | 0 | GeneratorUtils.element( w, "threadSafe", String.valueOf( extendedMojoDescriptor.isThreadSafe() ) ); |
413 | |
} |
414 | |
|
415 | |
|
416 | |
|
417 | |
|
418 | |
|
419 | 4 | @SuppressWarnings( "unchecked" ) List<Parameter> parameters = mojoDescriptor.getParameters(); |
420 | |
|
421 | 4 | w.startElement( "parameters" ); |
422 | |
|
423 | 4 | Map<String, Requirement> requirements = new LinkedHashMap<String, Requirement>(); |
424 | |
|
425 | 4 | Set<Parameter> configuration = new LinkedHashSet<Parameter>(); |
426 | |
|
427 | 4 | if ( parameters != null ) |
428 | |
{ |
429 | 4 | if ( helpDescriptor ) |
430 | |
{ |
431 | 2 | PluginUtils.sortMojoParameters( parameters ); |
432 | |
} |
433 | |
|
434 | 4 | for ( Parameter parameter : parameters ) |
435 | |
{ |
436 | 4 | String expression = getExpression( parameter ); |
437 | |
|
438 | 4 | if ( StringUtils.isNotEmpty( expression ) && expression.startsWith( "${component." ) ) |
439 | |
{ |
440 | |
|
441 | |
|
442 | |
|
443 | 0 | String role = expression.substring( "${component.".length(), expression.length() - 1 ); |
444 | |
|
445 | 0 | String roleHint = null; |
446 | |
|
447 | 0 | int posRoleHintSeparator = role.indexOf( "#" ); |
448 | 0 | if ( posRoleHintSeparator > 0 ) |
449 | |
{ |
450 | 0 | roleHint = role.substring( posRoleHintSeparator + 1 ); |
451 | |
|
452 | 0 | role = role.substring( 0, posRoleHintSeparator ); |
453 | |
} |
454 | |
|
455 | |
|
456 | 0 | requirements.put( parameter.getName(), new Requirement( role, roleHint ) ); |
457 | 0 | } |
458 | 4 | else if ( parameter.getRequirement() != null ) |
459 | |
{ |
460 | 0 | requirements.put( parameter.getName(), parameter.getRequirement() ); |
461 | |
} |
462 | 4 | else if ( !helpDescriptor || parameter.isEditable() ) |
463 | |
{ |
464 | |
|
465 | |
|
466 | 4 | w.startElement( "parameter" ); |
467 | |
|
468 | 4 | GeneratorUtils.element( w, "name", parameter.getName() ); |
469 | |
|
470 | 4 | if ( parameter.getAlias() != null ) |
471 | |
{ |
472 | 0 | GeneratorUtils.element( w, "alias", parameter.getAlias() ); |
473 | |
} |
474 | |
|
475 | 4 | GeneratorUtils.element( w, "type", parameter.getType() ); |
476 | |
|
477 | 4 | if ( parameter.getSince() != null ) |
478 | |
{ |
479 | 0 | w.startElement( "since" ); |
480 | |
|
481 | 0 | if ( StringUtils.isEmpty( parameter.getSince() ) ) |
482 | |
{ |
483 | 0 | w.writeText( "No version given" ); |
484 | |
} |
485 | |
else |
486 | |
{ |
487 | 0 | w.writeText( parameter.getSince() ); |
488 | |
} |
489 | |
|
490 | 0 | w.endElement(); |
491 | |
} |
492 | |
|
493 | 4 | if ( parameter.getDeprecated() != null ) |
494 | |
{ |
495 | 0 | if ( StringUtils.isEmpty( parameter.getDeprecated() ) ) |
496 | |
{ |
497 | 0 | GeneratorUtils.element( w, "deprecated", "No reason given" ); |
498 | |
} |
499 | |
else |
500 | |
{ |
501 | 0 | GeneratorUtils.element( w, "deprecated", parameter.getDeprecated() ); |
502 | |
} |
503 | |
} |
504 | |
|
505 | 4 | if ( parameter.getImplementation() != null ) |
506 | |
{ |
507 | 0 | GeneratorUtils.element( w, "implementation", parameter.getImplementation() ); |
508 | |
} |
509 | |
|
510 | 4 | GeneratorUtils.element( w, "required", Boolean.toString( parameter.isRequired() ) ); |
511 | |
|
512 | 4 | GeneratorUtils.element( w, "editable", Boolean.toString( parameter.isEditable() ) ); |
513 | |
|
514 | 4 | GeneratorUtils.element( w, "description", parameter.getDescription(), helpDescriptor ); |
515 | |
|
516 | 4 | if ( StringUtils.isNotEmpty( parameter.getDefaultValue() ) || StringUtils.isNotEmpty( |
517 | |
parameter.getExpression() ) ) |
518 | |
{ |
519 | 4 | configuration.add( parameter ); |
520 | |
} |
521 | |
|
522 | 4 | w.endElement(); |
523 | |
} |
524 | |
|
525 | 4 | } |
526 | |
} |
527 | |
|
528 | 4 | w.endElement(); |
529 | |
|
530 | |
|
531 | |
|
532 | |
|
533 | |
|
534 | 4 | if ( !configuration.isEmpty() ) |
535 | |
{ |
536 | 4 | w.startElement( "configuration" ); |
537 | |
|
538 | 4 | for ( Parameter parameter : configuration ) |
539 | |
{ |
540 | 4 | if ( helpDescriptor && !parameter.isEditable() ) |
541 | |
{ |
542 | |
|
543 | 0 | continue; |
544 | |
} |
545 | |
|
546 | 4 | w.startElement( parameter.getName() ); |
547 | |
|
548 | 4 | String type = parameter.getType(); |
549 | 4 | if ( StringUtils.isNotEmpty( type ) ) |
550 | |
{ |
551 | 4 | w.addAttribute( "implementation", type ); |
552 | |
} |
553 | |
|
554 | 4 | if ( parameter.getDefaultValue() != null ) |
555 | |
{ |
556 | 4 | w.addAttribute( "default-value", parameter.getDefaultValue() ); |
557 | |
} |
558 | |
|
559 | 4 | if ( StringUtils.isNotEmpty( parameter.getExpression() ) ) |
560 | |
{ |
561 | 4 | w.writeText( parameter.getExpression() ); |
562 | |
} |
563 | |
|
564 | 4 | w.endElement(); |
565 | 4 | } |
566 | |
|
567 | 4 | w.endElement(); |
568 | |
} |
569 | |
|
570 | |
|
571 | |
|
572 | |
|
573 | |
|
574 | 4 | if ( !requirements.isEmpty() && !helpDescriptor ) |
575 | |
{ |
576 | 0 | w.startElement( "requirements" ); |
577 | |
|
578 | 0 | for ( Map.Entry<String, Requirement> entry : requirements.entrySet() ) |
579 | |
{ |
580 | 0 | String key = entry.getKey(); |
581 | 0 | Requirement requirement = entry.getValue(); |
582 | |
|
583 | 0 | w.startElement( "requirement" ); |
584 | |
|
585 | 0 | GeneratorUtils.element( w, "role", requirement.getRole() ); |
586 | |
|
587 | 0 | if ( StringUtils.isNotEmpty( requirement.getRoleHint() ) ) |
588 | |
{ |
589 | 0 | GeneratorUtils.element( w, "role-hint", requirement.getRoleHint() ); |
590 | |
} |
591 | |
|
592 | 0 | GeneratorUtils.element( w, "field-name", key ); |
593 | |
|
594 | 0 | w.endElement(); |
595 | 0 | } |
596 | |
|
597 | 0 | w.endElement(); |
598 | |
} |
599 | |
|
600 | 4 | w.endElement(); |
601 | 4 | } |
602 | |
|
603 | |
|
604 | |
|
605 | |
|
606 | |
|
607 | |
|
608 | |
|
609 | |
private String getExpression( Parameter parameter ) |
610 | |
{ |
611 | 4 | String expression = parameter.getExpression(); |
612 | 4 | if ( StringUtils.isNotBlank( expression ) && !expression.contains( "${" ) ) |
613 | |
{ |
614 | 0 | expression = "${" + expression.trim() + "}"; |
615 | 0 | parameter.setExpression( expression ); |
616 | |
} |
617 | 4 | return expression; |
618 | |
} |
619 | |
} |