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