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 java.util.Set;
15  import org.apache.maven.api.annotations.Experimental;
16  import org.apache.maven.api.annotations.Generated;
17  import org.apache.maven.api.annotations.Immutable;
18  import org.apache.maven.api.annotations.Nonnull;
19  import org.apache.maven.api.annotations.NotThreadSafe;
20  import org.apache.maven.api.annotations.ThreadSafe;
21  import org.apache.maven.api.xml.XmlNode;
22  
23  /**
24   * The {@code <plugin>} element contains information required for a plugin.
25   */
26  @Experimental
27  @Generated @ThreadSafe @Immutable
28  public class Plugin
29      extends ConfigurationContainer
30      implements Serializable, InputLocationTracker
31  {
32      /**
33       * The group ID of the plugin in the repository.
34       */
35      final String groupId;
36      /**
37       * The artifact ID of the plugin in the repository.
38       */
39      final String artifactId;
40      /**
41       * The version (or valid range of versions) of the plugin to be used.
42       */
43      final String version;
44      /**
45       * Whether to load Maven extensions (such as packaging and type handlers) from
46       * this plugin. For performance reasons, this should only be enabled when necessary. Note: While the type
47       * of this field is {@code String} for technical reasons, the semantic type is actually
48       * {@code Boolean}. Default value is {@code false}.
49       */
50      final String extensions;
51      /**
52       * Multiple specifications of a set of goals to execute during the build
53       * lifecycle, each having (possibly) a different configuration.
54       */
55      final List<PluginExecution> executions;
56      /**
57       * Additional dependencies that this project needs to introduce to the plugin's
58       * classloader.
59       */
60      final List<Dependency> dependencies;
61      /** Locations (this potentially hides the same name field from the super class) */
62      final Map<Object, InputLocation> locations;
63  
64      /**
65        * Constructor for this class, to be called from its subclasses and {@link Builder}.
66        * @see Builder#build()
67        */
68      protected Plugin(Builder builder) {
69          super(builder);
70          this.groupId = builder.groupId != null ? builder.groupId : (builder.base != null ? builder.base.groupId : null);
71          this.artifactId = builder.artifactId != null ? builder.artifactId : (builder.base != null ? builder.base.artifactId : null);
72          this.version = builder.version != null ? builder.version : (builder.base != null ? builder.base.version : null);
73          this.extensions = builder.extensions != null ? builder.extensions : (builder.base != null ? builder.base.extensions : null);
74          this.executions = ImmutableCollections.copy(builder.executions != null ? builder.executions : (builder.base != null ? builder.base.executions : null));
75          this.dependencies = ImmutableCollections.copy(builder.dependencies != null ? builder.dependencies : (builder.base != null ? builder.base.dependencies : null));
76          Map<Object, InputLocation> newlocs = builder.locations != null ? builder.locations : Collections.emptyMap();
77          Map<Object, InputLocation> oldlocs = builder.base != null && builder.base.locations != null ? builder.base.locations : Collections.emptyMap();
78          Map<Object, InputLocation> mutableLocations = new HashMap<>(super.locations);
79          mutableLocations.put("groupId", newlocs.containsKey("groupId") ? newlocs.get("groupId") : oldlocs.get("groupId"));
80          mutableLocations.put("artifactId", newlocs.containsKey("artifactId") ? newlocs.get("artifactId") : oldlocs.get("artifactId"));
81          mutableLocations.put("version", newlocs.containsKey("version") ? newlocs.get("version") : oldlocs.get("version"));
82          mutableLocations.put("extensions", newlocs.containsKey("extensions") ? newlocs.get("extensions") : oldlocs.get("extensions"));
83          mutableLocations.put("executions", newlocs.containsKey("executions") ? newlocs.get("executions") : oldlocs.get("executions"));
84          mutableLocations.put("dependencies", newlocs.containsKey("dependencies") ? newlocs.get("dependencies") : oldlocs.get("dependencies"));
85          this.locations = Collections.unmodifiableMap(mutableLocations);
86      }
87  
88      /**
89       * The group ID of the plugin in the repository.
90       *
91       * @return a {@code String}
92       */
93      public String getGroupId() {
94          return this.groupId;
95      }
96  
97      /**
98       * The artifact ID of the plugin in the repository.
99       *
100      * @return a {@code String}
101      */
102     public String getArtifactId() {
103         return this.artifactId;
104     }
105 
106     /**
107      * The version (or valid range of versions) of the plugin to be used.
108      *
109      * @return a {@code String}
110      */
111     public String getVersion() {
112         return this.version;
113     }
114 
115     /**
116      * Whether to load Maven extensions (such as packaging and type handlers) from
117      * this plugin. For performance reasons, this should only be enabled when necessary. Note: While the type
118      * of this field is {@code String} for technical reasons, the semantic type is actually
119      * {@code Boolean}. Default value is {@code false}.
120      *
121      * @return a {@code String}
122      */
123     public String getExtensions() {
124         return this.extensions;
125     }
126 
127     /**
128      * Multiple specifications of a set of goals to execute during the build
129      * lifecycle, each having (possibly) a different configuration.
130      *
131      * @return a {@code List<PluginExecution>}
132      */
133     @Nonnull
134     public List<PluginExecution> getExecutions() {
135         return this.executions;
136     }
137 
138     /**
139      * Additional dependencies that this project needs to introduce to the plugin's
140      * classloader.
141      *
142      * @return a {@code List<Dependency>}
143      */
144     @Nonnull
145     public List<Dependency> getDependencies() {
146         return this.dependencies;
147     }
148 
149     /**
150      * Gets the location of the specified field in the input source.
151      */
152     public InputLocation getLocation(Object key) {
153         return locations != null ? locations.get(key) : null;
154     }
155 
156     /**
157     * Gets the keys of the locations of the input source.
158     */
159     public Set<Object> getLocationKeys() {
160         return locations != null ? locations.keySet() : null;
161     }
162 
163     /**
164      * Creates a new builder with this object as the basis.
165      *
166      * @return a {@code Builder}
167      */
168     @Nonnull
169     public Builder with() {
170         return newBuilder(this);
171     }
172     /**
173      * Creates a new {@code Plugin} instance using the specified inherited.
174      *
175      * @param inherited the new {@code String} to use
176      * @return a {@code Plugin} with the specified inherited
177      */
178     @Nonnull
179     public Plugin withInherited(String inherited) {
180         return newBuilder(this, true).inherited(inherited).build();
181     }
182     /**
183      * Creates a new {@code Plugin} instance using the specified configuration.
184      *
185      * @param configuration the new {@code XmlNode} to use
186      * @return a {@code Plugin} with the specified configuration
187      */
188     @Nonnull
189     public Plugin withConfiguration(XmlNode configuration) {
190         return newBuilder(this, true).configuration(configuration).build();
191     }
192     /**
193      * Creates a new {@code Plugin} instance using the specified groupId.
194      *
195      * @param groupId the new {@code String} to use
196      * @return a {@code Plugin} with the specified groupId
197      */
198     @Nonnull
199     public Plugin withGroupId(String groupId) {
200         return newBuilder(this, true).groupId(groupId).build();
201     }
202     /**
203      * Creates a new {@code Plugin} instance using the specified artifactId.
204      *
205      * @param artifactId the new {@code String} to use
206      * @return a {@code Plugin} with the specified artifactId
207      */
208     @Nonnull
209     public Plugin withArtifactId(String artifactId) {
210         return newBuilder(this, true).artifactId(artifactId).build();
211     }
212     /**
213      * Creates a new {@code Plugin} instance using the specified version.
214      *
215      * @param version the new {@code String} to use
216      * @return a {@code Plugin} with the specified version
217      */
218     @Nonnull
219     public Plugin withVersion(String version) {
220         return newBuilder(this, true).version(version).build();
221     }
222     /**
223      * Creates a new {@code Plugin} instance using the specified extensions.
224      *
225      * @param extensions the new {@code String} to use
226      * @return a {@code Plugin} with the specified extensions
227      */
228     @Nonnull
229     public Plugin withExtensions(String extensions) {
230         return newBuilder(this, true).extensions(extensions).build();
231     }
232     /**
233      * Creates a new {@code Plugin} instance using the specified executions.
234      *
235      * @param executions the new {@code Collection<PluginExecution>} to use
236      * @return a {@code Plugin} with the specified executions
237      */
238     @Nonnull
239     public Plugin withExecutions(Collection<PluginExecution> executions) {
240         return newBuilder(this, true).executions(executions).build();
241     }
242     /**
243      * Creates a new {@code Plugin} instance using the specified dependencies.
244      *
245      * @param dependencies the new {@code Collection<Dependency>} to use
246      * @return a {@code Plugin} with the specified dependencies
247      */
248     @Nonnull
249     public Plugin withDependencies(Collection<Dependency> dependencies) {
250         return newBuilder(this, true).dependencies(dependencies).build();
251     }
252 
253     /**
254      * Creates a new {@code Plugin} instance.
255      * Equivalent to {@code newInstance(true)}.
256      * @see #newInstance(boolean)
257      *
258      * @return a new {@code Plugin}
259      */
260     @Nonnull
261     public static Plugin newInstance() {
262         return newInstance(true);
263     }
264 
265     /**
266      * Creates a new {@code Plugin} instance using default values or not.
267      * Equivalent to {@code newBuilder(withDefaults).build()}.
268      *
269      * @param withDefaults the boolean indicating whether default values should be used
270      * @return a new {@code Plugin}
271      */
272     @Nonnull
273     public static Plugin newInstance(boolean withDefaults) {
274         return newBuilder(withDefaults).build();
275     }
276 
277     /**
278      * Creates a new {@code Plugin} builder instance.
279      * Equivalent to {@code newBuilder(true)}.
280      * @see #newBuilder(boolean)
281      *
282      * @return a new {@code Builder}
283      */
284     @Nonnull
285     public static Builder newBuilder() {
286         return newBuilder(true);
287     }
288 
289     /**
290      * Creates a new {@code Plugin} builder instance using default values or not.
291      *
292      * @param withDefaults the boolean indicating whether default values should be used
293      * @return a new {@code Builder}
294      */
295     @Nonnull
296     public static Builder newBuilder(boolean withDefaults) {
297         return new Builder(withDefaults);
298     }
299 
300     /**
301      * Creates a new {@code Plugin} builder instance using the specified object as a basis.
302      * Equivalent to {@code newBuilder(from, false)}.
303      *
304      * @param from the {@code Plugin} instance to use as a basis
305      * @return a new {@code Builder}
306      */
307     @Nonnull
308     public static Builder newBuilder(Plugin from) {
309         return newBuilder(from, false);
310     }
311 
312     /**
313      * Creates a new {@code Plugin} builder instance using the specified object as a basis.
314      *
315      * @param from the {@code Plugin} instance to use as a basis
316      * @param forceCopy the boolean indicating if a copy should be forced
317      * @return a new {@code Builder}
318      */
319     @Nonnull
320     public static Builder newBuilder(Plugin from, boolean forceCopy) {
321         return new Builder(from, forceCopy);
322     }
323 
324     /**
325      * Builder class used to create Plugin instances.
326      * @see #with()
327      * @see #newBuilder()
328      */
329     @NotThreadSafe
330     public static class Builder
331         extends ConfigurationContainer.Builder
332     {
333         Plugin base;
334         String groupId;
335         String artifactId;
336         String version;
337         String extensions;
338         Collection<PluginExecution> executions;
339         Collection<Dependency> dependencies;
340 
341         protected Builder(boolean withDefaults) {
342             super(withDefaults);
343             if (withDefaults) {
344                 this.groupId = "org.apache.maven.plugins";
345             }
346         }
347 
348         protected Builder(Plugin base, boolean forceCopy) {
349             super(base, forceCopy);
350             if (forceCopy) {
351                 this.groupId = base.groupId;
352                 this.artifactId = base.artifactId;
353                 this.version = base.version;
354                 this.extensions = base.extensions;
355                 this.executions = base.executions;
356                 this.dependencies = base.dependencies;
357                 this.locations = base.locations;
358                 this.importedFrom = base.importedFrom;
359             } else {
360                 this.base = base;
361             }
362         }
363 
364         @Nonnull
365         public Builder inherited(String inherited) {
366             this.inherited = inherited;
367             return this;
368         }
369 
370         @Nonnull
371         public Builder configuration(XmlNode configuration) {
372             this.configuration = configuration;
373             return this;
374         }
375 
376         @Nonnull
377         public Builder groupId(String groupId) {
378             this.groupId = groupId;
379             return this;
380         }
381 
382         @Nonnull
383         public Builder artifactId(String artifactId) {
384             this.artifactId = artifactId;
385             return this;
386         }
387 
388         @Nonnull
389         public Builder version(String version) {
390             this.version = version;
391             return this;
392         }
393 
394         @Nonnull
395         public Builder extensions(String extensions) {
396             this.extensions = extensions;
397             return this;
398         }
399 
400         @Nonnull
401         public Builder executions(Collection<PluginExecution> executions) {
402             this.executions = executions;
403             return this;
404         }
405 
406         @Nonnull
407         public Builder dependencies(Collection<Dependency> dependencies) {
408             this.dependencies = dependencies;
409             return this;
410         }
411 
412 
413         @Nonnull
414         public Builder location(Object key, InputLocation location) {
415             if (location != null) {
416                 if (!(this.locations instanceof HashMap)) {
417                     this.locations = this.locations != null ? new HashMap<>(this.locations) : new HashMap<>();
418                 }
419                 this.locations.put(key, location);
420             }
421             return this;
422         }
423 
424         @Nonnull
425         public Builder importedFrom(InputLocation importedFrom) {
426             this.importedFrom = importedFrom;
427             return this;
428         }
429 
430         @Nonnull
431         public Plugin build() {
432             // this method should not contain any logic other than creating (or reusing) an object in order to ease subclassing
433             if (base != null
434                     && (inherited == null || inherited == base.inherited)
435                     && (configuration == null || configuration == base.configuration)
436                     && (groupId == null || groupId == base.groupId)
437                     && (artifactId == null || artifactId == base.artifactId)
438                     && (version == null || version == base.version)
439                     && (extensions == null || extensions == base.extensions)
440                     && (executions == null || executions == base.executions)
441                     && (dependencies == null || dependencies == base.dependencies)
442             ) {
443                 return base;
444             }
445             return new Plugin(this);
446         }
447     }
448 
449 
450             
451     public boolean isExtensions() {
452         return (getExtensions() != null) ? Boolean.parseBoolean(getExtensions()) : false;
453     }
454             
455           
456 
457             
458     /**
459      * Gets the identifier of the plugin.
460      *
461      * @return the plugin id in the form {@code <groupId>:<artifactId>:<version>}, never {@code null}
462      */
463     public String getId() {
464         return new StringBuilder(128)
465             .append((getGroupId() == null) ? "[unknown-group-id]" : getGroupId())
466             .append(":")
467             .append((getArtifactId() == null) ? "[unknown-artifact-id]" : getArtifactId())
468             .append(":")
469             .append((getVersion() == null) ? "[unknown-version]" : getVersion())
470             .toString();
471     }
472 
473     /**
474      * @return the key of the plugin, ie {@code groupId:artifactId}
475      */
476     public String getKey() {
477         return constructKey(getGroupId(), getArtifactId());
478     }
479 
480     /**
481      * @param groupId the group ID of the plugin in the repository
482      * @param artifactId the artifact ID of the reporting plugin in the repository
483      * @return the key of the plugin, ie {@code groupId:artifactId}
484      */
485     public static String constructKey(String groupId, String artifactId) {
486         return groupId + ":" + artifactId;
487     }
488 
489             
490           
491 
492             
493     /**
494      * @see java.lang.Object#equals(java.lang.Object)
495      */
496     public boolean equals(Object other) {
497         if (other instanceof Plugin) {
498             Plugin otherPlugin = (Plugin) other;
499             return getKey().equals(otherPlugin.getKey());
500         }
501         return false;
502     }
503 
504     /**
505      * @see java.lang.Object#hashCode()
506      */
507     public int hashCode() {
508         return getKey().hashCode();
509     }
510 
511     /**
512      * @see java.lang.Object#toString()
513      */
514     public String toString() {
515         return "Plugin [" + getKey() + "]";
516     }
517             
518           
519 }