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