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.MojoDescriptor; |
23 | |
import org.apache.maven.plugin.descriptor.Parameter; |
24 | |
import org.apache.maven.plugin.descriptor.PluginDescriptor; |
25 | |
import org.apache.maven.plugin.descriptor.Requirement; |
26 | |
import org.apache.maven.tools.plugin.DefaultPluginToolsRequest; |
27 | |
import org.apache.maven.tools.plugin.ExtendedMojoDescriptor; |
28 | |
import org.apache.maven.tools.plugin.PluginToolsRequest; |
29 | |
import org.apache.maven.tools.plugin.util.PluginUtils; |
30 | |
import org.codehaus.plexus.util.IOUtil; |
31 | |
import org.codehaus.plexus.util.StringUtils; |
32 | |
import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter; |
33 | |
import org.codehaus.plexus.util.xml.XMLWriter; |
34 | |
|
35 | |
import java.io.File; |
36 | |
import java.io.FileOutputStream; |
37 | |
import java.io.IOException; |
38 | |
import java.io.OutputStreamWriter; |
39 | |
import java.io.Writer; |
40 | |
import java.util.Iterator; |
41 | |
import java.util.LinkedHashMap; |
42 | |
import java.util.LinkedHashSet; |
43 | |
import java.util.List; |
44 | |
import java.util.Map; |
45 | |
import java.util.Set; |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | 1 | public class PluginDescriptorGenerator |
55 | |
implements Generator |
56 | |
{ |
57 | |
|
58 | |
public void execute( File destinationDirectory, PluginDescriptor pluginDescriptor ) |
59 | |
throws IOException |
60 | |
{ |
61 | 0 | execute( destinationDirectory, new DefaultPluginToolsRequest( null, pluginDescriptor ) ); |
62 | 0 | } |
63 | |
|
64 | |
|
65 | |
public void execute( File destinationDirectory, PluginToolsRequest request ) |
66 | |
throws IOException |
67 | |
{ |
68 | 1 | PluginDescriptor pluginDescriptor = request.getPluginDescriptor(); |
69 | |
|
70 | 1 | String encoding = "UTF-8"; |
71 | |
|
72 | 1 | File f = new File( destinationDirectory, "plugin.xml" ); |
73 | |
|
74 | 1 | if ( !f.getParentFile().exists() ) |
75 | |
{ |
76 | 0 | f.getParentFile().mkdirs(); |
77 | |
} |
78 | |
|
79 | 1 | Writer writer = null; |
80 | |
try |
81 | |
{ |
82 | 1 | writer = new OutputStreamWriter( new FileOutputStream( f ), encoding ); |
83 | |
|
84 | 1 | XMLWriter w = new PrettyPrintXMLWriter( writer, encoding, null ); |
85 | |
|
86 | 1 | w.startElement( "plugin" ); |
87 | |
|
88 | 1 | PluginUtils.element( w, "name", pluginDescriptor.getName() ); |
89 | |
|
90 | 1 | PluginUtils.element( w, "description", pluginDescriptor.getDescription() ); |
91 | |
|
92 | 1 | PluginUtils.element( w, "groupId", pluginDescriptor.getGroupId() ); |
93 | |
|
94 | 1 | PluginUtils.element( w, "artifactId", pluginDescriptor.getArtifactId() ); |
95 | |
|
96 | 1 | PluginUtils.element( w, "version", pluginDescriptor.getVersion() ); |
97 | |
|
98 | 1 | PluginUtils.element( w, "goalPrefix", pluginDescriptor.getGoalPrefix() ); |
99 | |
|
100 | 1 | PluginUtils.element( w, "isolatedRealm", "" + pluginDescriptor.isIsolatedRealm() ); |
101 | |
|
102 | 1 | PluginUtils.element( w, "inheritedByDefault", "" + pluginDescriptor.isInheritedByDefault() ); |
103 | |
|
104 | 1 | w.startElement( "mojos" ); |
105 | |
|
106 | 1 | if ( pluginDescriptor.getMojos() != null ) |
107 | |
{ |
108 | |
for ( @SuppressWarnings( "unchecked" ) |
109 | 1 | Iterator<MojoDescriptor> it = pluginDescriptor.getMojos().iterator(); it.hasNext(); ) |
110 | |
{ |
111 | 1 | MojoDescriptor descriptor = it.next(); |
112 | 1 | processMojoDescriptor( descriptor, w ); |
113 | 1 | } |
114 | |
} |
115 | |
|
116 | 1 | w.endElement(); |
117 | |
|
118 | 1 | PluginUtils.writeDependencies( w, pluginDescriptor ); |
119 | |
|
120 | 1 | w.endElement(); |
121 | |
|
122 | 1 | writer.flush(); |
123 | |
} |
124 | |
finally |
125 | |
{ |
126 | 1 | IOUtil.close( writer ); |
127 | 1 | } |
128 | 1 | } |
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
protected void processMojoDescriptor( MojoDescriptor mojoDescriptor, XMLWriter w ) |
135 | |
{ |
136 | 1 | w.startElement( "mojo" ); |
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | 1 | w.startElement( "goal" ); |
143 | 1 | w.writeText( mojoDescriptor.getGoal() ); |
144 | 1 | w.endElement(); |
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | 1 | String description = mojoDescriptor.getDescription(); |
151 | |
|
152 | 1 | if ( description != null ) |
153 | |
{ |
154 | 0 | w.startElement( "description" ); |
155 | 0 | w.writeText( mojoDescriptor.getDescription() ); |
156 | 0 | w.endElement(); |
157 | |
} |
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | 1 | if ( mojoDescriptor.isDependencyResolutionRequired() != null ) |
164 | |
{ |
165 | 1 | PluginUtils.element( w, "requiresDependencyResolution", mojoDescriptor.isDependencyResolutionRequired() ); |
166 | |
} |
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | 1 | PluginUtils.element( w, "requiresDirectInvocation", "" + mojoDescriptor.isDirectInvocationOnly() ); |
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | 1 | PluginUtils.element( w, "requiresProject", "" + mojoDescriptor.isProjectRequired() ); |
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | 1 | PluginUtils.element( w, "requiresReports", "" + mojoDescriptor.isRequiresReports() ); |
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | 1 | PluginUtils.element( w, "aggregator", "" + mojoDescriptor.isAggregator() ); |
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | 1 | PluginUtils.element( w, "requiresOnline", "" + mojoDescriptor.isOnlineRequired() ); |
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | 1 | PluginUtils.element( w, "inheritedByDefault", "" + mojoDescriptor.isInheritedByDefault() ); |
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | 1 | if ( mojoDescriptor.getPhase() != null ) |
209 | |
{ |
210 | 0 | PluginUtils.element( w, "phase", mojoDescriptor.getPhase() ); |
211 | |
} |
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | 1 | if ( mojoDescriptor.getExecutePhase() != null ) |
218 | |
{ |
219 | 0 | PluginUtils.element( w, "executePhase", mojoDescriptor.getExecutePhase() ); |
220 | |
} |
221 | |
|
222 | 1 | if ( mojoDescriptor.getExecuteGoal() != null ) |
223 | |
{ |
224 | 0 | PluginUtils.element( w, "executeGoal", mojoDescriptor.getExecuteGoal() ); |
225 | |
} |
226 | |
|
227 | 1 | if ( mojoDescriptor.getExecuteLifecycle() != null ) |
228 | |
{ |
229 | 0 | PluginUtils.element( w, "executeLifecycle", mojoDescriptor.getExecuteLifecycle() ); |
230 | |
} |
231 | |
|
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | 1 | w.startElement( "implementation" ); |
237 | 1 | w.writeText( mojoDescriptor.getImplementation() ); |
238 | 1 | w.endElement(); |
239 | |
|
240 | |
|
241 | |
|
242 | |
|
243 | |
|
244 | 1 | w.startElement( "language" ); |
245 | 1 | w.writeText( mojoDescriptor.getLanguage() ); |
246 | 1 | w.endElement(); |
247 | |
|
248 | |
|
249 | |
|
250 | |
|
251 | |
|
252 | 1 | if ( mojoDescriptor.getComponentConfigurator() != null ) |
253 | |
{ |
254 | 0 | w.startElement( "configurator" ); |
255 | 0 | w.writeText( mojoDescriptor.getComponentConfigurator() ); |
256 | 0 | w.endElement(); |
257 | |
} |
258 | |
|
259 | |
|
260 | |
|
261 | |
|
262 | |
|
263 | 1 | if ( mojoDescriptor.getComponentComposer() != null ) |
264 | |
{ |
265 | 0 | w.startElement( "composer" ); |
266 | 0 | w.writeText( mojoDescriptor.getComponentComposer() ); |
267 | 0 | w.endElement(); |
268 | |
} |
269 | |
|
270 | |
|
271 | |
|
272 | |
|
273 | |
|
274 | 1 | w.startElement( "instantiationStrategy" ); |
275 | 1 | w.writeText( mojoDescriptor.getInstantiationStrategy() ); |
276 | 1 | w.endElement(); |
277 | |
|
278 | |
|
279 | |
|
280 | |
|
281 | |
|
282 | 1 | w.startElement( "executionStrategy" ); |
283 | 1 | w.writeText( mojoDescriptor.getExecutionStrategy() ); |
284 | 1 | w.endElement(); |
285 | |
|
286 | |
|
287 | |
|
288 | |
|
289 | |
|
290 | 1 | if ( mojoDescriptor.getDeprecated() != null ) |
291 | |
{ |
292 | 0 | w.startElement( "deprecated" ); |
293 | |
|
294 | 0 | if ( StringUtils.isEmpty( mojoDescriptor.getDeprecated() ) ) |
295 | |
{ |
296 | 0 | w.writeText( "No reason given" ); |
297 | |
} |
298 | |
else |
299 | |
{ |
300 | 0 | w.writeText( mojoDescriptor.getDeprecated() ); |
301 | |
} |
302 | |
|
303 | 0 | w.endElement(); |
304 | |
} |
305 | |
|
306 | |
|
307 | |
|
308 | |
|
309 | |
|
310 | 1 | if ( mojoDescriptor instanceof ExtendedMojoDescriptor ) |
311 | |
{ |
312 | 0 | ExtendedMojoDescriptor extendedMojoDescriptor = (ExtendedMojoDescriptor) mojoDescriptor; |
313 | 0 | if ( extendedMojoDescriptor.getDependencyCollectionRequired() != null ) |
314 | |
{ |
315 | 0 | PluginUtils.element( w, "requiresDependencyCollection", |
316 | |
extendedMojoDescriptor.getDependencyCollectionRequired() ); |
317 | |
} |
318 | |
|
319 | 0 | PluginUtils.element( w, "threadSafe", "" + extendedMojoDescriptor.isThreadSafe() ); |
320 | |
} |
321 | |
|
322 | |
|
323 | |
|
324 | |
|
325 | |
|
326 | |
@SuppressWarnings( "unchecked" ) |
327 | 1 | List<Parameter> parameters = mojoDescriptor.getParameters(); |
328 | |
|
329 | 1 | w.startElement( "parameters" ); |
330 | |
|
331 | 1 | Map<String, Requirement> requirements = new LinkedHashMap<String, Requirement>(); |
332 | |
|
333 | 1 | Set<Parameter> configuration = new LinkedHashSet<Parameter>(); |
334 | |
|
335 | 1 | if ( parameters != null ) |
336 | |
{ |
337 | 1 | for ( Parameter parameter : parameters ) |
338 | |
{ |
339 | 1 | String expression = parameter.getExpression(); |
340 | |
|
341 | 1 | if ( StringUtils.isNotEmpty( expression ) && expression.startsWith( "${component." ) ) |
342 | |
{ |
343 | |
|
344 | |
|
345 | |
|
346 | 0 | String role = expression.substring( "${component.".length(), expression.length() - 1 ); |
347 | |
|
348 | 0 | String roleHint = null; |
349 | |
|
350 | 0 | int posRoleHintSeparator = role.indexOf( "#" ); |
351 | 0 | if ( posRoleHintSeparator > 0 ) |
352 | |
{ |
353 | 0 | roleHint = role.substring( posRoleHintSeparator + 1 ); |
354 | |
|
355 | 0 | role = role.substring( 0, posRoleHintSeparator ); |
356 | |
} |
357 | |
|
358 | |
|
359 | 0 | requirements.put( parameter.getName(), new Requirement( role, roleHint ) ); |
360 | 0 | } |
361 | 1 | else if ( parameter.getRequirement() != null ) |
362 | |
{ |
363 | 0 | requirements.put( parameter.getName(), parameter.getRequirement() ); |
364 | |
} |
365 | |
else |
366 | |
{ |
367 | |
|
368 | |
|
369 | 1 | w.startElement( "parameter" ); |
370 | |
|
371 | 1 | PluginUtils.element( w, "name", parameter.getName() ); |
372 | |
|
373 | 1 | if ( parameter.getAlias() != null ) |
374 | |
{ |
375 | 0 | PluginUtils.element( w, "alias", parameter.getAlias() ); |
376 | |
} |
377 | |
|
378 | 1 | PluginUtils.element( w, "type", parameter.getType() ); |
379 | |
|
380 | 1 | if ( parameter.getDeprecated() != null ) |
381 | |
{ |
382 | 0 | if ( StringUtils.isEmpty( parameter.getDeprecated() ) ) |
383 | |
{ |
384 | 0 | PluginUtils.element( w, "deprecated", "No reason given" ); |
385 | |
} |
386 | |
else |
387 | |
{ |
388 | 0 | PluginUtils.element( w, "deprecated", parameter.getDeprecated() ); |
389 | |
} |
390 | |
} |
391 | |
|
392 | 1 | if ( parameter.getImplementation() != null ) |
393 | |
{ |
394 | 0 | PluginUtils.element( w, "implementation", parameter.getImplementation() ); |
395 | |
} |
396 | |
|
397 | 1 | PluginUtils.element( w, "required", Boolean.toString( parameter.isRequired() ) ); |
398 | |
|
399 | 1 | PluginUtils.element( w, "editable", Boolean.toString( parameter.isEditable() ) ); |
400 | |
|
401 | 1 | PluginUtils.element( w, "description", parameter.getDescription() ); |
402 | |
|
403 | 1 | if ( StringUtils.isNotEmpty( parameter.getDefaultValue() ) |
404 | |
|| StringUtils.isNotEmpty( parameter.getExpression() ) ) |
405 | |
{ |
406 | 1 | configuration.add( parameter ); |
407 | |
} |
408 | |
|
409 | 1 | w.endElement(); |
410 | |
} |
411 | |
|
412 | 1 | } |
413 | |
} |
414 | |
|
415 | 1 | w.endElement(); |
416 | |
|
417 | |
|
418 | |
|
419 | |
|
420 | |
|
421 | 1 | if ( !configuration.isEmpty() ) |
422 | |
{ |
423 | 1 | w.startElement( "configuration" ); |
424 | |
|
425 | 1 | for ( Parameter parameter : configuration ) |
426 | |
{ |
427 | 1 | w.startElement( parameter.getName() ); |
428 | |
|
429 | 1 | String type = parameter.getType(); |
430 | 1 | if ( type != null ) |
431 | |
{ |
432 | 1 | w.addAttribute( "implementation", type ); |
433 | |
} |
434 | |
|
435 | 1 | if ( parameter.getDefaultValue() != null ) |
436 | |
{ |
437 | 1 | w.addAttribute( "default-value", parameter.getDefaultValue() ); |
438 | |
} |
439 | |
|
440 | 1 | if ( parameter.getExpression() != null ) |
441 | |
{ |
442 | 1 | w.writeText( parameter.getExpression() ); |
443 | |
} |
444 | |
|
445 | 1 | w.endElement(); |
446 | 1 | } |
447 | |
|
448 | 1 | w.endElement(); |
449 | |
} |
450 | |
|
451 | |
|
452 | |
|
453 | |
|
454 | |
|
455 | 1 | if ( !requirements.isEmpty() ) |
456 | |
{ |
457 | 0 | w.startElement( "requirements" ); |
458 | |
|
459 | 0 | for ( Map.Entry<String, Requirement> entry : requirements.entrySet() ) |
460 | |
{ |
461 | 0 | String key = entry.getKey(); |
462 | 0 | Requirement requirement = entry.getValue(); |
463 | |
|
464 | 0 | w.startElement( "requirement" ); |
465 | |
|
466 | 0 | PluginUtils.element( w, "role", requirement.getRole() ); |
467 | |
|
468 | 0 | if ( requirement.getRoleHint() != null ) |
469 | |
{ |
470 | 0 | PluginUtils.element( w, "role-hint", requirement.getRoleHint() ); |
471 | |
} |
472 | |
|
473 | 0 | PluginUtils.element( w, "field-name", key ); |
474 | |
|
475 | 0 | w.endElement(); |
476 | 0 | } |
477 | |
|
478 | 0 | w.endElement(); |
479 | |
} |
480 | |
|
481 | 1 | w.endElement(); |
482 | 1 | } |
483 | |
} |