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  
61      /**
62        * Constructor for this class, package protected.
63        * @see Builder#build()
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       * The group ID of the plugin in the repository.
91       *
92       * @return a {@code String}
93       */
94      public String getGroupId() {
95          return this.groupId;
96      }
97  
98      /**
99       * The artifact ID of the plugin in the repository.
100      *
101      * @return a {@code String}
102      */
103     public String getArtifactId() {
104         return this.artifactId;
105     }
106 
107     /**
108      * The version (or valid range of versions) of the plugin to be used.
109      *
110      * @return a {@code String}
111      */
112     public String getVersion() {
113         return this.version;
114     }
115 
116     /**
117      * Whether to load Maven extensions (such as packaging and type handlers) from
118      * this plugin. For performance reasons, this should only be enabled when necessary. Note: While the type
119      * of this field is {@code String} for technical reasons, the semantic type is actually
120      * {@code Boolean}. Default value is {@code false}.
121      *
122      * @return a {@code String}
123      */
124     public String getExtensions() {
125         return this.extensions;
126     }
127 
128     /**
129      * Multiple specifications of a set of goals to execute during the build
130      * lifecycle, each having (possibly) a different configuration.
131      *
132      * @return a {@code List<PluginExecution>}
133      */
134     @Nonnull
135     public List<PluginExecution> getExecutions() {
136         return this.executions;
137     }
138 
139     /**
140      * Additional dependencies that this project needs to introduce to the plugin's
141      * classloader.
142      *
143      * @return a {@code List<Dependency>}
144      */
145     @Nonnull
146     public List<Dependency> getDependencies() {
147         return this.dependencies;
148     }
149 
150     /**
151      * Creates a new builder with this object as the basis.
152      *
153      * @return a {@code Builder}
154      */
155     @Nonnull
156     public Builder with() {
157         return newBuilder(this);
158     }
159     /**
160      * Creates a new {@code Plugin} instance using the specified inherited.
161      *
162      * @param inherited the new {@code String} to use
163      * @return a {@code Plugin} with the specified inherited
164      */
165     @Nonnull
166     public Plugin withInherited(String inherited) {
167         return newBuilder(this, true).inherited(inherited).build();
168     }
169     /**
170      * Creates a new {@code Plugin} instance using the specified configuration.
171      *
172      * @param configuration the new {@code XmlNode} to use
173      * @return a {@code Plugin} with the specified configuration
174      */
175     @Nonnull
176     public Plugin withConfiguration(XmlNode configuration) {
177         return newBuilder(this, true).configuration(configuration).build();
178     }
179     /**
180      * Creates a new {@code Plugin} instance using the specified groupId.
181      *
182      * @param groupId the new {@code String} to use
183      * @return a {@code Plugin} with the specified groupId
184      */
185     @Nonnull
186     public Plugin withGroupId(String groupId) {
187         return newBuilder(this, true).groupId(groupId).build();
188     }
189     /**
190      * Creates a new {@code Plugin} instance using the specified artifactId.
191      *
192      * @param artifactId the new {@code String} to use
193      * @return a {@code Plugin} with the specified artifactId
194      */
195     @Nonnull
196     public Plugin withArtifactId(String artifactId) {
197         return newBuilder(this, true).artifactId(artifactId).build();
198     }
199     /**
200      * Creates a new {@code Plugin} instance using the specified version.
201      *
202      * @param version the new {@code String} to use
203      * @return a {@code Plugin} with the specified version
204      */
205     @Nonnull
206     public Plugin withVersion(String version) {
207         return newBuilder(this, true).version(version).build();
208     }
209     /**
210      * Creates a new {@code Plugin} instance using the specified extensions.
211      *
212      * @param extensions the new {@code String} to use
213      * @return a {@code Plugin} with the specified extensions
214      */
215     @Nonnull
216     public Plugin withExtensions(String extensions) {
217         return newBuilder(this, true).extensions(extensions).build();
218     }
219     /**
220      * Creates a new {@code Plugin} instance using the specified executions.
221      *
222      * @param executions the new {@code Collection<PluginExecution>} to use
223      * @return a {@code Plugin} with the specified executions
224      */
225     @Nonnull
226     public Plugin withExecutions(Collection<PluginExecution> executions) {
227         return newBuilder(this, true).executions(executions).build();
228     }
229     /**
230      * Creates a new {@code Plugin} instance using the specified dependencies.
231      *
232      * @param dependencies the new {@code Collection<Dependency>} to use
233      * @return a {@code Plugin} with the specified dependencies
234      */
235     @Nonnull
236     public Plugin withDependencies(Collection<Dependency> dependencies) {
237         return newBuilder(this, true).dependencies(dependencies).build();
238     }
239 
240     /**
241      * Creates a new {@code Plugin} instance.
242      * Equivalent to {@code newInstance(true)}.
243      * @see #newInstance(boolean)
244      *
245      * @return a new {@code Plugin}
246      */
247     @Nonnull
248     public static Plugin newInstance() {
249         return newInstance(true);
250     }
251 
252     /**
253      * Creates a new {@code Plugin} instance using default values or not.
254      * Equivalent to {@code newBuilder(withDefaults).build()}.
255      *
256      * @param withDefaults the boolean indicating whether default values should be used
257      * @return a new {@code Plugin}
258      */
259     @Nonnull
260     public static Plugin newInstance(boolean withDefaults) {
261         return newBuilder(withDefaults).build();
262     }
263 
264     /**
265      * Creates a new {@code Plugin} builder instance.
266      * Equivalent to {@code newBuilder(true)}.
267      * @see #newBuilder(boolean)
268      *
269      * @return a new {@code Builder}
270      */
271     @Nonnull
272     public static Builder newBuilder() {
273         return newBuilder(true);
274     }
275 
276     /**
277      * Creates a new {@code Plugin} builder instance using default values or not.
278      *
279      * @param withDefaults the boolean indicating whether default values should be used
280      * @return a new {@code Builder}
281      */
282     @Nonnull
283     public static Builder newBuilder(boolean withDefaults) {
284         return new Builder(withDefaults);
285     }
286 
287     /**
288      * Creates a new {@code Plugin} builder instance using the specified object as a basis.
289      * Equivalent to {@code newBuilder(from, false)}.
290      *
291      * @param from the {@code Plugin} instance to use as a basis
292      * @return a new {@code Builder}
293      */
294     @Nonnull
295     public static Builder newBuilder(Plugin from) {
296         return newBuilder(from, false);
297     }
298 
299     /**
300      * Creates a new {@code Plugin} builder instance using the specified object as a basis.
301      *
302      * @param from the {@code Plugin} instance to use as a basis
303      * @param forceCopy the boolean indicating if a copy should be forced
304      * @return a new {@code Builder}
305      */
306     @Nonnull
307     public static Builder newBuilder(Plugin from, boolean forceCopy) {
308         return new Builder(from, forceCopy);
309     }
310 
311     /**
312      * Builder class used to create Plugin instances.
313      * @see #with()
314      * @see #newBuilder()
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> locations = null;
425             InputLocation location = null;
426             InputLocation inheritedLocation = null;
427             InputLocation configurationLocation = null;
428             InputLocation groupIdLocation = null;
429             InputLocation artifactIdLocation = null;
430             InputLocation versionLocation = null;
431             InputLocation extensionsLocation = null;
432             InputLocation executionsLocation = null;
433             InputLocation dependenciesLocation = null;
434             if (this.locations != null) {
435                 locations = this.locations;
436             }
437             return new Plugin(
438                 inherited != null ? inherited : (base != null ? base.inherited : null),
439                 configuration != null ? configuration : (base != null ? base.configuration : null),
440                 groupId != null ? groupId : (base != null ? base.groupId : null),
441                 artifactId != null ? artifactId : (base != null ? base.artifactId : null),
442                 version != null ? version : (base != null ? base.version : null),
443                 extensions != null ? extensions : (base != null ? base.extensions : null),
444                 executions != null ? executions : (base != null ? base.executions : null),
445                 dependencies != null ? dependencies : (base != null ? base.dependencies : null),
446                 locations != null ? locations : (base != null ? base.locations : null)
447             );
448         }
449     }
450 
451 
452             
453     public boolean isExtensions()
454     {
455         return ( getExtensions() != null ) ? Boolean.parseBoolean( getExtensions() ) : false;
456     }
457             
458           
459 
460             
461     private java.util.Map<String, PluginExecution> executionMap = null;
462 
463     /**
464      * Reset the {@code executionMap} field to {@code null}
465      */
466     public void flushExecutionMap()
467     {
468         this.executionMap = null;
469     }
470     /**
471      * @return a Map of executions field with {@code PluginExecution#getId()} as key
472      * @see PluginExecution#getId()
473      */
474     public java.util.Map<String, PluginExecution> getExecutionsAsMap()
475     {
476         if ( executionMap == null )
477         {
478             executionMap = new java.util.LinkedHashMap<String, PluginExecution>();
479             for ( java.util.Iterator<PluginExecution> i = getExecutions().iterator(); i.hasNext(); )
480             {
481                 PluginExecution exec = (PluginExecution) i.next();
482                 if ( executionMap.containsKey( exec.getId() ) )
483                 {
484                     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" );
485                 }
486                 executionMap.put( exec.getId(), exec );
487             }
488         }
489         return executionMap;
490     }
491 
492     /**
493      * Gets the identifier of the plugin.
494      *
495      * @return the plugin id in the form {@code <groupId>:<artifactId>:<version>}, never {@code null}
496      */
497     public String getId()
498     {
499         StringBuilder id = new StringBuilder( 128 );
500 
501         id.append( ( getGroupId() == null ) ? "[unknown-group-id]" : getGroupId() );
502         id.append( ":" );
503         id.append( ( getArtifactId() == null ) ? "[unknown-artifact-id]" : getArtifactId() );
504         id.append( ":" );
505         id.append( ( getVersion() == null ) ? "[unknown-version]" : getVersion() );
506 
507         return id.toString();
508     }
509 
510     /**
511      * @return the key of the plugin, ie {@code groupId:artifactId}
512      */
513     public String getKey()
514     {
515         return constructKey( getGroupId(), getArtifactId() );
516     }
517 
518     /**
519      * @param groupId The group ID of the plugin in the repository
520      * @param artifactId The artifact ID of the reporting plugin in the repository
521      * @return the key of the plugin, ie {@code groupId:artifactId}
522      */
523     public static String constructKey( String groupId, String artifactId )
524     {
525         return groupId + ":" + artifactId;
526     }
527 
528             
529           
530 
531             
532     /**
533      * @see java.lang.Object#equals(java.lang.Object)
534      */
535     public boolean equals( Object other )
536     {
537         if ( other instanceof Plugin )
538         {
539             Plugin otherPlugin = (Plugin) other;
540 
541             return getKey().equals( otherPlugin.getKey() );
542         }
543 
544         return false;
545     }
546 
547     /**
548      * @see java.lang.Object#hashCode()
549      */
550     public int hashCode()
551     {
552         return getKey().hashCode();
553     }
554 
555     /**
556      * @see java.lang.Object#toString()
557      */
558     public String toString()
559     {
560         return "Plugin [" + getKey() + "]";
561     }
562             
563           
564 }