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> newlocs = this.locations != null ? this.locations : Collections.emptyMap();
425             Map<Object, InputLocation> oldlocs = this.base != null && this.base.locations != null ? this.base.locations : Collections.emptyMap();
426             Map<Object, InputLocation> locations = new HashMap<>();
427             locations.put("", newlocs.containsKey("") ? newlocs.get("") : oldlocs.get(""));
428             locations.put("inherited", newlocs.containsKey("inherited") ? newlocs.get("inherited") : oldlocs.get("inherited"));
429             locations.put("configuration", newlocs.containsKey("configuration") ? newlocs.get("configuration") : oldlocs.get("configuration"));
430             locations.put("groupId", newlocs.containsKey("groupId") ? newlocs.get("groupId") : oldlocs.get("groupId"));
431             locations.put("artifactId", newlocs.containsKey("artifactId") ? newlocs.get("artifactId") : oldlocs.get("artifactId"));
432             locations.put("version", newlocs.containsKey("version") ? newlocs.get("version") : oldlocs.get("version"));
433             locations.put("extensions", newlocs.containsKey("extensions") ? newlocs.get("extensions") : oldlocs.get("extensions"));
434             locations.put("executions", newlocs.containsKey("executions") ? newlocs.get("executions") : oldlocs.get("executions"));
435             locations.put("dependencies", newlocs.containsKey("dependencies") ? newlocs.get("dependencies") : oldlocs.get("dependencies"));
436             return new Plugin(
437                 inherited != null ? inherited : (base != null ? base.inherited : null),
438                 configuration != null ? configuration : (base != null ? base.configuration : null),
439                 groupId != null ? groupId : (base != null ? base.groupId : null),
440                 artifactId != null ? artifactId : (base != null ? base.artifactId : null),
441                 version != null ? version : (base != null ? base.version : null),
442                 extensions != null ? extensions : (base != null ? base.extensions : null),
443                 executions != null ? executions : (base != null ? base.executions : null),
444                 dependencies != null ? dependencies : (base != null ? base.dependencies : null),
445                 locations
446             );
447         }
448     }
449 
450 
451             
452     public boolean isExtensions() {
453         return (getExtensions() != null) ? Boolean.parseBoolean(getExtensions()) : false;
454     }
455             
456           
457 
458             
459     private Map<String, PluginExecution> executionMap = null;
460 
461     /**
462      * Reset the {@code executionMap} field to {@code null}
463      */
464     public void flushExecutionMap() {
465         this.executionMap = null;
466     }
467 
468     /**
469      * @return a Map of executions field with {@code PluginExecution#getId()} as key
470      * @see PluginExecution#getId()
471      */
472     public Map<String, PluginExecution> getExecutionsAsMap() {
473         if (executionMap == null) {
474             executionMap = new java.util.LinkedHashMap<String, PluginExecution>();
475             for (java.util.Iterator<PluginExecution> i = getExecutions().iterator(); i.hasNext();) {
476                 PluginExecution exec = (PluginExecution) i.next();
477                 if (executionMap.containsKey(exec.getId())) {
478                     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");
479                 }
480                 executionMap.put(exec.getId(), exec);
481             }
482         }
483         return executionMap;
484     }
485 
486     /**
487      * Gets the identifier of the plugin.
488      *
489      * @return the plugin id in the form {@code <groupId>:<artifactId>:<version>}, never {@code null}
490      */
491     public String getId() {
492         return new StringBuilder(128)
493             .append((getGroupId() == null) ? "[unknown-group-id]" : getGroupId())
494             .append(":")
495             .append((getArtifactId() == null) ? "[unknown-artifact-id]" : getArtifactId())
496             .append(":")
497             .append((getVersion() == null) ? "[unknown-version]" : getVersion())
498             .toString();
499     }
500 
501     /**
502      * @return the key of the plugin, ie {@code groupId:artifactId}
503      */
504     public String getKey() {
505         return constructKey(getGroupId(), getArtifactId());
506     }
507 
508     /**
509      * @param groupId the group ID of the plugin in the repository
510      * @param artifactId the artifact ID of the reporting plugin in the repository
511      * @return the key of the plugin, ie {@code groupId:artifactId}
512      */
513     public static String constructKey(String groupId, String artifactId) {
514         return groupId + ":" + artifactId;
515     }
516 
517             
518           
519 
520             
521     /**
522      * @see java.lang.Object#equals(java.lang.Object)
523      */
524     public boolean equals(Object other) {
525         if (other instanceof Plugin) {
526             Plugin otherPlugin = (Plugin) other;
527             return getKey().equals(otherPlugin.getKey());
528         }
529         return false;
530     }
531 
532     /**
533      * @see java.lang.Object#hashCode()
534      */
535     public int hashCode() {
536         return getKey().hashCode();
537     }
538 
539     /**
540      * @see java.lang.Object#toString()
541      */
542     public String toString() {
543         return "Plugin [" + getKey() + "]";
544     }
545             
546           
547 }