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.codehaus.plexus.util.IOUtil; |
26 |
|
import org.codehaus.plexus.util.StringUtils; |
27 |
|
import org.codehaus.plexus.util.xml.XMLWriter; |
28 |
|
|
29 |
|
import java.io.File; |
30 |
|
import java.io.FileWriter; |
31 |
|
import java.io.IOException; |
32 |
|
import java.util.ArrayList; |
33 |
|
import java.util.Iterator; |
34 |
|
import java.util.List; |
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
1 |
public class PluginXdocGenerator |
40 |
|
implements Generator |
41 |
|
{ |
42 |
|
|
43 |
|
public void execute( File destinationDirectory, PluginDescriptor pluginDescriptor ) |
44 |
|
throws IOException |
45 |
|
{ |
46 |
1 |
for ( Iterator it = pluginDescriptor.getMojos().iterator(); it.hasNext(); ) |
47 |
|
{ |
48 |
1 |
MojoDescriptor descriptor = (MojoDescriptor) it.next(); |
49 |
1 |
processMojoDescriptor( descriptor, destinationDirectory ); |
50 |
1 |
} |
51 |
1 |
} |
52 |
|
|
53 |
|
protected void processMojoDescriptor( MojoDescriptor mojoDescriptor, File destinationDirectory ) |
54 |
|
throws IOException |
55 |
|
{ |
56 |
1 |
FileWriter writer = null; |
57 |
|
try |
58 |
|
{ |
59 |
1 |
writer = new FileWriter( new File( destinationDirectory, getMojoFilename( mojoDescriptor, "xml" ) ) ); |
60 |
|
|
61 |
1 |
writeBody( writer, mojoDescriptor ); |
62 |
|
|
63 |
1 |
writer.flush(); |
64 |
|
} |
65 |
|
finally |
66 |
|
{ |
67 |
1 |
IOUtil.close( writer ); |
68 |
1 |
} |
69 |
1 |
} |
70 |
|
|
71 |
|
private String getMojoFilename( MojoDescriptor mojo, String ext ) |
72 |
|
{ |
73 |
1 |
return mojo.getGoal() + "-mojo." + ext; |
74 |
|
} |
75 |
|
|
76 |
|
private void writeBody( FileWriter writer, MojoDescriptor mojoDescriptor ) |
77 |
|
{ |
78 |
1 |
XMLWriter w = new PrettyPrintXMLWriter( writer ); |
79 |
|
|
80 |
1 |
w.startElement( "document" ); |
81 |
|
|
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
|
86 |
1 |
w.startElement( "properties" ); |
87 |
|
|
88 |
1 |
w.startElement( "title" ); |
89 |
|
|
90 |
|
|
91 |
1 |
w.writeText( mojoDescriptor.getPluginDescriptor().getArtifactId() + " - " + mojoDescriptor.getFullGoalName() ); |
92 |
|
|
93 |
1 |
w.endElement(); |
94 |
|
|
95 |
1 |
w.endElement(); |
96 |
|
|
97 |
|
|
98 |
|
|
99 |
|
|
100 |
|
|
101 |
1 |
w.startElement( "body" ); |
102 |
|
|
103 |
1 |
w.startElement( "section" ); |
104 |
|
|
105 |
1 |
w.addAttribute( "name", mojoDescriptor.getFullGoalName() ); |
106 |
|
|
107 |
1 |
w.startElement( "p" ); |
108 |
|
|
109 |
1 |
if ( mojoDescriptor.getDescription() != null ) |
110 |
|
{ |
111 |
0 |
w.writeMarkup( mojoDescriptor.getDescription() ); |
112 |
0 |
} |
113 |
|
else |
114 |
|
{ |
115 |
1 |
w.writeText( "No description." ); |
116 |
|
} |
117 |
|
|
118 |
1 |
w.endElement(); |
119 |
|
|
120 |
1 |
writeGoalAttributes( mojoDescriptor, w ); |
121 |
|
|
122 |
1 |
writeGoalParameterTable( mojoDescriptor, w ); |
123 |
|
|
124 |
1 |
w.endElement(); |
125 |
|
|
126 |
1 |
w.endElement(); |
127 |
|
|
128 |
1 |
w.endElement(); |
129 |
1 |
} |
130 |
|
|
131 |
|
private void writeGoalAttributes( MojoDescriptor mojoDescriptor, XMLWriter w ) |
132 |
|
{ |
133 |
1 |
w.startElement( "p" ); |
134 |
1 |
w.writeMarkup( "<b>Mojo Attributes</b>:" ); |
135 |
1 |
w.startElement( "ul" ); |
136 |
|
|
137 |
1 |
String value = mojoDescriptor.getDeprecated(); |
138 |
1 |
if ( StringUtils.isNotEmpty( value ) ) |
139 |
|
{ |
140 |
0 |
w.startElement( "li" ); |
141 |
0 |
w.writeMarkup( "This plugin goal has been deprecated: " + value + "" ); |
142 |
0 |
w.endElement(); |
143 |
|
} |
144 |
|
|
145 |
1 |
if ( mojoDescriptor.isProjectRequired() ) |
146 |
|
{ |
147 |
1 |
w.startElement( "li" ); |
148 |
1 |
w.writeMarkup( "Requires a Maven 2.0 project to execute." ); |
149 |
1 |
w.endElement(); |
150 |
|
} |
151 |
|
|
152 |
1 |
if ( mojoDescriptor.isAggregator() ) |
153 |
|
{ |
154 |
0 |
w.startElement( "li" ); |
155 |
0 |
w.writeMarkup( "Executes as an aggregator plugin." ); |
156 |
0 |
w.endElement(); |
157 |
|
} |
158 |
|
|
159 |
1 |
if ( mojoDescriptor.isDirectInvocationOnly() ) |
160 |
|
{ |
161 |
0 |
w.startElement( "li" ); |
162 |
0 |
w.writeMarkup( "Executes by direct invocation only." ); |
163 |
0 |
w.endElement(); |
164 |
|
} |
165 |
|
|
166 |
1 |
value = mojoDescriptor.isDependencyResolutionRequired(); |
167 |
1 |
if ( StringUtils.isNotEmpty( value ) ) |
168 |
|
{ |
169 |
1 |
w.startElement( "li" ); |
170 |
1 |
w.writeMarkup( "Requires dependency resolution of artifacts in scope: <code>" + value + "</code>" ); |
171 |
1 |
w.endElement(); |
172 |
|
} |
173 |
|
|
174 |
1 |
value = mojoDescriptor.getPhase(); |
175 |
1 |
if ( StringUtils.isNotEmpty( value ) ) |
176 |
|
{ |
177 |
0 |
w.startElement( "li" ); |
178 |
0 |
w.writeMarkup( "Automatically executes within the lifecycle phase: <code>" + value + "</code>" ); |
179 |
0 |
w.endElement(); |
180 |
|
} |
181 |
|
|
182 |
1 |
value = mojoDescriptor.getExecutePhase(); |
183 |
1 |
if ( StringUtils.isNotEmpty( value ) ) |
184 |
|
{ |
185 |
0 |
w.startElement( "li" ); |
186 |
0 |
w.writeMarkup( |
187 |
|
"Invokes the execution of the lifecycle phase <code>" + value + "</code> prior to executing itself." ); |
188 |
0 |
w.endElement(); |
189 |
|
} |
190 |
|
|
191 |
1 |
value = mojoDescriptor.getExecuteGoal(); |
192 |
1 |
if ( StringUtils.isNotEmpty( value ) ) |
193 |
|
{ |
194 |
0 |
w.startElement( "li" ); |
195 |
0 |
w.writeMarkup( |
196 |
|
"Invokes the execution of this plugin's goal <code>" + value + "</code> prior to executing itself." ); |
197 |
0 |
w.endElement(); |
198 |
|
} |
199 |
|
|
200 |
1 |
value = mojoDescriptor.getExecuteLifecycle(); |
201 |
1 |
if ( StringUtils.isNotEmpty( value ) ) |
202 |
|
{ |
203 |
0 |
w.startElement( "li" ); |
204 |
0 |
w.writeMarkup( "Executes in its own lifecycle: <code>" + value + "</code>" ); |
205 |
0 |
w.endElement(); |
206 |
|
} |
207 |
|
|
208 |
1 |
if ( mojoDescriptor.isOnlineRequired() ) |
209 |
|
{ |
210 |
0 |
w.startElement( "li" ); |
211 |
0 |
w.writeMarkup( "Requires that mvn runs in online mode." ); |
212 |
0 |
w.endElement(); |
213 |
|
} |
214 |
|
|
215 |
1 |
if ( !mojoDescriptor.isInheritedByDefault() ) |
216 |
|
{ |
217 |
0 |
w.startElement( "li" ); |
218 |
0 |
w.writeMarkup( "Is NOT inherited by default in multi-project builds." ); |
219 |
0 |
w.endElement(); |
220 |
|
} |
221 |
|
|
222 |
1 |
w.endElement(); |
223 |
1 |
w.endElement(); |
224 |
1 |
} |
225 |
|
|
226 |
|
private void writeGoalParameterTable( MojoDescriptor mojoDescriptor, XMLWriter w ) |
227 |
|
{ |
228 |
1 |
List parameterList = mojoDescriptor.getParameters(); |
229 |
|
|
230 |
|
|
231 |
1 |
List list = filterParameters( parameterList ); |
232 |
|
|
233 |
1 |
if ( list != null && list.size() > 0 ) |
234 |
|
{ |
235 |
1 |
writeParameterSummary( list, w ); |
236 |
|
|
237 |
1 |
writeParameterDetails( list, w ); |
238 |
|
} |
239 |
1 |
} |
240 |
|
|
241 |
|
private List filterParameters( List parameterList ) |
242 |
|
{ |
243 |
1 |
List filtered = new ArrayList(); |
244 |
|
|
245 |
1 |
for ( Iterator parameters = parameterList.iterator(); parameters.hasNext(); ) |
246 |
|
{ |
247 |
1 |
Parameter parameter = (Parameter) parameters.next(); |
248 |
|
|
249 |
1 |
if ( parameter.isEditable() ) |
250 |
|
{ |
251 |
1 |
String expression = parameter.getExpression(); |
252 |
|
|
253 |
1 |
if ( expression == null || !expression.startsWith( "${component." ) ) |
254 |
|
{ |
255 |
1 |
filtered.add( parameter ); |
256 |
|
} |
257 |
|
} |
258 |
1 |
} |
259 |
|
|
260 |
1 |
return filtered; |
261 |
|
} |
262 |
|
|
263 |
|
private void writeParameterDetails( List parameterList, XMLWriter w ) |
264 |
|
{ |
265 |
1 |
w.startElement( "subsection" ); |
266 |
1 |
w.addAttribute( "name", "Parameter Details" ); |
267 |
|
|
268 |
1 |
for ( Iterator parameters = parameterList.iterator(); parameters.hasNext(); ) |
269 |
|
{ |
270 |
1 |
Parameter parameter = (Parameter) parameters.next(); |
271 |
|
|
272 |
1 |
w.startElement( "p" ); |
273 |
1 |
w.writeMarkup( "<b><a name=\"" + parameter.getName() + "\">" + parameter.getName() + "</a></b>" ); |
274 |
1 |
w.endElement(); |
275 |
|
|
276 |
1 |
String description = parameter.getDescription(); |
277 |
1 |
if ( StringUtils.isEmpty( description ) ) |
278 |
|
{ |
279 |
0 |
description = "No Description."; |
280 |
|
} |
281 |
1 |
w.startElement( "p" ); |
282 |
1 |
w.writeMarkup( description ); |
283 |
1 |
w.endElement(); |
284 |
|
|
285 |
1 |
w.startElement( "ul" ); |
286 |
|
|
287 |
1 |
writeDetail( "Type", parameter.getType(), w ); |
288 |
|
|
289 |
1 |
if ( parameter.isRequired() ) |
290 |
|
{ |
291 |
1 |
writeDetail( "Required", "Yes", w ); |
292 |
1 |
} |
293 |
|
else |
294 |
|
{ |
295 |
0 |
writeDetail( "Required", "No", w ); |
296 |
|
} |
297 |
|
|
298 |
1 |
writeDetail( "Expression", parameter.getExpression(), w ); |
299 |
|
|
300 |
1 |
writeDetail( "Default", parameter.getDefaultValue(), w ); |
301 |
|
|
302 |
1 |
w.endElement(); |
303 |
|
|
304 |
1 |
if ( parameters.hasNext() ) |
305 |
|
{ |
306 |
0 |
w.writeMarkup( "<hr/>" ); |
307 |
|
} |
308 |
1 |
} |
309 |
|
|
310 |
1 |
w.endElement(); |
311 |
1 |
} |
312 |
|
|
313 |
|
private void writeDetail( String param, String value, XMLWriter w ) |
314 |
|
{ |
315 |
4 |
if ( StringUtils.isNotEmpty( value ) ) |
316 |
|
{ |
317 |
3 |
w.startElement( "li" ); |
318 |
3 |
w.writeMarkup( "<b>" + param + "</b>: <code>" ); |
319 |
3 |
w.writeText( value ); |
320 |
3 |
w.writeMarkup( "</code>" ); |
321 |
3 |
w.endElement(); |
322 |
|
} |
323 |
4 |
} |
324 |
|
|
325 |
|
private void writeParameterSummary( List parameterList, XMLWriter w ) |
326 |
|
{ |
327 |
1 |
List requiredParams = getParametersByRequired( true, parameterList ); |
328 |
1 |
if ( requiredParams.size() > 0 ) |
329 |
|
{ |
330 |
1 |
writeParameterList( "Required Parameters", requiredParams, w ); |
331 |
|
} |
332 |
|
|
333 |
1 |
List optionalParams = getParametersByRequired( false, parameterList ); |
334 |
1 |
if ( optionalParams.size() > 0 ) |
335 |
|
{ |
336 |
0 |
writeParameterList( "Optional Parameters", optionalParams, w ); |
337 |
|
} |
338 |
1 |
} |
339 |
|
|
340 |
|
private void writeParameterList( String title, List parameterList, XMLWriter w ) |
341 |
|
{ |
342 |
1 |
w.startElement( "subsection" ); |
343 |
1 |
w.addAttribute( "name", title ); |
344 |
|
|
345 |
1 |
w.startElement( "table" ); |
346 |
|
|
347 |
1 |
w.startElement( "tr" ); |
348 |
1 |
w.startElement( "th" ); |
349 |
1 |
w.writeText( "Name" ); |
350 |
1 |
w.endElement(); |
351 |
1 |
w.startElement( "th" ); |
352 |
1 |
w.writeText( "Type" ); |
353 |
1 |
w.endElement(); |
354 |
1 |
w.startElement( "th" ); |
355 |
1 |
w.writeText( "Description" ); |
356 |
1 |
w.endElement(); |
357 |
1 |
w.endElement(); |
358 |
|
|
359 |
1 |
for ( Iterator parameters = parameterList.iterator(); parameters.hasNext(); ) |
360 |
|
{ |
361 |
1 |
Parameter parameter = (Parameter) parameters.next(); |
362 |
|
|
363 |
1 |
w.startElement( "tr" ); |
364 |
1 |
w.startElement( "td" ); |
365 |
1 |
w.writeMarkup( "<b><a href=\"#" + parameter.getName() + "\">" + parameter.getName() + "</a></b>" ); |
366 |
1 |
w.endElement(); |
367 |
1 |
w.startElement( "td" ); |
368 |
1 |
int index = parameter.getType().lastIndexOf( "." ); |
369 |
1 |
w.writeMarkup( "<code>" + parameter.getType().substring( index + 1 ) + "</code>" ); |
370 |
1 |
w.endElement(); |
371 |
1 |
w.startElement( "td" ); |
372 |
1 |
String description = parameter.getDescription(); |
373 |
1 |
if ( StringUtils.isEmpty( description ) ) |
374 |
|
{ |
375 |
0 |
description = "No description."; |
376 |
|
} |
377 |
1 |
w.writeMarkup( description ); |
378 |
|
|
379 |
1 |
if ( StringUtils.isNotEmpty( parameter.getDefaultValue() ) ) |
380 |
|
{ |
381 |
0 |
w.writeMarkup( " Default value is <code>" ); |
382 |
0 |
w.writeText( parameter.getDefaultValue() ); |
383 |
0 |
w.writeMarkup( "</code>." ); |
384 |
|
} |
385 |
1 |
w.endElement(); |
386 |
1 |
w.endElement(); |
387 |
1 |
} |
388 |
|
|
389 |
1 |
w.endElement(); |
390 |
1 |
w.endElement(); |
391 |
1 |
} |
392 |
|
|
393 |
|
private List getParametersByRequired( boolean required, List parameterList ) |
394 |
|
{ |
395 |
2 |
List list = new ArrayList(); |
396 |
|
|
397 |
2 |
for ( Iterator parameters = parameterList.iterator(); parameters.hasNext(); ) |
398 |
|
{ |
399 |
2 |
Parameter parameter = (Parameter) parameters.next(); |
400 |
|
|
401 |
2 |
if ( parameter.isRequired() == required ) |
402 |
|
{ |
403 |
1 |
list.add( parameter ); |
404 |
|
} |
405 |
2 |
} |
406 |
|
|
407 |
2 |
return list; |
408 |
|
} |
409 |
|
} |
410 |
|
|