1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugin.descriptor;
20
21 import java.util.ArrayList;
22 import java.util.LinkedHashMap;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Objects;
26 import java.util.stream.Collectors;
27
28 import org.apache.maven.plugin.Mojo;
29 import org.codehaus.plexus.component.repository.ComponentDescriptor;
30 import org.codehaus.plexus.configuration.PlexusConfiguration;
31 import org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration;
32
33
34
35
36
37
38
39
40
41
42 public class MojoDescriptor extends ComponentDescriptor<Mojo> implements Cloneable {
43
44 public static final String MAVEN_PLUGIN = "maven-plugin";
45
46
47 public static final String SINGLE_PASS_EXEC_STRATEGY = "once-per-session";
48
49
50 public static final String MULTI_PASS_EXEC_STRATEGY = "always";
51
52 private static final String DEFAULT_INSTANTIATION_STRATEGY = "per-lookup";
53
54 private static final String DEFAULT_LANGUAGE = "java";
55
56 private final ArrayList<Parameter> parameters;
57
58
59 private String executionStrategy = SINGLE_PASS_EXEC_STRATEGY;
60
61
62
63
64
65 private String goal;
66
67
68
69
70
71
72
73 private String phase;
74
75
76 private String since;
77
78
79 private String executePhase;
80
81
82 private String executeGoal;
83
84
85 private String executeLifecycle;
86
87
88
89
90
91 private String deprecated;
92
93
94
95
96
97 private boolean aggregator = false;
98
99
100
101
102
103
104 private String dependencyResolutionRequired = null;
105
106
107
108
109
110 private String dependencyCollectionRequired;
111
112
113 private boolean projectRequired = true;
114
115
116 private boolean onlineRequired = false;
117
118
119 private PlexusConfiguration mojoConfiguration;
120
121
122 private PluginDescriptor pluginDescriptor;
123
124
125 private boolean inheritedByDefault = true;
126
127
128 private boolean directInvocationOnly = false;
129
130
131 private boolean requiresReports = false;
132
133
134
135
136
137 private boolean threadSafe = false;
138
139 private boolean v4Api = false;
140
141
142
143
144 public MojoDescriptor() {
145 this.parameters = new ArrayList<>();
146 setInstantiationStrategy(DEFAULT_INSTANTIATION_STRATEGY);
147 setComponentFactory(DEFAULT_LANGUAGE);
148 }
149
150 public MojoDescriptor(PluginDescriptor pd, org.apache.maven.api.plugin.descriptor.MojoDescriptor md) {
151 this();
152 this.setPluginDescriptor(pd);
153 this.setGoal(md.getGoal());
154 this.setExecuteGoal(md.getExecuteGoal());
155 this.setExecuteLifecycle(md.getExecuteLifecycle());
156 this.setExecutePhase(md.getExecutePhase());
157 this.setDeprecated(md.getDeprecated());
158 this.setLanguage(md.getLanguage());
159 this.setAggregator(md.isAggregator());
160 this.setDependencyCollectionRequired(md.getDependencyCollection());
161 this.setDependencyResolutionRequired(md.getDependencyResolution());
162 this.setComponentConfigurator(md.getConfigurator());
163 this.setInheritedByDefault(md.isInheritedByDefault());
164 this.setPhase(md.getPhase());
165 this.setOnlineRequired(md.isOnlineRequired());
166 this.setProjectRequired(md.isProjectRequired());
167 this.setSince(md.getSince());
168 this.setThreadSafe(true);
169 this.setV4Api(true);
170 this.setImplementation(md.getImplementation());
171 try {
172 this.setParameters(md.getParameters().stream().map(Parameter::new).collect(Collectors.toList()));
173 } catch (DuplicateParameterException e) {
174 throw new IllegalArgumentException(e);
175 }
176 this.mojoDescriptorV4 = md;
177 }
178
179
180
181
182
183
184
185 public String getLanguage() {
186 return getComponentFactory();
187 }
188
189
190
191
192 public void setLanguage(String language) {
193 setComponentFactory(language);
194 }
195
196
197
198
199 public String getDeprecated() {
200 return deprecated;
201 }
202
203
204
205
206 public void setDeprecated(String deprecated) {
207 this.deprecated = deprecated;
208 }
209
210
211
212
213
214 public List<Parameter> getParameters() {
215 return new ArrayList<>(parameters);
216 }
217
218
219
220
221
222 public void setParameters(List<Parameter> parameters) throws DuplicateParameterException {
223 this.parameters.clear();
224 for (Parameter parameter : parameters) {
225 addParameter(parameter);
226 }
227 }
228
229
230
231
232
233 public void addParameter(Parameter parameter) throws DuplicateParameterException {
234 if (parameters.contains(parameter)) {
235 throw new DuplicateParameterException(parameter.getName()
236 + " has been declared multiple times in mojo with goal: " + getGoal() + " (implementation: "
237 + getImplementation() + ")");
238 }
239
240 parameters.add(parameter);
241 }
242
243
244
245
246
247
248 public Map<String, Parameter> getParameterMap() {
249 LinkedHashMap<String, Parameter> parameterMap = new LinkedHashMap<>();
250
251 for (Parameter pd : parameters) {
252 parameterMap.put(pd.getName(), pd);
253 }
254
255 return parameterMap;
256 }
257
258
259
260
261
262
263
264
265 public void setDependencyResolutionRequired(String requiresDependencyResolution) {
266 this.dependencyResolutionRequired = requiresDependencyResolution;
267 }
268
269 public String getDependencyResolutionRequired() {
270 return dependencyResolutionRequired;
271 }
272
273
274
275
276
277 @Deprecated
278 public String isDependencyResolutionRequired() {
279 return dependencyResolutionRequired;
280 }
281
282
283
284
285 public void setDependencyCollectionRequired(String requiresDependencyCollection) {
286 this.dependencyCollectionRequired = requiresDependencyCollection;
287 }
288
289
290
291
292
293
294
295
296
297
298
299 public String getDependencyCollectionRequired() {
300 return dependencyCollectionRequired;
301 }
302
303
304
305
306
307
308
309
310
311 public void setProjectRequired(boolean requiresProject) {
312 this.projectRequired = requiresProject;
313 }
314
315
316
317
318 public boolean isProjectRequired() {
319 return projectRequired;
320 }
321
322
323
324
325
326
327
328
329 public void setOnlineRequired(boolean requiresOnline) {
330 this.onlineRequired = requiresOnline;
331 }
332
333
334
335
336
337
338 public boolean isOnlineRequired() {
339 return onlineRequired;
340 }
341
342
343
344
345
346 public boolean requiresOnline() {
347 return onlineRequired;
348 }
349
350
351
352
353 public String getPhase() {
354 return phase;
355 }
356
357
358
359
360 public void setPhase(String phase) {
361 this.phase = phase;
362 }
363
364
365
366
367 public String getSince() {
368 return since;
369 }
370
371
372
373
374 public void setSince(String since) {
375 this.since = since;
376 }
377
378
379
380
381 public String getGoal() {
382 return goal;
383 }
384
385
386
387
388 public void setGoal(String goal) {
389 this.goal = goal;
390 }
391
392
393
394
395 public String getExecutePhase() {
396 return executePhase;
397 }
398
399
400
401
402 public void setExecutePhase(String executePhase) {
403 this.executePhase = executePhase;
404 }
405
406
407
408
409 public boolean alwaysExecute() {
410 return MULTI_PASS_EXEC_STRATEGY.equals(executionStrategy);
411 }
412
413
414
415
416 public String getExecutionStrategy() {
417 return executionStrategy;
418 }
419
420
421
422
423 public void setExecutionStrategy(String executionStrategy) {
424 this.executionStrategy = executionStrategy;
425 }
426
427
428
429
430 public PlexusConfiguration getMojoConfiguration() {
431 if (mojoConfiguration == null) {
432 mojoConfiguration = new XmlPlexusConfiguration("configuration");
433 }
434 return mojoConfiguration;
435 }
436
437
438
439
440 public void setMojoConfiguration(PlexusConfiguration mojoConfiguration) {
441 this.mojoConfiguration = mojoConfiguration;
442 }
443
444
445 public String getRole() {
446 return isV4Api() ? "org.apache.maven.api.plugin.Mojo" : Mojo.ROLE;
447 }
448
449
450 public String getRoleHint() {
451 return getId();
452 }
453
454
455
456
457 public String getId() {
458 return getPluginDescriptor().getId() + ":" + getGoal();
459 }
460
461
462
463
464
465
466 public String getFullGoalName() {
467 return getPluginDescriptor().getGoalPrefix() + ":" + getGoal();
468 }
469
470
471 public String getComponentType() {
472 return MAVEN_PLUGIN;
473 }
474
475
476
477
478 public PluginDescriptor getPluginDescriptor() {
479 return pluginDescriptor;
480 }
481
482
483
484
485 public void setPluginDescriptor(PluginDescriptor pluginDescriptor) {
486 this.pluginDescriptor = pluginDescriptor;
487 }
488
489
490
491
492 public boolean isInheritedByDefault() {
493 return inheritedByDefault;
494 }
495
496
497
498
499 public void setInheritedByDefault(boolean inheritedByDefault) {
500 this.inheritedByDefault = inheritedByDefault;
501 }
502
503
504 public boolean equals(Object object) {
505 if (this == object) {
506 return true;
507 }
508
509 if (object instanceof MojoDescriptor) {
510 MojoDescriptor other = (MojoDescriptor) object;
511
512 return Objects.equals(getPluginDescriptor(), other.getPluginDescriptor())
513 && Objects.equals(getGoal(), other.getGoal());
514 }
515
516 return false;
517 }
518
519
520 public int hashCode() {
521 return Objects.hash(getGoal(), getPluginDescriptor());
522 }
523
524
525
526
527 public String getExecuteLifecycle() {
528 return executeLifecycle;
529 }
530
531
532
533
534 public void setExecuteLifecycle(String executeLifecycle) {
535 this.executeLifecycle = executeLifecycle;
536 }
537
538
539
540
541
542 public void setAggregator(boolean aggregator) {
543 this.aggregator = aggregator;
544 }
545
546
547
548
549
550 public boolean isAggregator() {
551 return aggregator;
552 }
553
554
555
556
557 public boolean isDirectInvocationOnly() {
558 return directInvocationOnly;
559 }
560
561
562
563
564
565 public void setDirectInvocationOnly(boolean directInvocationOnly) {
566 this.directInvocationOnly = directInvocationOnly;
567 }
568
569
570
571
572 public boolean isRequiresReports() {
573 return requiresReports;
574 }
575
576
577
578
579 public void setRequiresReports(boolean requiresReports) {
580 this.requiresReports = requiresReports;
581 }
582
583
584
585
586 public void setExecuteGoal(String executeGoal) {
587 this.executeGoal = executeGoal;
588 }
589
590
591
592
593 public String getExecuteGoal() {
594 return executeGoal;
595 }
596
597
598
599
600
601 public boolean isThreadSafe() {
602 return threadSafe;
603 }
604
605
606
607
608
609 public void setThreadSafe(boolean threadSafe) {
610 this.threadSafe = threadSafe;
611 }
612
613
614
615
616 public boolean isForking() {
617 return (getExecuteGoal() != null && getExecuteGoal().length() > 0)
618 || (getExecutePhase() != null && getExecutePhase().length() > 0);
619 }
620
621 public boolean isV4Api() {
622 return v4Api;
623 }
624
625 public void setV4Api(boolean v4Api) {
626 this.v4Api = v4Api;
627 }
628
629
630
631
632 @Override
633 public MojoDescriptor clone() {
634 try {
635 return (MojoDescriptor) super.clone();
636 } catch (CloneNotSupportedException e) {
637 throw new UnsupportedOperationException(e);
638 }
639 }
640
641 private volatile org.apache.maven.api.plugin.descriptor.MojoDescriptor mojoDescriptorV4;
642
643 public org.apache.maven.api.plugin.descriptor.MojoDescriptor getMojoDescriptorV4() {
644 if (mojoDescriptorV4 == null) {
645 synchronized (this) {
646 if (mojoDescriptorV4 == null) {
647 mojoDescriptorV4 = org.apache.maven.api.plugin.descriptor.MojoDescriptor.newBuilder()
648 .goal(goal)
649 .description(getDescription())
650 .implementation(getImplementation())
651 .language(getLanguage())
652 .phase(phase)
653 .executeGoal(executeGoal)
654 .executeLifecycle(executeLifecycle)
655 .executePhase(executePhase)
656 .aggregator(aggregator)
657 .dependencyResolution(dependencyResolutionRequired)
658 .dependencyCollection(dependencyCollectionRequired)
659 .projectRequired(projectRequired)
660 .onlineRequired(onlineRequired)
661 .inheritedByDefault(inheritedByDefault)
662 .since(since)
663 .deprecated(deprecated)
664 .configurator(getComponentConfigurator())
665 .parameters(getParameters().stream()
666 .filter(p -> p.getRequirement() == null)
667 .map(Parameter::getParameterV4)
668 .collect(Collectors.toList()))
669 .id(getId())
670 .fullGoalName(getFullGoalName())
671 .build();
672 }
673 }
674 }
675 return mojoDescriptorV4;
676 }
677 }