1 | |
package org.apache.maven.tools.plugin.extractor.java; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import com.thoughtworks.qdox.JavaDocBuilder; |
23 | |
import com.thoughtworks.qdox.model.DocletTag; |
24 | |
import com.thoughtworks.qdox.model.JavaClass; |
25 | |
import com.thoughtworks.qdox.model.JavaField; |
26 | |
import com.thoughtworks.qdox.model.Type; |
27 | |
|
28 | |
import org.apache.maven.plugin.descriptor.InvalidParameterException; |
29 | |
import org.apache.maven.plugin.descriptor.InvalidPluginDescriptorException; |
30 | |
import org.apache.maven.plugin.descriptor.MojoDescriptor; |
31 | |
import org.apache.maven.plugin.descriptor.Parameter; |
32 | |
import org.apache.maven.plugin.descriptor.PluginDescriptor; |
33 | |
import org.apache.maven.plugin.descriptor.Requirement; |
34 | |
import org.apache.maven.project.MavenProject; |
35 | |
import org.apache.maven.tools.plugin.DefaultPluginToolsRequest; |
36 | |
import org.apache.maven.tools.plugin.ExtendedMojoDescriptor; |
37 | |
import org.apache.maven.tools.plugin.PluginToolsRequest; |
38 | |
import org.apache.maven.tools.plugin.extractor.MojoDescriptorExtractor; |
39 | |
import org.apache.maven.tools.plugin.extractor.ExtractionException; |
40 | |
import org.apache.maven.tools.plugin.util.PluginUtils; |
41 | |
|
42 | |
import org.codehaus.plexus.component.annotations.Component; |
43 | |
import org.codehaus.plexus.logging.AbstractLogEnabled; |
44 | |
import org.codehaus.plexus.util.StringUtils; |
45 | |
|
46 | |
import java.io.File; |
47 | |
import java.util.ArrayList; |
48 | |
import java.util.List; |
49 | |
import java.util.Map; |
50 | |
import java.util.TreeMap; |
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
@Component( role = MojoDescriptorExtractor.class, hint = "java" ) |
65 | 10 | public class JavaMojoDescriptorExtractor |
66 | |
extends AbstractLogEnabled |
67 | |
implements MojoDescriptorExtractor, JavaMojoAnnotation |
68 | |
{ |
69 | |
|
70 | |
public static final String MAVEN_PLUGIN_INSTANTIATION = JavaMojoAnnotation.INSTANTIATION_STRATEGY; |
71 | |
|
72 | |
|
73 | |
public static final String CONFIGURATOR = JavaMojoAnnotation.CONFIGURATOR; |
74 | |
|
75 | |
|
76 | |
public static final String PARAMETER = JavaMojoAnnotation.PARAMETER; |
77 | |
|
78 | |
|
79 | |
public static final String PARAMETER_EXPRESSION = JavaMojoAnnotation.PARAMETER_EXPRESSION; |
80 | |
|
81 | |
|
82 | |
public static final String PARAMETER_DEFAULT_VALUE = JavaMojoAnnotation.PARAMETER_DEFAULT_VALUE; |
83 | |
|
84 | |
|
85 | |
public static final String PARAMETER_ALIAS = JavaMojoAnnotation.PARAMETER_ALIAS; |
86 | |
|
87 | |
|
88 | |
public static final String SINCE = JavaMojoAnnotation.SINCE; |
89 | |
|
90 | |
|
91 | |
public static final String PARAMETER_IMPLEMENTATION = JavaMojoAnnotation.PARAMETER_IMPLEMENTATION; |
92 | |
|
93 | |
|
94 | |
public static final String REQUIRED = JavaMojoAnnotation.REQUIRED; |
95 | |
|
96 | |
|
97 | |
public static final String DEPRECATED = JavaMojoAnnotation.DEPRECATED; |
98 | |
|
99 | |
|
100 | |
public static final String READONLY = JavaMojoAnnotation.READONLY; |
101 | |
|
102 | |
|
103 | |
public static final String GOAL = JavaMojoAnnotation.GOAL; |
104 | |
|
105 | |
|
106 | |
public static final String PHASE = JavaMojoAnnotation.PHASE; |
107 | |
|
108 | |
|
109 | |
public static final String EXECUTE = JavaMojoAnnotation.EXECUTE; |
110 | |
|
111 | |
|
112 | |
public static final String EXECUTE_LIFECYCLE = JavaMojoAnnotation.EXECUTE_LIFECYCLE; |
113 | |
|
114 | |
|
115 | |
public static final String EXECUTE_PHASE = JavaMojoAnnotation.EXECUTE_PHASE; |
116 | |
|
117 | |
|
118 | |
public static final String EXECUTE_GOAL = JavaMojoAnnotation.EXECUTE_GOAL; |
119 | |
|
120 | |
|
121 | |
public static final String GOAL_DESCRIPTION = JavaMojoAnnotation.DESCRIPTION; |
122 | |
|
123 | |
|
124 | |
public static final String GOAL_REQUIRES_DEPENDENCY_RESOLUTION = JavaMojoAnnotation.REQUIRES_DEPENDENCY_RESOLUTION; |
125 | |
|
126 | |
|
127 | |
public static final String GOAL_REQUIRES_PROJECT = JavaMojoAnnotation.REQUIRES_PROJECT; |
128 | |
|
129 | |
|
130 | |
public static final String GOAL_REQUIRES_REPORTS = JavaMojoAnnotation.REQUIRES_REPORTS; |
131 | |
|
132 | |
|
133 | |
public static final String GOAL_IS_AGGREGATOR = JavaMojoAnnotation.AGGREGATOR; |
134 | |
|
135 | |
|
136 | |
public static final String GOAL_REQUIRES_ONLINE = JavaMojoAnnotation.REQUIRES_ONLINE; |
137 | |
|
138 | |
|
139 | |
public static final String GOAL_INHERIT_BY_DEFAULT = JavaMojoAnnotation.INHERIT_BY_DEFAULT; |
140 | |
|
141 | |
|
142 | |
public static final String GOAL_MULTI_EXECUTION_STRATEGY = JavaMojoAnnotation.MULTI_EXECUTION_STRATEGY; |
143 | |
|
144 | |
|
145 | |
public static final String GOAL_REQUIRES_DIRECT_INVOCATION = JavaMojoAnnotation.REQUIRES_DIRECT_INVOCATION; |
146 | |
|
147 | |
|
148 | |
public static final String COMPONENT = JavaMojoAnnotation.COMPONENT; |
149 | |
|
150 | |
|
151 | |
public static final String COMPONENT_ROLE = JavaMojoAnnotation.COMPONENT_ROLE; |
152 | |
|
153 | |
|
154 | |
public static final String COMPONENT_ROLEHINT = JavaMojoAnnotation.COMPONENT_ROLEHINT; |
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
protected void validateParameter( Parameter parameter, int i ) |
162 | |
throws InvalidParameterException |
163 | |
{ |
164 | |
|
165 | 16 | String name = parameter.getName(); |
166 | |
|
167 | 16 | if ( name == null ) |
168 | |
{ |
169 | 0 | throw new InvalidParameterException( "name", i ); |
170 | |
} |
171 | |
|
172 | |
|
173 | 16 | String type = parameter.getType(); |
174 | |
|
175 | 16 | if ( type == null ) |
176 | |
{ |
177 | 0 | throw new InvalidParameterException( "type", i ); |
178 | |
} |
179 | |
|
180 | |
|
181 | 16 | String description = parameter.getDescription(); |
182 | |
|
183 | 16 | if ( description == null ) |
184 | |
{ |
185 | 0 | throw new InvalidParameterException( "description", i ); |
186 | |
} |
187 | 16 | } |
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
protected MojoDescriptor createMojoDescriptor( JavaClass javaClass ) |
199 | |
throws InvalidPluginDescriptorException |
200 | |
{ |
201 | 10 | ExtendedMojoDescriptor mojoDescriptor = new ExtendedMojoDescriptor(); |
202 | 10 | mojoDescriptor.setLanguage( "java" ); |
203 | 10 | mojoDescriptor.setImplementation( javaClass.getFullyQualifiedName() ); |
204 | 10 | mojoDescriptor.setDescription( javaClass.getComment() ); |
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | 10 | DocletTag aggregator = findInClassHierarchy( javaClass, JavaMojoAnnotation.AGGREGATOR ); |
212 | 10 | if ( aggregator != null ) |
213 | |
{ |
214 | 2 | mojoDescriptor.setAggregator( true ); |
215 | |
} |
216 | |
|
217 | |
|
218 | 10 | DocletTag configurator = findInClassHierarchy( javaClass, JavaMojoAnnotation.CONFIGURATOR ); |
219 | 10 | if ( configurator != null ) |
220 | |
{ |
221 | 2 | mojoDescriptor.setComponentConfigurator( configurator.getValue() ); |
222 | |
} |
223 | |
|
224 | |
|
225 | 10 | DocletTag execute = findInClassHierarchy( javaClass, JavaMojoAnnotation.EXECUTE ); |
226 | 10 | if ( execute != null ) |
227 | |
{ |
228 | 2 | String executePhase = execute.getNamedParameter( JavaMojoAnnotation.EXECUTE_PHASE ); |
229 | 2 | String executeGoal = execute.getNamedParameter( JavaMojoAnnotation.EXECUTE_GOAL ); |
230 | |
|
231 | 2 | if ( executePhase == null && executeGoal == null ) |
232 | |
{ |
233 | 0 | throw new InvalidPluginDescriptorException( javaClass.getFullyQualifiedName() |
234 | |
+ ": @execute tag requires either a 'phase' or 'goal' parameter" ); |
235 | |
} |
236 | 2 | else if ( executePhase != null && executeGoal != null ) |
237 | |
{ |
238 | 0 | throw new InvalidPluginDescriptorException( javaClass.getFullyQualifiedName() |
239 | |
+ ": @execute tag can have only one of a 'phase' or 'goal' parameter" ); |
240 | |
} |
241 | 2 | mojoDescriptor.setExecutePhase( executePhase ); |
242 | 2 | mojoDescriptor.setExecuteGoal( executeGoal ); |
243 | |
|
244 | 2 | String lifecycle = execute.getNamedParameter( JavaMojoAnnotation.EXECUTE_LIFECYCLE ); |
245 | 2 | if ( lifecycle != null ) |
246 | |
{ |
247 | 2 | mojoDescriptor.setExecuteLifecycle( lifecycle ); |
248 | 2 | if ( mojoDescriptor.getExecuteGoal() != null ) |
249 | |
{ |
250 | 0 | throw new InvalidPluginDescriptorException( javaClass.getFullyQualifiedName() |
251 | |
+ ": @execute lifecycle requires a phase instead of a goal" ); |
252 | |
} |
253 | |
} |
254 | |
} |
255 | |
|
256 | |
|
257 | 10 | DocletTag goal = findInClassHierarchy( javaClass, JavaMojoAnnotation.GOAL ); |
258 | 10 | if ( goal != null ) |
259 | |
{ |
260 | 10 | mojoDescriptor.setGoal( goal.getValue() ); |
261 | |
} |
262 | |
|
263 | |
|
264 | 10 | boolean value = |
265 | |
getBooleanTagValue( javaClass, JavaMojoAnnotation.INHERIT_BY_DEFAULT, |
266 | |
mojoDescriptor.isInheritedByDefault() ); |
267 | 10 | mojoDescriptor.setInheritedByDefault( value ); |
268 | |
|
269 | |
|
270 | 10 | DocletTag tag = findInClassHierarchy( javaClass, JavaMojoAnnotation.INSTANTIATION_STRATEGY ); |
271 | 10 | if ( tag != null ) |
272 | |
{ |
273 | 2 | mojoDescriptor.setInstantiationStrategy( tag.getValue() ); |
274 | |
} |
275 | |
|
276 | |
|
277 | 10 | tag = findInClassHierarchy( javaClass, JavaMojoAnnotation.MULTI_EXECUTION_STRATEGY ); |
278 | 10 | if ( tag != null ) |
279 | |
{ |
280 | 0 | getLogger().warn( "@" + JavaMojoAnnotation.MULTI_EXECUTION_STRATEGY + " in " |
281 | |
+ javaClass.getFullyQualifiedName() + " is deprecated: please use '@" |
282 | |
+ JavaMojoAnnotation.EXECUTION_STATEGY + " always' instead." ); |
283 | 0 | mojoDescriptor.setExecutionStrategy( MojoDescriptor.MULTI_PASS_EXEC_STRATEGY ); |
284 | |
} |
285 | |
else |
286 | |
{ |
287 | 10 | mojoDescriptor.setExecutionStrategy( MojoDescriptor.SINGLE_PASS_EXEC_STRATEGY ); |
288 | |
} |
289 | 10 | tag = findInClassHierarchy( javaClass, JavaMojoAnnotation.EXECUTION_STATEGY ); |
290 | 10 | if ( tag != null ) |
291 | |
{ |
292 | 2 | mojoDescriptor.setExecutionStrategy( tag.getValue() ); |
293 | |
} |
294 | |
|
295 | |
|
296 | 10 | DocletTag phase = findInClassHierarchy( javaClass, JavaMojoAnnotation.PHASE ); |
297 | 10 | if ( phase != null ) |
298 | |
{ |
299 | 2 | mojoDescriptor.setPhase( phase.getValue() ); |
300 | |
} |
301 | |
|
302 | |
|
303 | 10 | DocletTag requiresDependencyResolution = |
304 | |
findInClassHierarchy( javaClass, JavaMojoAnnotation.REQUIRES_DEPENDENCY_RESOLUTION ); |
305 | 10 | if ( requiresDependencyResolution != null ) |
306 | |
{ |
307 | 6 | String v = requiresDependencyResolution.getValue(); |
308 | |
|
309 | 6 | if ( StringUtils.isEmpty( v ) ) |
310 | |
{ |
311 | 0 | v = "runtime"; |
312 | |
} |
313 | |
|
314 | 6 | mojoDescriptor.setDependencyResolutionRequired( v ); |
315 | |
} |
316 | |
|
317 | |
|
318 | 10 | DocletTag requiresDependencyCollection = |
319 | |
findInClassHierarchy( javaClass, JavaMojoAnnotation.REQUIRES_DEPENDENCY_COLLECTION ); |
320 | 10 | if ( requiresDependencyCollection != null ) |
321 | |
{ |
322 | 6 | String v = requiresDependencyCollection.getValue(); |
323 | |
|
324 | 6 | if ( StringUtils.isEmpty( v ) ) |
325 | |
{ |
326 | 0 | v = "runtime"; |
327 | |
} |
328 | |
|
329 | 6 | mojoDescriptor.setDependencyCollectionRequired( v ); |
330 | |
} |
331 | |
|
332 | |
|
333 | 10 | value = |
334 | |
getBooleanTagValue( javaClass, JavaMojoAnnotation.REQUIRES_DIRECT_INVOCATION, |
335 | |
mojoDescriptor.isDirectInvocationOnly() ); |
336 | 10 | mojoDescriptor.setDirectInvocationOnly( value ); |
337 | |
|
338 | |
|
339 | 10 | value = |
340 | |
getBooleanTagValue( javaClass, JavaMojoAnnotation.REQUIRES_ONLINE, mojoDescriptor.isOnlineRequired() ); |
341 | 10 | mojoDescriptor.setOnlineRequired( value ); |
342 | |
|
343 | |
|
344 | 10 | value = |
345 | |
getBooleanTagValue( javaClass, JavaMojoAnnotation.REQUIRES_PROJECT, mojoDescriptor.isProjectRequired() ); |
346 | 10 | mojoDescriptor.setProjectRequired( value ); |
347 | |
|
348 | |
|
349 | 10 | value = |
350 | |
getBooleanTagValue( javaClass, JavaMojoAnnotation.REQUIRES_REPORTS, mojoDescriptor.isRequiresReports() ); |
351 | 10 | mojoDescriptor.setRequiresReports( value ); |
352 | |
|
353 | |
|
354 | |
|
355 | |
|
356 | |
|
357 | |
|
358 | 10 | DocletTag deprecated = javaClass.getTagByName( JavaMojoAnnotation.DEPRECATED ); |
359 | 10 | if ( deprecated != null ) |
360 | |
{ |
361 | 2 | mojoDescriptor.setDeprecated( deprecated.getValue() ); |
362 | |
} |
363 | |
|
364 | |
|
365 | 10 | DocletTag since = findInClassHierarchy( javaClass, JavaMojoAnnotation.SINCE ); |
366 | 10 | if ( since != null ) |
367 | |
{ |
368 | 2 | mojoDescriptor.setSince( since.getValue() ); |
369 | |
} |
370 | |
|
371 | |
|
372 | |
|
373 | 10 | value = getBooleanTagValue( javaClass, JavaMojoAnnotation.THREAD_SAFE, true, mojoDescriptor.isThreadSafe() ); |
374 | 10 | mojoDescriptor.setThreadSafe( value ); |
375 | |
|
376 | 10 | extractParameters( mojoDescriptor, javaClass ); |
377 | |
|
378 | 10 | return mojoDescriptor; |
379 | |
} |
380 | |
|
381 | |
|
382 | |
|
383 | |
|
384 | |
|
385 | |
|
386 | |
|
387 | |
|
388 | |
private static boolean getBooleanTagValue( JavaClass javaClass, String tagName, boolean defaultValue ) |
389 | |
{ |
390 | 50 | DocletTag tag = findInClassHierarchy( javaClass, tagName ); |
391 | |
|
392 | 50 | if ( tag != null ) |
393 | |
{ |
394 | 10 | String value = tag.getValue(); |
395 | |
|
396 | 10 | if ( StringUtils.isNotEmpty( value ) ) |
397 | |
{ |
398 | 10 | defaultValue = Boolean.valueOf( value ).booleanValue(); |
399 | |
} |
400 | |
} |
401 | 50 | return defaultValue; |
402 | |
} |
403 | |
|
404 | |
|
405 | |
|
406 | |
|
407 | |
|
408 | |
|
409 | |
|
410 | |
|
411 | |
|
412 | |
private static boolean getBooleanTagValue( JavaClass javaClass, String tagName, boolean defaultForTag, |
413 | |
boolean defaultValue ) |
414 | |
{ |
415 | 10 | DocletTag tag = findInClassHierarchy( javaClass, tagName ); |
416 | |
|
417 | 10 | if ( tag != null ) |
418 | |
{ |
419 | 6 | String value = tag.getValue(); |
420 | |
|
421 | 6 | if ( StringUtils.isNotEmpty( value ) ) |
422 | |
{ |
423 | 0 | return Boolean.valueOf( value ).booleanValue(); |
424 | |
} |
425 | |
else |
426 | |
{ |
427 | 6 | return defaultForTag; |
428 | |
} |
429 | |
} |
430 | 4 | return defaultValue; |
431 | |
} |
432 | |
|
433 | |
|
434 | |
|
435 | |
|
436 | |
|
437 | |
|
438 | |
private static DocletTag findInClassHierarchy( JavaClass javaClass, String tagName ) |
439 | |
{ |
440 | 406 | DocletTag tag = javaClass.getTagByName( tagName ); |
441 | |
|
442 | 406 | if ( tag == null ) |
443 | |
{ |
444 | 354 | JavaClass superClass = javaClass.getSuperJavaClass(); |
445 | |
|
446 | 354 | if ( superClass != null ) |
447 | |
{ |
448 | 236 | tag = findInClassHierarchy( superClass, tagName ); |
449 | |
} |
450 | |
} |
451 | |
|
452 | 406 | return tag; |
453 | |
} |
454 | |
|
455 | |
|
456 | |
|
457 | |
|
458 | |
|
459 | |
|
460 | |
private void extractParameters( MojoDescriptor mojoDescriptor, JavaClass javaClass ) |
461 | |
throws InvalidPluginDescriptorException |
462 | |
{ |
463 | |
|
464 | |
|
465 | |
|
466 | |
|
467 | 10 | Map<String, JavaField> rawParams = extractFieldParameterTags( javaClass ); |
468 | |
|
469 | 10 | for ( Map.Entry<String, JavaField> entry : rawParams.entrySet() ) |
470 | |
{ |
471 | 16 | JavaField field = entry.getValue(); |
472 | |
|
473 | 16 | Type type = field.getType(); |
474 | |
|
475 | 16 | Parameter pd = new Parameter(); |
476 | |
|
477 | 16 | pd.setName( entry.getKey() ); |
478 | |
|
479 | 16 | if ( !type.isArray() ) |
480 | |
{ |
481 | 12 | pd.setType( type.getValue() ); |
482 | |
} |
483 | |
else |
484 | |
{ |
485 | 4 | StringBuilder value = new StringBuilder( type.getValue() ); |
486 | |
|
487 | 4 | int remaining = type.getDimensions(); |
488 | |
|
489 | 8 | while ( remaining-- > 0 ) |
490 | |
{ |
491 | 4 | value.append( "[]" ); |
492 | |
} |
493 | |
|
494 | 4 | pd.setType( value.toString() ); |
495 | |
} |
496 | |
|
497 | 16 | pd.setDescription( field.getComment() ); |
498 | |
|
499 | 16 | DocletTag deprecationTag = field.getTagByName( JavaMojoAnnotation.DEPRECATED ); |
500 | |
|
501 | 16 | if ( deprecationTag != null ) |
502 | |
{ |
503 | 2 | pd.setDeprecated( deprecationTag.getValue() ); |
504 | |
} |
505 | |
|
506 | 16 | DocletTag sinceTag = field.getTagByName( JavaMojoAnnotation.SINCE ); |
507 | 16 | if ( sinceTag != null ) |
508 | |
{ |
509 | 2 | pd.setSince( sinceTag.getValue() ); |
510 | |
} |
511 | |
|
512 | 16 | DocletTag componentTag = field.getTagByName( JavaMojoAnnotation.COMPONENT ); |
513 | |
|
514 | 16 | if ( componentTag != null ) |
515 | |
{ |
516 | |
|
517 | 4 | String role = componentTag.getNamedParameter( JavaMojoAnnotation.COMPONENT_ROLE ); |
518 | |
|
519 | 4 | if ( role == null ) |
520 | |
{ |
521 | 2 | role = field.getType().toString(); |
522 | |
} |
523 | |
|
524 | 4 | String roleHint = componentTag.getNamedParameter( JavaMojoAnnotation.COMPONENT_ROLEHINT ); |
525 | |
|
526 | 4 | if ( roleHint == null ) |
527 | |
{ |
528 | |
|
529 | 2 | roleHint = componentTag.getNamedParameter( "role-hint" ); |
530 | |
} |
531 | |
|
532 | |
|
533 | 4 | String expression = PluginUtils.MAVEN_COMPONENTS.get( role ); |
534 | |
|
535 | 4 | if ( expression == null ) |
536 | |
{ |
537 | |
|
538 | 4 | pd.setRequirement( new Requirement( role, roleHint ) ); |
539 | |
} |
540 | |
else |
541 | |
{ |
542 | |
|
543 | 0 | pd.setDefaultValue( expression ); |
544 | 0 | pd.setType( role ); |
545 | 0 | pd.setRequired( true ); |
546 | |
} |
547 | |
|
548 | 4 | pd.setEditable( false ); |
549 | |
|
550 | |
|
551 | |
|
552 | 4 | } |
553 | |
else |
554 | |
{ |
555 | |
|
556 | 12 | DocletTag parameter = field.getTagByName( JavaMojoAnnotation.PARAMETER ); |
557 | |
|
558 | 12 | pd.setRequired( field.getTagByName( JavaMojoAnnotation.REQUIRED ) != null ); |
559 | |
|
560 | 12 | pd.setEditable( field.getTagByName( JavaMojoAnnotation.READONLY ) == null ); |
561 | |
|
562 | 12 | String alias = parameter.getNamedParameter( JavaMojoAnnotation.PARAMETER_ALIAS ); |
563 | |
|
564 | 12 | if ( !StringUtils.isEmpty( alias ) ) |
565 | |
{ |
566 | 2 | pd.setAlias( alias ); |
567 | |
} |
568 | |
|
569 | 12 | String expression = parameter.getNamedParameter( JavaMojoAnnotation.PARAMETER_EXPRESSION ); |
570 | 12 | String property = parameter.getNamedParameter( JavaMojoAnnotation.PARAMETER_PROPERTY ); |
571 | |
|
572 | 12 | if ( StringUtils.isNotEmpty( expression ) && StringUtils.isNotEmpty( property ) ) |
573 | |
{ |
574 | 0 | getLogger().error( javaClass.getFullyQualifiedName() + "#" + field.getName() + ":" ); |
575 | 0 | getLogger().error( " Cannot use both:" ); |
576 | 0 | getLogger().error( " @parameter expression=\"${property}\"" ); |
577 | 0 | getLogger().error( " and" ); |
578 | 0 | getLogger().error( " @parameter property=\"property\"" ); |
579 | 0 | getLogger().error( " Second syntax is preferred." ); |
580 | 0 | throw new InvalidParameterException( javaClass.getFullyQualifiedName() + "#" + field.getName() |
581 | |
+ ": cannot" + " use both @parameter expression and property", null ); |
582 | |
} |
583 | |
|
584 | 12 | if ( StringUtils.isNotEmpty( expression ) ) |
585 | |
{ |
586 | 2 | getLogger().warn( javaClass.getFullyQualifiedName() + "#" + field.getName() + ":" ); |
587 | 2 | getLogger().warn( " The syntax" ); |
588 | 2 | getLogger().warn( " @parameter expression=\"${property}\"" ); |
589 | 2 | getLogger().warn( " is deprecated, please use" ); |
590 | 2 | getLogger().warn( " @parameter property=\"property\"" ); |
591 | 2 | getLogger().warn( " instead." ); |
592 | |
|
593 | |
} |
594 | 10 | else if ( StringUtils.isNotEmpty( property ) ) |
595 | |
{ |
596 | 2 | expression = "${" + property + "}"; |
597 | |
} |
598 | |
|
599 | 12 | pd.setExpression( expression ); |
600 | |
|
601 | 12 | if ( StringUtils.isNotEmpty( expression ) && expression.startsWith( "${component." ) ) |
602 | |
{ |
603 | 0 | getLogger().warn( javaClass.getFullyQualifiedName() + "#" + field.getName() + ":" ); |
604 | 0 | getLogger().warn( " The syntax" ); |
605 | 0 | getLogger().warn( " @parameter expression=\"${component.<role>#<roleHint>}\"" ); |
606 | 0 | getLogger().warn( " is deprecated, please use" ); |
607 | 0 | getLogger().warn( " @component role=\"<role>\" roleHint=\"<roleHint>\"" ); |
608 | 0 | getLogger().warn( " instead." ); |
609 | |
} |
610 | |
|
611 | 12 | if ( "${reports}".equals( pd.getExpression() ) ) |
612 | |
{ |
613 | 0 | mojoDescriptor.setRequiresReports( true ); |
614 | |
} |
615 | |
|
616 | 12 | pd.setDefaultValue( parameter.getNamedParameter( JavaMojoAnnotation.PARAMETER_DEFAULT_VALUE ) ); |
617 | |
|
618 | 12 | pd.setImplementation( parameter.getNamedParameter( JavaMojoAnnotation.PARAMETER_IMPLEMENTATION ) ); |
619 | |
} |
620 | |
|
621 | 16 | mojoDescriptor.addParameter( pd ); |
622 | 16 | } |
623 | 10 | } |
624 | |
|
625 | |
|
626 | |
|
627 | |
|
628 | |
|
629 | |
|
630 | |
|
631 | |
private Map<String, JavaField> extractFieldParameterTags( JavaClass javaClass ) |
632 | |
{ |
633 | |
Map<String, JavaField> rawParams; |
634 | |
|
635 | |
|
636 | |
|
637 | 30 | JavaClass superClass = javaClass.getSuperJavaClass(); |
638 | |
|
639 | 30 | if ( superClass != null ) |
640 | |
{ |
641 | 20 | rawParams = extractFieldParameterTags( superClass ); |
642 | |
} |
643 | |
else |
644 | |
{ |
645 | 10 | rawParams = new TreeMap<String, JavaField>(); |
646 | |
} |
647 | |
|
648 | 30 | JavaField[] classFields = javaClass.getFields(); |
649 | |
|
650 | 30 | if ( classFields != null ) |
651 | |
{ |
652 | 70 | for ( JavaField field : classFields ) |
653 | |
{ |
654 | 40 | if ( field.getTagByName( JavaMojoAnnotation.PARAMETER ) != null |
655 | |
|| field.getTagByName( JavaMojoAnnotation.COMPONENT ) != null ) |
656 | |
{ |
657 | 16 | rawParams.put( field.getName(), field ); |
658 | |
} |
659 | |
} |
660 | |
} |
661 | 30 | return rawParams; |
662 | |
} |
663 | |
|
664 | |
|
665 | |
public List<MojoDescriptor> execute( MavenProject project, PluginDescriptor pluginDescriptor ) |
666 | |
throws ExtractionException, InvalidPluginDescriptorException |
667 | |
{ |
668 | 0 | return execute( new DefaultPluginToolsRequest( project, pluginDescriptor ) ); |
669 | |
} |
670 | |
|
671 | |
|
672 | |
public List<MojoDescriptor> execute( PluginToolsRequest request ) |
673 | |
throws ExtractionException, InvalidPluginDescriptorException |
674 | |
{ |
675 | 10 | JavaClass[] javaClasses = discoverClasses( request ); |
676 | |
|
677 | 10 | List<MojoDescriptor> descriptors = new ArrayList<MojoDescriptor>(); |
678 | |
|
679 | 32 | for ( JavaClass javaClass : javaClasses ) |
680 | |
{ |
681 | 22 | DocletTag tag = javaClass.getTagByName( GOAL ); |
682 | |
|
683 | 22 | if ( tag != null ) |
684 | |
{ |
685 | 10 | MojoDescriptor mojoDescriptor = createMojoDescriptor( javaClass ); |
686 | 10 | mojoDescriptor.setPluginDescriptor( request.getPluginDescriptor() ); |
687 | |
|
688 | |
|
689 | 10 | validate( mojoDescriptor ); |
690 | |
|
691 | 10 | descriptors.add( mojoDescriptor ); |
692 | |
} |
693 | |
} |
694 | |
|
695 | 10 | return descriptors; |
696 | |
} |
697 | |
|
698 | |
|
699 | |
|
700 | |
|
701 | |
|
702 | |
@SuppressWarnings( "unchecked" ) |
703 | |
protected JavaClass[] discoverClasses( final PluginToolsRequest request ) |
704 | |
{ |
705 | 10 | JavaDocBuilder builder = new JavaDocBuilder(); |
706 | 10 | builder.setEncoding( request.getEncoding() ); |
707 | |
|
708 | 10 | MavenProject project = request.getProject(); |
709 | |
|
710 | 10 | for ( String source : (List<String>) project.getCompileSourceRoots() ) |
711 | |
{ |
712 | 10 | builder.addSourceTree( new File( source ) ); |
713 | |
} |
714 | |
|
715 | |
|
716 | 10 | File generatedPlugin = new File( project.getBasedir(), "target/generated-sources/plugin" ); |
717 | 10 | if ( !project.getCompileSourceRoots().contains( generatedPlugin.getAbsolutePath() ) ) |
718 | |
{ |
719 | 10 | builder.addSourceTree( generatedPlugin ); |
720 | |
} |
721 | |
|
722 | 10 | return builder.getClasses(); |
723 | |
} |
724 | |
|
725 | |
|
726 | |
|
727 | |
|
728 | |
|
729 | |
protected void validate( MojoDescriptor mojoDescriptor ) |
730 | |
throws InvalidParameterException |
731 | |
{ |
732 | |
@SuppressWarnings( "unchecked" ) |
733 | 10 | List<Parameter> parameters = mojoDescriptor.getParameters(); |
734 | |
|
735 | 10 | if ( parameters != null ) |
736 | |
{ |
737 | 24 | for ( int j = 0; j < parameters.size(); j++ ) |
738 | |
{ |
739 | 16 | validateParameter( parameters.get( j ), j ); |
740 | |
} |
741 | |
} |
742 | 10 | } |
743 | |
} |