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