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