View Javadoc
1   // =================== DO NOT EDIT THIS FILE ====================
2   //   Generated by Maven, any modifications will be overwritten.
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   * The {@code <plugin>} element contains informations required for a plugin.
23   */
24  @Experimental
25  @Generated @ThreadSafe @Immutable
26  public class Plugin
27      extends ConfigurationContainer
28      implements Serializable, InputLocationTracker
29  {
30      /**
31       * The group ID of the plugin in the repository.
32       */
33      final String groupId;
34      /**
35       * The artifact ID of the plugin in the repository.
36       */
37      final String artifactId;
38      /**
39       * The version (or valid range of versions) of the plugin to be used.
40       */
41      final String version;
42      /**
43       * Whether to load Maven extensions (such as packaging and type handlers) from
44       * this plugin. For performance reasons, this should only be enabled when necessary. Note: While the type
45       * of this field is {@code String} for technical reasons, the semantic type is actually
46       * {@code Boolean}. Default value is {@code false}.
47       */
48      final String extensions;
49      /**
50       * Multiple specifications of a set of goals to execute during the build
51       * lifecycle, each having (possibly) a different configuration.
52       */
53      final List<PluginExecution> executions;
54      /**
55       * Additional dependencies that this project needs to introduce to the plugin's
56       * classloader.
57       */
58      final List<Dependency> dependencies;
59      /** Location of the xml element for the field groupId. */
60      final InputLocation groupIdLocation;
61      /** Location of the xml element for the field artifactId. */
62      final InputLocation artifactIdLocation;
63      /** Location of the xml element for the field version. */
64      final InputLocation versionLocation;
65      /** Location of the xml element for the field extensions. */
66      final InputLocation extensionsLocation;
67      /** Location of the xml element for the field executions. */
68      final InputLocation executionsLocation;
69      /** Location of the xml element for the field dependencies. */
70      final InputLocation dependenciesLocation;
71  
72      /**
73        * Constructor for this class, package protected.
74        * @see Builder#build()
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      * The group ID of the plugin in the repository.
121      *
122      * @return a {@code String}
123      */
124     public String getGroupId()
125     {
126         return this.groupId;
127     }
128 
129     /**
130      * The artifact ID of the plugin in the repository.
131      *
132      * @return a {@code String}
133      */
134     public String getArtifactId()
135     {
136         return this.artifactId;
137     }
138 
139     /**
140      * The version (or valid range of versions) of the plugin to be used.
141      *
142      * @return a {@code String}
143      */
144     public String getVersion()
145     {
146         return this.version;
147     }
148 
149     /**
150      * Whether to load Maven extensions (such as packaging and type handlers) from
151      * this plugin. For performance reasons, this should only be enabled when necessary. Note: While the type
152      * of this field is {@code String} for technical reasons, the semantic type is actually
153      * {@code Boolean}. Default value is {@code false}.
154      *
155      * @return a {@code String}
156      */
157     public String getExtensions()
158     {
159         return this.extensions;
160     }
161 
162     /**
163      * Multiple specifications of a set of goals to execute during the build
164      * lifecycle, each having (possibly) a different configuration.
165      *
166      * @return a {@code List<PluginExecution>}
167      */
168     @Nonnull
169     public List<PluginExecution> getExecutions()
170     {
171         return this.executions;
172     }
173 
174     /**
175      * Additional dependencies that this project needs to introduce to the plugin's
176      * classloader.
177      *
178      * @return a {@code List<Dependency>}
179      */
180     @Nonnull
181     public List<Dependency> getDependencies()
182     {
183         return this.dependencies;
184     }
185 
186     /**
187      * Gets the location of the specified field in the input source.
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      * Creates a new builder with this object as the basis.
214      *
215      * @return a {@code Builder}
216      */
217     @Nonnull
218     public Builder with()
219     {
220         return newBuilder( this );
221     }
222     /**
223      * Creates a new {@code Plugin} instance using the specified inherited.
224      *
225      * @param inherited the new {@code String} to use
226      * @return a {@code Plugin} with the specified inherited
227      */
228     @Nonnull
229     public Plugin withInherited( String inherited )
230     {
231         return with().inherited( inherited ).build();
232     }
233     /**
234      * Creates a new {@code Plugin} instance using the specified configuration.
235      *
236      * @param configuration the new {@code Dom} to use
237      * @return a {@code Plugin} with the specified configuration
238      */
239     @Nonnull
240     public Plugin withConfiguration( Dom configuration )
241     {
242         return with().configuration( configuration ).build();
243     }
244     /**
245      * Creates a new {@code Plugin} instance using the specified groupId.
246      *
247      * @param groupId the new {@code String} to use
248      * @return a {@code Plugin} with the specified groupId
249      */
250     @Nonnull
251     public Plugin withGroupId( String groupId )
252     {
253         return with().groupId( groupId ).build();
254     }
255     /**
256      * Creates a new {@code Plugin} instance using the specified artifactId.
257      *
258      * @param artifactId the new {@code String} to use
259      * @return a {@code Plugin} with the specified artifactId
260      */
261     @Nonnull
262     public Plugin withArtifactId( String artifactId )
263     {
264         return with().artifactId( artifactId ).build();
265     }
266     /**
267      * Creates a new {@code Plugin} instance using the specified version.
268      *
269      * @param version the new {@code String} to use
270      * @return a {@code Plugin} with the specified version
271      */
272     @Nonnull
273     public Plugin withVersion( String version )
274     {
275         return with().version( version ).build();
276     }
277     /**
278      * Creates a new {@code Plugin} instance using the specified extensions.
279      *
280      * @param extensions the new {@code String} to use
281      * @return a {@code Plugin} with the specified extensions
282      */
283     @Nonnull
284     public Plugin withExtensions( String extensions )
285     {
286         return with().extensions( extensions ).build();
287     }
288     /**
289      * Creates a new {@code Plugin} instance using the specified executions.
290      *
291      * @param executions the new {@code Collection<PluginExecution>} to use
292      * @return a {@code Plugin} with the specified executions
293      */
294     @Nonnull
295     public Plugin withExecutions( Collection<PluginExecution> executions )
296     {
297         return with().executions( executions ).build();
298     }
299     /**
300      * Creates a new {@code Plugin} instance using the specified dependencies.
301      *
302      * @param dependencies the new {@code Collection<Dependency>} to use
303      * @return a {@code Plugin} with the specified dependencies
304      */
305     @Nonnull
306     public Plugin withDependencies( Collection<Dependency> dependencies )
307     {
308         return with().dependencies( dependencies ).build();
309     }
310 
311     /**
312      * Creates a new {@code Plugin} instance.
313      * Equivalent to {@code newInstance( true )}.
314      * @see #newInstance(boolean)
315      *
316      * @return a new {@code Plugin}
317      */
318     @Nonnull
319     public static Plugin newInstance()
320     {
321         return newInstance( true );
322     }
323 
324     /**
325      * Creates a new {@code Plugin} instance using default values or not.
326      * Equivalent to {@code newBuilder( withDefaults ).build()}.
327      *
328      * @param withDefaults the boolean indicating whether default values should be used
329      * @return a new {@code Plugin}
330      */
331     @Nonnull
332     public static Plugin newInstance( boolean withDefaults )
333     {
334         return newBuilder( withDefaults ).build();
335     }
336 
337     /**
338      * Creates a new {@code Plugin} builder instance.
339      * Equivalent to {@code newBuilder( true )}.
340      * @see #newBuilder(boolean)
341      *
342      * @return a new {@code Builder}
343      */
344     @Nonnull
345     public static Builder newBuilder()
346     {
347         return newBuilder( true );
348     }
349 
350     /**
351      * Creates a new {@code Plugin} builder instance using default values or not.
352      *
353      * @param withDefaults the boolean indicating whether default values should be used
354      * @return a new {@code Builder}
355      */
356     @Nonnull
357     public static Builder newBuilder( boolean withDefaults )
358     {
359         return new Builder( withDefaults );
360     }
361 
362     /**
363      * Creates a new {@code Plugin} builder instance using the specified object as a basis.
364      * Equivalent to {@code newBuilder( from, false )}.
365      *
366      * @param from the {@code Plugin} instance to use as a basis
367      * @return a new {@code Builder}
368      */
369     @Nonnull
370     public static Builder newBuilder( Plugin from )
371     {
372         return newBuilder( from, false );
373     }
374 
375     /**
376      * Creates a new {@code Plugin} builder instance using the specified object as a basis.
377      *
378      * @param from the {@code Plugin} instance to use as a basis
379      * @param forceCopy the boolean indicating if a copy should be forced
380      * @return a new {@code Builder}
381      */
382     @Nonnull
383     public static Builder newBuilder( Plugin from, boolean forceCopy )
384     {
385         return new Builder( from, forceCopy );
386     }
387 
388     /**
389      * Builder class used to create Plugin instances.
390      * @see #with()
391      * @see #newBuilder()
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      * Reset the {@code executionMap} field to {@code null}
579      */
580     public void flushExecutionMap()
581     {
582         this.executionMap = null;
583     }
584     /**
585      * @return a Map of executions field with {@code PluginExecution#getId()} as key
586      * @see PluginExecution#getId()
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      * Gets the identifier of the plugin.
608      *
609      * @return the plugin id in the form {@code <groupId>:<artifactId>:<version>}, never {@code null}
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      * @return the key of the plugin, ie {@code groupId:artifactId}
626      */
627     public String getKey()
628     {
629         return constructKey( getGroupId(), getArtifactId() );
630     }
631 
632     /**
633      * @param groupId The group ID of the plugin in the repository
634      * @param artifactId The artifact ID of the reporting plugin in the repository
635      * @return the key of the plugin, ie {@code groupId:artifactId}
636      */
637     public static String constructKey( String groupId, String artifactId )
638     {
639         return groupId + ":" + artifactId;
640     }
641 
642     /**
643      * @see java.lang.Object#equals(java.lang.Object)
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      * @see java.lang.Object#hashCode()
659      */
660     public int hashCode()
661     {
662         return getKey().hashCode();
663     }
664 
665     /**
666      * @see java.lang.Object#toString()
667      */
668     public String toString()
669     {
670         return "Plugin [" + getKey() + "]";
671     }
672             
673           
674 }