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