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 | 1 | 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 | 1 | PluginHelpGenerator.rewriteHelpMojo( request ); |
70 | |
|
71 | |
try |
72 | |
{ |
73 | |
|
74 | 1 | File f = new File( destinationDirectory, "plugin.xml" ); |
75 | 1 | writeDescriptor( f, request, false ); |
76 | |
|
77 | |
|
78 | 1 | MavenProject mavenProject = request.getProject(); |
79 | 1 | String pluginHelpFilePath = |
80 | |
"META-INF/maven/" + mavenProject.getGroupId() + "/" + mavenProject.getArtifactId() + "/plugin-help.xml"; |
81 | |
|
82 | 1 | f = new File( request.getProject().getBuild().getOutputDirectory(), pluginHelpFilePath ); |
83 | 1 | 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 | 1 | } |
93 | 1 | } |
94 | |
|
95 | |
private String getVersion() |
96 | |
{ |
97 | 2 | Package p = this.getClass().getPackage(); |
98 | 2 | String version = ( p == null ) ? null : p.getSpecificationVersion(); |
99 | 2 | return ( version == null ) ? "SNAPSHOT" : version; |
100 | |
} |
101 | |
|
102 | |
public void writeDescriptor( File destinationFile, PluginToolsRequest request, boolean helpDescriptor ) |
103 | |
throws IOException, DuplicateMojoDescriptorException |
104 | |
{ |
105 | 2 | PluginDescriptor pluginDescriptor = request.getPluginDescriptor(); |
106 | |
|
107 | 2 | if ( destinationFile.exists() ) |
108 | |
{ |
109 | 1 | destinationFile.delete(); |
110 | |
} |
111 | |
else |
112 | |
{ |
113 | 1 | if ( !destinationFile.getParentFile().exists() ) |
114 | |
{ |
115 | 0 | destinationFile.getParentFile().mkdirs(); |
116 | |
} |
117 | |
} |
118 | |
|
119 | 2 | String encoding = "UTF-8"; |
120 | |
|
121 | 2 | Writer writer = null; |
122 | |
try |
123 | |
{ |
124 | 2 | writer = new OutputStreamWriter( new FileOutputStream( destinationFile ), encoding ); |
125 | |
|
126 | 2 | XMLWriter w = new PrettyPrintXMLWriter( writer, encoding, null ); |
127 | |
|
128 | 2 | w.writeMarkup( "\n<!-- Generated by maven-plugin-tools " + getVersion() + " on " + new SimpleDateFormat( |
129 | |
"yyyy-MM-dd" ).format( new Date() ) + " -->\n\n" ); |
130 | |
|
131 | 2 | w.startElement( "plugin" ); |
132 | |
|
133 | 2 | GeneratorUtils.element( w, "name", pluginDescriptor.getName() ); |
134 | |
|
135 | 2 | GeneratorUtils.element( w, "description", pluginDescriptor.getDescription(), helpDescriptor ); |
136 | |
|
137 | 2 | GeneratorUtils.element( w, "groupId", pluginDescriptor.getGroupId() ); |
138 | |
|
139 | 2 | GeneratorUtils.element( w, "artifactId", pluginDescriptor.getArtifactId() ); |
140 | |
|
141 | 2 | GeneratorUtils.element( w, "version", pluginDescriptor.getVersion() ); |
142 | |
|
143 | 2 | GeneratorUtils.element( w, "goalPrefix", pluginDescriptor.getGoalPrefix() ); |
144 | |
|
145 | 2 | if ( !helpDescriptor ) |
146 | |
{ |
147 | 1 | GeneratorUtils.element( w, "isolatedRealm", String.valueOf( pluginDescriptor.isIsolatedRealm() ) ); |
148 | |
|
149 | 1 | GeneratorUtils.element( w, "inheritedByDefault", |
150 | |
String.valueOf( pluginDescriptor.isInheritedByDefault() ) ); |
151 | |
} |
152 | |
|
153 | 2 | w.startElement( "mojos" ); |
154 | |
|
155 | 2 | if ( pluginDescriptor.getMojos() != null ) |
156 | |
{ |
157 | 2 | @SuppressWarnings( "unchecked" ) List<MojoDescriptor> descriptors = pluginDescriptor.getMojos(); |
158 | |
|
159 | 2 | if ( helpDescriptor ) |
160 | |
{ |
161 | 1 | PluginUtils.sortMojos( descriptors ); |
162 | |
} |
163 | |
|
164 | 2 | for ( MojoDescriptor descriptor : descriptors ) |
165 | |
{ |
166 | 2 | processMojoDescriptor( descriptor, w, helpDescriptor ); |
167 | |
} |
168 | |
} |
169 | |
|
170 | 2 | w.endElement(); |
171 | |
|
172 | 2 | if ( !helpDescriptor ) |
173 | |
{ |
174 | 1 | GeneratorUtils.writeDependencies( w, pluginDescriptor ); |
175 | |
} |
176 | |
|
177 | 2 | w.endElement(); |
178 | |
|
179 | 2 | writer.flush(); |
180 | |
|
181 | |
} |
182 | |
finally |
183 | |
{ |
184 | 2 | IOUtil.close( writer ); |
185 | 2 | } |
186 | 2 | } |
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 | 2 | w.startElement( "mojo" ); |
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | 2 | w.startElement( "goal" ); |
207 | 2 | w.writeText( mojoDescriptor.getGoal() ); |
208 | 2 | w.endElement(); |
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | 2 | String description = mojoDescriptor.getDescription(); |
215 | |
|
216 | 2 | 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 | 2 | if ( StringUtils.isNotEmpty( mojoDescriptor.isDependencyResolutionRequired() ) ) |
235 | |
{ |
236 | 2 | GeneratorUtils.element( w, "requiresDependencyResolution", |
237 | |
mojoDescriptor.isDependencyResolutionRequired() ); |
238 | |
} |
239 | |
|
240 | |
|
241 | |
|
242 | |
|
243 | |
|
244 | 2 | GeneratorUtils.element( w, "requiresDirectInvocation", |
245 | |
String.valueOf( mojoDescriptor.isDirectInvocationOnly() ) ); |
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | |
|
251 | 2 | GeneratorUtils.element( w, "requiresProject", String.valueOf( mojoDescriptor.isProjectRequired() ) ); |
252 | |
|
253 | |
|
254 | |
|
255 | |
|
256 | |
|
257 | 2 | GeneratorUtils.element( w, "requiresReports", String.valueOf( mojoDescriptor.isRequiresReports() ) ); |
258 | |
|
259 | |
|
260 | |
|
261 | |
|
262 | |
|
263 | 2 | GeneratorUtils.element( w, "aggregator", String.valueOf( mojoDescriptor.isAggregator() ) ); |
264 | |
|
265 | |
|
266 | |
|
267 | |
|
268 | |
|
269 | 2 | GeneratorUtils.element( w, "requiresOnline", String.valueOf( mojoDescriptor.isOnlineRequired() ) ); |
270 | |
|
271 | |
|
272 | |
|
273 | |
|
274 | |
|
275 | 2 | GeneratorUtils.element( w, "inheritedByDefault", String.valueOf( mojoDescriptor.isInheritedByDefault() ) ); |
276 | |
|
277 | |
|
278 | |
|
279 | |
|
280 | |
|
281 | 2 | if ( StringUtils.isNotEmpty( mojoDescriptor.getPhase() ) ) |
282 | |
{ |
283 | 0 | GeneratorUtils.element( w, "phase", mojoDescriptor.getPhase() ); |
284 | |
} |
285 | |
|
286 | |
|
287 | |
|
288 | |
|
289 | |
|
290 | 2 | if ( StringUtils.isNotEmpty( mojoDescriptor.getExecutePhase() ) ) |
291 | |
{ |
292 | 0 | GeneratorUtils.element( w, "executePhase", mojoDescriptor.getExecutePhase() ); |
293 | |
} |
294 | |
|
295 | 2 | if ( StringUtils.isNotEmpty( mojoDescriptor.getExecuteGoal() ) ) |
296 | |
{ |
297 | 0 | GeneratorUtils.element( w, "executeGoal", mojoDescriptor.getExecuteGoal() ); |
298 | |
} |
299 | |
|
300 | 2 | if ( StringUtils.isNotEmpty( mojoDescriptor.getExecuteLifecycle() ) ) |
301 | |
{ |
302 | 0 | GeneratorUtils.element( w, "executeLifecycle", mojoDescriptor.getExecuteLifecycle() ); |
303 | |
} |
304 | |
|
305 | |
|
306 | |
|
307 | |
|
308 | |
|
309 | 2 | w.startElement( "implementation" ); |
310 | 2 | w.writeText( mojoDescriptor.getImplementation() ); |
311 | 2 | w.endElement(); |
312 | |
|
313 | |
|
314 | |
|
315 | |
|
316 | |
|
317 | 2 | w.startElement( "language" ); |
318 | 2 | w.writeText( mojoDescriptor.getLanguage() ); |
319 | 2 | w.endElement(); |
320 | |
|
321 | |
|
322 | |
|
323 | |
|
324 | |
|
325 | 2 | 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 | 2 | 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 | 2 | w.startElement( "instantiationStrategy" ); |
348 | 2 | w.writeText( mojoDescriptor.getInstantiationStrategy() ); |
349 | 2 | w.endElement(); |
350 | |
|
351 | |
|
352 | |
|
353 | |
|
354 | |
|
355 | 2 | w.startElement( "executionStrategy" ); |
356 | 2 | w.writeText( mojoDescriptor.getExecutionStrategy() ); |
357 | 2 | w.endElement(); |
358 | |
|
359 | |
|
360 | |
|
361 | |
|
362 | |
|
363 | 2 | 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 | 2 | 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 | 2 | 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 | 2 | @SuppressWarnings( "unchecked" ) List<Parameter> parameters = mojoDescriptor.getParameters(); |
420 | |
|
421 | 2 | w.startElement( "parameters" ); |
422 | |
|
423 | 2 | Map<String, Requirement> requirements = new LinkedHashMap<String, Requirement>(); |
424 | |
|
425 | 2 | Set<Parameter> configuration = new LinkedHashSet<Parameter>(); |
426 | |
|
427 | 2 | if ( parameters != null ) |
428 | |
{ |
429 | 2 | if ( helpDescriptor ) |
430 | |
{ |
431 | 1 | PluginUtils.sortMojoParameters( parameters ); |
432 | |
} |
433 | |
|
434 | 2 | for ( Parameter parameter : parameters ) |
435 | |
{ |
436 | 2 | String expression = getExpression( parameter ); |
437 | |
|
438 | 2 | 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 | 2 | else if ( parameter.getRequirement() != null ) |
459 | |
{ |
460 | 0 | requirements.put( parameter.getName(), parameter.getRequirement() ); |
461 | |
} |
462 | 2 | else if ( !helpDescriptor || parameter.isEditable() ) |
463 | |
{ |
464 | |
|
465 | |
|
466 | 2 | w.startElement( "parameter" ); |
467 | |
|
468 | 2 | GeneratorUtils.element( w, "name", parameter.getName() ); |
469 | |
|
470 | 2 | if ( parameter.getAlias() != null ) |
471 | |
{ |
472 | 0 | GeneratorUtils.element( w, "alias", parameter.getAlias() ); |
473 | |
} |
474 | |
|
475 | 2 | GeneratorUtils.element( w, "type", parameter.getType() ); |
476 | |
|
477 | 2 | 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 | 2 | 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 | 2 | if ( parameter.getImplementation() != null ) |
506 | |
{ |
507 | 0 | GeneratorUtils.element( w, "implementation", parameter.getImplementation() ); |
508 | |
} |
509 | |
|
510 | 2 | GeneratorUtils.element( w, "required", Boolean.toString( parameter.isRequired() ) ); |
511 | |
|
512 | 2 | GeneratorUtils.element( w, "editable", Boolean.toString( parameter.isEditable() ) ); |
513 | |
|
514 | 2 | GeneratorUtils.element( w, "description", parameter.getDescription(), helpDescriptor ); |
515 | |
|
516 | 2 | if ( StringUtils.isNotEmpty( parameter.getDefaultValue() ) || StringUtils.isNotEmpty( |
517 | |
parameter.getExpression() ) ) |
518 | |
{ |
519 | 2 | configuration.add( parameter ); |
520 | |
} |
521 | |
|
522 | 2 | w.endElement(); |
523 | |
} |
524 | |
|
525 | 2 | } |
526 | |
} |
527 | |
|
528 | 2 | w.endElement(); |
529 | |
|
530 | |
|
531 | |
|
532 | |
|
533 | |
|
534 | 2 | if ( !configuration.isEmpty() ) |
535 | |
{ |
536 | 2 | w.startElement( "configuration" ); |
537 | |
|
538 | 2 | for ( Parameter parameter : configuration ) |
539 | |
{ |
540 | 2 | if ( helpDescriptor && !parameter.isEditable() ) |
541 | |
{ |
542 | |
|
543 | 0 | continue; |
544 | |
} |
545 | |
|
546 | 2 | w.startElement( parameter.getName() ); |
547 | |
|
548 | 2 | String type = parameter.getType(); |
549 | 2 | if ( StringUtils.isNotEmpty( type ) ) |
550 | |
{ |
551 | 2 | w.addAttribute( "implementation", type ); |
552 | |
} |
553 | |
|
554 | 2 | if ( parameter.getDefaultValue() != null ) |
555 | |
{ |
556 | 2 | w.addAttribute( "default-value", parameter.getDefaultValue() ); |
557 | |
} |
558 | |
|
559 | 2 | if ( StringUtils.isNotEmpty( parameter.getExpression() ) ) |
560 | |
{ |
561 | 2 | w.writeText( parameter.getExpression() ); |
562 | |
} |
563 | |
|
564 | 2 | w.endElement(); |
565 | 2 | } |
566 | |
|
567 | 2 | w.endElement(); |
568 | |
} |
569 | |
|
570 | |
|
571 | |
|
572 | |
|
573 | |
|
574 | 2 | 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 | 2 | w.endElement(); |
601 | 2 | } |
602 | |
|
603 | |
|
604 | |
|
605 | |
|
606 | |
|
607 | |
|
608 | |
|
609 | |
private String getExpression( Parameter parameter ) |
610 | |
{ |
611 | 2 | String expression = parameter.getExpression(); |
612 | 2 | if ( StringUtils.isNotBlank( expression ) && !expression.contains( "${" ) ) |
613 | |
{ |
614 | 0 | expression = "${" + expression.trim() + "}"; |
615 | 0 | parameter.setExpression( expression ); |
616 | |
} |
617 | 2 | return expression; |
618 | |
} |
619 | |
} |