1
2
3
4
5 package org.apache.maven.api.model;
6
7 import java.io.Serializable;
8 import java.util.ArrayList;
9 import java.util.Collection;
10 import java.util.Collections;
11 import java.util.HashMap;
12 import java.util.List;
13 import java.util.Map;
14 import org.apache.maven.api.annotations.Experimental;
15 import org.apache.maven.api.annotations.Generated;
16 import org.apache.maven.api.annotations.Immutable;
17 import org.apache.maven.api.annotations.Nonnull;
18 import org.apache.maven.api.annotations.NotThreadSafe;
19 import org.apache.maven.api.annotations.ThreadSafe;
20 import org.apache.maven.api.xml.XmlNode;
21
22
23
24
25 @Experimental
26 @Generated @ThreadSafe @Immutable
27 public class Plugin
28 extends ConfigurationContainer
29 implements Serializable, InputLocationTracker
30 {
31
32
33
34 final String groupId;
35
36
37
38 final String artifactId;
39
40
41
42 final String version;
43
44
45
46
47
48
49 final String extensions;
50
51
52
53
54 final List<PluginExecution> executions;
55
56
57
58
59 final List<Dependency> dependencies;
60
61
62
63
64
65 Plugin(
66 String inherited,
67 XmlNode configuration,
68 String groupId,
69 String artifactId,
70 String version,
71 String extensions,
72 Collection<PluginExecution> executions,
73 Collection<Dependency> dependencies,
74 Map<Object, InputLocation> locations
75 ) {
76 super(
77 inherited,
78 configuration,
79 locations
80 );
81 this.groupId = groupId;
82 this.artifactId = artifactId;
83 this.version = version;
84 this.extensions = extensions;
85 this.executions = ImmutableCollections.copy(executions);
86 this.dependencies = ImmutableCollections.copy(dependencies);
87 }
88
89
90
91
92
93
94 public String getGroupId() {
95 return this.groupId;
96 }
97
98
99
100
101
102
103 public String getArtifactId() {
104 return this.artifactId;
105 }
106
107
108
109
110
111
112 public String getVersion() {
113 return this.version;
114 }
115
116
117
118
119
120
121
122
123
124 public String getExtensions() {
125 return this.extensions;
126 }
127
128
129
130
131
132
133
134 @Nonnull
135 public List<PluginExecution> getExecutions() {
136 return this.executions;
137 }
138
139
140
141
142
143
144
145 @Nonnull
146 public List<Dependency> getDependencies() {
147 return this.dependencies;
148 }
149
150
151
152
153
154
155 @Nonnull
156 public Builder with() {
157 return newBuilder(this);
158 }
159
160
161
162
163
164
165 @Nonnull
166 public Plugin withInherited(String inherited) {
167 return newBuilder(this, true).inherited(inherited).build();
168 }
169
170
171
172
173
174
175 @Nonnull
176 public Plugin withConfiguration(XmlNode configuration) {
177 return newBuilder(this, true).configuration(configuration).build();
178 }
179
180
181
182
183
184
185 @Nonnull
186 public Plugin withGroupId(String groupId) {
187 return newBuilder(this, true).groupId(groupId).build();
188 }
189
190
191
192
193
194
195 @Nonnull
196 public Plugin withArtifactId(String artifactId) {
197 return newBuilder(this, true).artifactId(artifactId).build();
198 }
199
200
201
202
203
204
205 @Nonnull
206 public Plugin withVersion(String version) {
207 return newBuilder(this, true).version(version).build();
208 }
209
210
211
212
213
214
215 @Nonnull
216 public Plugin withExtensions(String extensions) {
217 return newBuilder(this, true).extensions(extensions).build();
218 }
219
220
221
222
223
224
225 @Nonnull
226 public Plugin withExecutions(Collection<PluginExecution> executions) {
227 return newBuilder(this, true).executions(executions).build();
228 }
229
230
231
232
233
234
235 @Nonnull
236 public Plugin withDependencies(Collection<Dependency> dependencies) {
237 return newBuilder(this, true).dependencies(dependencies).build();
238 }
239
240
241
242
243
244
245
246
247 @Nonnull
248 public static Plugin newInstance() {
249 return newInstance(true);
250 }
251
252
253
254
255
256
257
258
259 @Nonnull
260 public static Plugin newInstance(boolean withDefaults) {
261 return newBuilder(withDefaults).build();
262 }
263
264
265
266
267
268
269
270
271 @Nonnull
272 public static Builder newBuilder() {
273 return newBuilder(true);
274 }
275
276
277
278
279
280
281
282 @Nonnull
283 public static Builder newBuilder(boolean withDefaults) {
284 return new Builder(withDefaults);
285 }
286
287
288
289
290
291
292
293
294 @Nonnull
295 public static Builder newBuilder(Plugin from) {
296 return newBuilder(from, false);
297 }
298
299
300
301
302
303
304
305
306 @Nonnull
307 public static Builder newBuilder(Plugin from, boolean forceCopy) {
308 return new Builder(from, forceCopy);
309 }
310
311
312
313
314
315
316 @NotThreadSafe
317 public static class Builder
318 extends ConfigurationContainer.Builder
319 {
320 Plugin base;
321 String groupId;
322 String artifactId;
323 String version;
324 String extensions;
325 Collection<PluginExecution> executions;
326 Collection<Dependency> dependencies;
327
328 Builder(boolean withDefaults) {
329 super(withDefaults);
330 if (withDefaults) {
331 this.groupId = "org.apache.maven.plugins";
332 }
333 }
334
335 Builder(Plugin base, boolean forceCopy) {
336 super(base, forceCopy);
337 if (forceCopy) {
338 this.groupId = base.groupId;
339 this.artifactId = base.artifactId;
340 this.version = base.version;
341 this.extensions = base.extensions;
342 this.executions = base.executions;
343 this.dependencies = base.dependencies;
344 this.locations = base.locations;
345 } else {
346 this.base = base;
347 }
348 }
349
350 @Nonnull
351 public Builder inherited(String inherited) {
352 this.inherited = inherited;
353 return this;
354 }
355
356 @Nonnull
357 public Builder configuration(XmlNode configuration) {
358 this.configuration = configuration;
359 return this;
360 }
361
362 @Nonnull
363 public Builder groupId(String groupId) {
364 this.groupId = groupId;
365 return this;
366 }
367
368 @Nonnull
369 public Builder artifactId(String artifactId) {
370 this.artifactId = artifactId;
371 return this;
372 }
373
374 @Nonnull
375 public Builder version(String version) {
376 this.version = version;
377 return this;
378 }
379
380 @Nonnull
381 public Builder extensions(String extensions) {
382 this.extensions = extensions;
383 return this;
384 }
385
386 @Nonnull
387 public Builder executions(Collection<PluginExecution> executions) {
388 this.executions = executions;
389 return this;
390 }
391
392 @Nonnull
393 public Builder dependencies(Collection<Dependency> dependencies) {
394 this.dependencies = dependencies;
395 return this;
396 }
397
398
399 @Nonnull
400 public Builder location(Object key, InputLocation location) {
401 if (location != null) {
402 if (!(this.locations instanceof HashMap)) {
403 this.locations = this.locations != null ? new HashMap<>(this.locations) : new HashMap<>();
404 }
405 this.locations.put(key, location);
406 }
407 return this;
408 }
409
410 @Nonnull
411 public Plugin build() {
412 if (base != null
413 && (inherited == null || inherited == base.inherited)
414 && (configuration == null || configuration == base.configuration)
415 && (groupId == null || groupId == base.groupId)
416 && (artifactId == null || artifactId == base.artifactId)
417 && (version == null || version == base.version)
418 && (extensions == null || extensions == base.extensions)
419 && (executions == null || executions == base.executions)
420 && (dependencies == null || dependencies == base.dependencies)
421 ) {
422 return base;
423 }
424 Map<Object, InputLocation> newlocs = this.locations != null ? this.locations : Collections.emptyMap();
425 Map<Object, InputLocation> oldlocs = this.base != null && this.base.locations != null ? this.base.locations : Collections.emptyMap();
426 Map<Object, InputLocation> locations = new HashMap<>();
427 locations.put("", newlocs.containsKey("") ? newlocs.get("") : oldlocs.get(""));
428 locations.put("inherited", newlocs.containsKey("inherited") ? newlocs.get("inherited") : oldlocs.get("inherited"));
429 locations.put("configuration", newlocs.containsKey("configuration") ? newlocs.get("configuration") : oldlocs.get("configuration"));
430 locations.put("groupId", newlocs.containsKey("groupId") ? newlocs.get("groupId") : oldlocs.get("groupId"));
431 locations.put("artifactId", newlocs.containsKey("artifactId") ? newlocs.get("artifactId") : oldlocs.get("artifactId"));
432 locations.put("version", newlocs.containsKey("version") ? newlocs.get("version") : oldlocs.get("version"));
433 locations.put("extensions", newlocs.containsKey("extensions") ? newlocs.get("extensions") : oldlocs.get("extensions"));
434 locations.put("executions", newlocs.containsKey("executions") ? newlocs.get("executions") : oldlocs.get("executions"));
435 locations.put("dependencies", newlocs.containsKey("dependencies") ? newlocs.get("dependencies") : oldlocs.get("dependencies"));
436 return new Plugin(
437 inherited != null ? inherited : (base != null ? base.inherited : null),
438 configuration != null ? configuration : (base != null ? base.configuration : null),
439 groupId != null ? groupId : (base != null ? base.groupId : null),
440 artifactId != null ? artifactId : (base != null ? base.artifactId : null),
441 version != null ? version : (base != null ? base.version : null),
442 extensions != null ? extensions : (base != null ? base.extensions : null),
443 executions != null ? executions : (base != null ? base.executions : null),
444 dependencies != null ? dependencies : (base != null ? base.dependencies : null),
445 locations
446 );
447 }
448 }
449
450
451
452 public boolean isExtensions() {
453 return (getExtensions() != null) ? Boolean.parseBoolean(getExtensions()) : false;
454 }
455
456
457
458
459 private Map<String, PluginExecution> executionMap = null;
460
461
462
463
464 public void flushExecutionMap() {
465 this.executionMap = null;
466 }
467
468
469
470
471
472 public Map<String, PluginExecution> getExecutionsAsMap() {
473 if (executionMap == null) {
474 executionMap = new java.util.LinkedHashMap<String, PluginExecution>();
475 for (java.util.Iterator<PluginExecution> i = getExecutions().iterator(); i.hasNext();) {
476 PluginExecution exec = (PluginExecution) i.next();
477 if (executionMap.containsKey(exec.getId())) {
478 throw new IllegalStateException("You cannot have two plugin executions with the same (or missing) <id/> elements.\nOffending execution\n\nId: \'" + exec.getId() + "\'\nPlugin:\'" + getKey() + "\'\n\n");
479 }
480 executionMap.put(exec.getId(), exec);
481 }
482 }
483 return executionMap;
484 }
485
486
487
488
489
490
491 public String getId() {
492 return new StringBuilder(128)
493 .append((getGroupId() == null) ? "[unknown-group-id]" : getGroupId())
494 .append(":")
495 .append((getArtifactId() == null) ? "[unknown-artifact-id]" : getArtifactId())
496 .append(":")
497 .append((getVersion() == null) ? "[unknown-version]" : getVersion())
498 .toString();
499 }
500
501
502
503
504 public String getKey() {
505 return constructKey(getGroupId(), getArtifactId());
506 }
507
508
509
510
511
512
513 public static String constructKey(String groupId, String artifactId) {
514 return groupId + ":" + artifactId;
515 }
516
517
518
519
520
521
522
523
524 public boolean equals(Object other) {
525 if (other instanceof Plugin) {
526 Plugin otherPlugin = (Plugin) other;
527 return getKey().equals(otherPlugin.getKey());
528 }
529 return false;
530 }
531
532
533
534
535 public int hashCode() {
536 return getKey().hashCode();
537 }
538
539
540
541
542 public String toString() {
543 return "Plugin [" + getKey() + "]";
544 }
545
546
547 }