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