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