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  
26  /**
27   * Base class for the {@code Model} and the {@code Profile} objects.
28   */
29  @Experimental
30  @Generated @ThreadSafe @Immutable
31  public class ModelBase
32      implements Serializable, InputLocationTracker
33  {
34      /**
35       * @deprecated Use {@link #subprojects} instead.
36       */
37      @Deprecated(since = "4.0.0")
38      final List<String> modules;
39      /**
40       * The subprojects (formerly called modules) to build as a part of this
41       * project. Each subproject listed is a relative path to the directory containing the subproject.
42       * To be consistent with the way default URLs are calculated from parent, it is recommended
43       * to have subproject names match artifact ids.
44       */
45      final List<String> subprojects;
46      /**
47       * Distribution information for a project that enables deployment of the site
48       * and artifacts to remote web servers and repositories respectively.
49       */
50      final DistributionManagement distributionManagement;
51      /**
52       * Properties that can be used throughout the POM as a substitution, and
53       * are used as filters in resources if enabled.
54       * The format is {@code <name>value</name>}.
55       */
56      final Map<String, String> properties;
57      /**
58       * Default dependency information for projects that inherit from this one. The
59       * dependencies in this section are not immediately resolved. Instead, when a POM derived
60       * from this one declares a dependency described by a matching groupId and artifactId, the
61       * version and other values from this section are used for that dependency if they were not
62       * already specified.
63       */
64      final DependencyManagement dependencyManagement;
65      /**
66       * This element describes all the dependencies associated with a project.
67       * These dependencies are used to construct a classpath for your
68       * project during the build process. They are automatically downloaded from the
69       * repositories defined in this project.
70       * 
71       * @see <a href="https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html">Dependency mechanism</a>
72       */
73      final List<Dependency> dependencies;
74      /**
75       * The lists of the remote repositories for discovering dependencies and
76       * extensions.
77       */
78      final List<Repository> repositories;
79      /**
80       * The lists of the remote repositories for discovering plugins for builds and
81       * reports.
82       */
83      final List<Repository> pluginRepositories;
84      /**
85       * This element includes the specification of report plugins to use
86       * to generate the reports on the Maven-generated site.
87       * These reports will be run when a user executes {@code mvn site}.
88       * All the reports will be included in the navigation bar for browsing.
89       */
90      final Reporting reporting;
91      /** Locations */
92      final Map<Object, InputLocation> locations;
93      /** Location tracking */
94      final InputLocation importedFrom;
95  
96      /**
97        * Constructor for this class, to be called from its subclasses and {@link Builder}.
98        * @see Builder#build()
99        */
100     protected ModelBase(Builder builder) {
101         this.modules = ImmutableCollections.copy(builder.modules != null ? builder.modules : (builder.base != null ? builder.base.modules : null));
102         this.subprojects = ImmutableCollections.copy(builder.subprojects != null ? builder.subprojects : (builder.base != null ? builder.base.subprojects : null));
103         this.distributionManagement = builder.distributionManagement != null ? builder.distributionManagement : (builder.base != null ? builder.base.distributionManagement : null);
104         this.properties = ImmutableCollections.copy(builder.properties != null ? builder.properties : (builder.base != null ? builder.base.properties : null));
105         this.dependencyManagement = builder.dependencyManagement != null ? builder.dependencyManagement : (builder.base != null ? builder.base.dependencyManagement : null);
106         this.dependencies = ImmutableCollections.copy(builder.dependencies != null ? builder.dependencies : (builder.base != null ? builder.base.dependencies : null));
107         this.repositories = ImmutableCollections.copy(builder.repositories != null ? builder.repositories : (builder.base != null ? builder.base.repositories : null));
108         this.pluginRepositories = ImmutableCollections.copy(builder.pluginRepositories != null ? builder.pluginRepositories : (builder.base != null ? builder.base.pluginRepositories : null));
109         this.reporting = builder.reporting != null ? builder.reporting : (builder.base != null ? builder.base.reporting : null);
110         this.locations = builder.computeLocations();
111         this.importedFrom = builder.importedFrom;
112     }
113 
114     /**
115      * @deprecated Use {@link #subprojects} instead.
116      *
117      * @return a {@code List<String>}
118      */
119     @Deprecated(since = "4.0.0")
120     @Nonnull
121     public List<String> getModules() {
122         return this.modules;
123     }
124 
125     /**
126      * The subprojects (formerly called modules) to build as a part of this
127      * project. Each subproject listed is a relative path to the directory containing the subproject.
128      * To be consistent with the way default URLs are calculated from parent, it is recommended
129      * to have subproject names match artifact ids.
130      *
131      * @return a {@code List<String>}
132      */
133     @Nonnull
134     public List<String> getSubprojects() {
135         return this.subprojects;
136     }
137 
138     /**
139      * Distribution information for a project that enables deployment of the site
140      * and artifacts to remote web servers and repositories respectively.
141      *
142      * @return a {@code DistributionManagement}
143      */
144     public DistributionManagement getDistributionManagement() {
145         return this.distributionManagement;
146     }
147 
148     /**
149      * Properties that can be used throughout the POM as a substitution, and
150      * are used as filters in resources if enabled.
151      * The format is {@code <name>value</name>}.
152      *
153      * @return a {@code Map<String, String>}
154      */
155     @Nonnull
156     public Map<String, String> getProperties() {
157         return this.properties;
158     }
159 
160     /**
161      * Default dependency information for projects that inherit from this one. The
162      * dependencies in this section are not immediately resolved. Instead, when a POM derived
163      * from this one declares a dependency described by a matching groupId and artifactId, the
164      * version and other values from this section are used for that dependency if they were not
165      * already specified.
166      *
167      * @return a {@code DependencyManagement}
168      */
169     public DependencyManagement getDependencyManagement() {
170         return this.dependencyManagement;
171     }
172 
173     /**
174      * This element describes all the dependencies associated with a project.
175      * These dependencies are used to construct a classpath for your
176      * project during the build process. They are automatically downloaded from the
177      * repositories defined in this project.
178      * 
179      * @see <a href="https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html">Dependency mechanism</a>
180      *
181      * @return a {@code List<Dependency>}
182      */
183     @Nonnull
184     public List<Dependency> getDependencies() {
185         return this.dependencies;
186     }
187 
188     /**
189      * The lists of the remote repositories for discovering dependencies and
190      * extensions.
191      *
192      * @return a {@code List<Repository>}
193      */
194     @Nonnull
195     public List<Repository> getRepositories() {
196         return this.repositories;
197     }
198 
199     /**
200      * The lists of the remote repositories for discovering plugins for builds and
201      * reports.
202      *
203      * @return a {@code List<Repository>}
204      */
205     @Nonnull
206     public List<Repository> getPluginRepositories() {
207         return this.pluginRepositories;
208     }
209 
210     /**
211      * This element includes the specification of report plugins to use
212      * to generate the reports on the Maven-generated site.
213      * These reports will be run when a user executes {@code mvn site}.
214      * All the reports will be included in the navigation bar for browsing.
215      *
216      * @return a {@code Reporting}
217      */
218     public Reporting getReporting() {
219         return this.reporting;
220     }
221 
222     /**
223      * Gets the location of the specified field in the input source.
224      *
225      * @param key the key of the field, must not be {@code null}
226      * @return the location of the field in the input source or {@code null} if unknown
227      * @throws NullPointerException if {@code key} is {@code null}
228      */
229     public InputLocation getLocation(Object key) {
230         Objects.requireNonNull(key, "key");
231         return locations.get(key);
232     }
233 
234     /**
235      * Gets the keys of the locations of the input source.
236      */
237     public Set<Object> getLocationKeys() {
238         return locations.keySet();
239     }
240 
241     protected Stream<Object> getLocationKeyStream() {
242         return locations.keySet().stream();
243     }
244 
245     /**
246      * Gets the input location that caused this model to be read.
247      */
248     public InputLocation getImportedFrom() {
249         return importedFrom;
250     }
251 
252     /**
253      * Creates a new builder with this object as the basis.
254      *
255      * @return a {@code Builder}
256      */
257     @Nonnull
258     public Builder with() {
259         return newBuilder(this);
260     }
261     /**
262      * Creates a new {@code ModelBase} instance using the specified modules.
263      *
264      * @param modules the new {@code Collection<String>} to use
265      * @return a {@code ModelBase} with the specified modules
266      */
267     @Deprecated(since = "4.0.0")
268     @Nonnull
269     public ModelBase withModules(Collection<String> modules) {
270         return newBuilder(this, true).modules(modules).build();
271     }
272     /**
273      * Creates a new {@code ModelBase} instance using the specified subprojects.
274      *
275      * @param subprojects the new {@code Collection<String>} to use
276      * @return a {@code ModelBase} with the specified subprojects
277      */
278     @Nonnull
279     public ModelBase withSubprojects(Collection<String> subprojects) {
280         return newBuilder(this, true).subprojects(subprojects).build();
281     }
282     /**
283      * Creates a new {@code ModelBase} instance using the specified distributionManagement.
284      *
285      * @param distributionManagement the new {@code DistributionManagement} to use
286      * @return a {@code ModelBase} with the specified distributionManagement
287      */
288     @Nonnull
289     public ModelBase withDistributionManagement(DistributionManagement distributionManagement) {
290         return newBuilder(this, true).distributionManagement(distributionManagement).build();
291     }
292     /**
293      * Creates a new {@code ModelBase} instance using the specified properties.
294      *
295      * @param properties the new {@code Map<String, String>} to use
296      * @return a {@code ModelBase} with the specified properties
297      */
298     @Nonnull
299     public ModelBase withProperties(Map<String, String> properties) {
300         return newBuilder(this, true).properties(properties).build();
301     }
302     /**
303      * Creates a new {@code ModelBase} instance using the specified dependencyManagement.
304      *
305      * @param dependencyManagement the new {@code DependencyManagement} to use
306      * @return a {@code ModelBase} with the specified dependencyManagement
307      */
308     @Nonnull
309     public ModelBase withDependencyManagement(DependencyManagement dependencyManagement) {
310         return newBuilder(this, true).dependencyManagement(dependencyManagement).build();
311     }
312     /**
313      * Creates a new {@code ModelBase} instance using the specified dependencies.
314      *
315      * @param dependencies the new {@code Collection<Dependency>} to use
316      * @return a {@code ModelBase} with the specified dependencies
317      */
318     @Nonnull
319     public ModelBase withDependencies(Collection<Dependency> dependencies) {
320         return newBuilder(this, true).dependencies(dependencies).build();
321     }
322     /**
323      * Creates a new {@code ModelBase} instance using the specified repositories.
324      *
325      * @param repositories the new {@code Collection<Repository>} to use
326      * @return a {@code ModelBase} with the specified repositories
327      */
328     @Nonnull
329     public ModelBase withRepositories(Collection<Repository> repositories) {
330         return newBuilder(this, true).repositories(repositories).build();
331     }
332     /**
333      * Creates a new {@code ModelBase} instance using the specified pluginRepositories.
334      *
335      * @param pluginRepositories the new {@code Collection<Repository>} to use
336      * @return a {@code ModelBase} with the specified pluginRepositories
337      */
338     @Nonnull
339     public ModelBase withPluginRepositories(Collection<Repository> pluginRepositories) {
340         return newBuilder(this, true).pluginRepositories(pluginRepositories).build();
341     }
342     /**
343      * Creates a new {@code ModelBase} instance using the specified reporting.
344      *
345      * @param reporting the new {@code Reporting} to use
346      * @return a {@code ModelBase} with the specified reporting
347      */
348     @Nonnull
349     public ModelBase withReporting(Reporting reporting) {
350         return newBuilder(this, true).reporting(reporting).build();
351     }
352 
353     /**
354      * Creates a new {@code ModelBase} instance.
355      * Equivalent to {@code newInstance(true)}.
356      * @see #newInstance(boolean)
357      *
358      * @return a new {@code ModelBase}
359      */
360     @Nonnull
361     public static ModelBase newInstance() {
362         return newInstance(true);
363     }
364 
365     /**
366      * Creates a new {@code ModelBase} instance using default values or not.
367      * Equivalent to {@code newBuilder(withDefaults).build()}.
368      *
369      * @param withDefaults the boolean indicating whether default values should be used
370      * @return a new {@code ModelBase}
371      */
372     @Nonnull
373     public static ModelBase newInstance(boolean withDefaults) {
374         return newBuilder(withDefaults).build();
375     }
376 
377     /**
378      * Creates a new {@code ModelBase} builder instance.
379      * Equivalent to {@code newBuilder(true)}.
380      * @see #newBuilder(boolean)
381      *
382      * @return a new {@code Builder}
383      */
384     @Nonnull
385     public static Builder newBuilder() {
386         return newBuilder(true);
387     }
388 
389     /**
390      * Creates a new {@code ModelBase} builder instance using default values or not.
391      *
392      * @param withDefaults the boolean indicating whether default values should be used
393      * @return a new {@code Builder}
394      */
395     @Nonnull
396     public static Builder newBuilder(boolean withDefaults) {
397         return new Builder(withDefaults);
398     }
399 
400     /**
401      * Creates a new {@code ModelBase} builder instance using the specified object as a basis.
402      * Equivalent to {@code newBuilder(from, false)}.
403      *
404      * @param from the {@code ModelBase} instance to use as a basis
405      * @return a new {@code Builder}
406      */
407     @Nonnull
408     public static Builder newBuilder(ModelBase from) {
409         return newBuilder(from, false);
410     }
411 
412     /**
413      * Creates a new {@code ModelBase} builder instance using the specified object as a basis.
414      *
415      * @param from the {@code ModelBase} instance to use as a basis
416      * @param forceCopy the boolean indicating if a copy should be forced
417      * @return a new {@code Builder}
418      */
419     @Nonnull
420     public static Builder newBuilder(ModelBase from, boolean forceCopy) {
421         return new Builder(from, forceCopy);
422     }
423 
424     /**
425      * Builder class used to create ModelBase instances.
426      * @see #with()
427      * @see #newBuilder()
428      */
429     @NotThreadSafe
430     public static class Builder
431     {
432         ModelBase base;
433         Collection<String> modules;
434         Collection<String> subprojects;
435         DistributionManagement distributionManagement;
436         Map<String, String> properties;
437         DependencyManagement dependencyManagement;
438         Collection<Dependency> dependencies;
439         Collection<Repository> repositories;
440         Collection<Repository> pluginRepositories;
441         Reporting reporting;
442         Map<Object, InputLocation> locations;
443         InputLocation importedFrom;
444 
445         protected Builder(boolean withDefaults) {
446             if (withDefaults) {
447             }
448         }
449 
450         protected Builder(ModelBase base, boolean forceCopy) {
451             if (forceCopy) {
452                 this.modules = base.modules;
453                 this.subprojects = base.subprojects;
454                 this.distributionManagement = base.distributionManagement;
455                 this.properties = base.properties;
456                 this.dependencyManagement = base.dependencyManagement;
457                 this.dependencies = base.dependencies;
458                 this.repositories = base.repositories;
459                 this.pluginRepositories = base.pluginRepositories;
460                 this.reporting = base.reporting;
461                 this.locations = base.locations;
462                 this.importedFrom = base.importedFrom;
463             } else {
464                 this.base = base;
465             }
466         }
467 
468         @Deprecated(since = "4.0.0")
469         @Nonnull
470         public Builder modules(Collection<String> modules) {
471             this.modules = modules;
472             return this;
473         }
474 
475         @Nonnull
476         public Builder subprojects(Collection<String> subprojects) {
477             this.subprojects = subprojects;
478             return this;
479         }
480 
481         @Nonnull
482         public Builder distributionManagement(DistributionManagement distributionManagement) {
483             this.distributionManagement = distributionManagement;
484             return this;
485         }
486 
487         @Nonnull
488         public Builder properties(Map<String, String> properties) {
489             this.properties = properties;
490             return this;
491         }
492 
493         @Nonnull
494         public Builder dependencyManagement(DependencyManagement dependencyManagement) {
495             this.dependencyManagement = dependencyManagement;
496             return this;
497         }
498 
499         @Nonnull
500         public Builder dependencies(Collection<Dependency> dependencies) {
501             this.dependencies = dependencies;
502             return this;
503         }
504 
505         @Nonnull
506         public Builder repositories(Collection<Repository> repositories) {
507             this.repositories = repositories;
508             return this;
509         }
510 
511         @Nonnull
512         public Builder pluginRepositories(Collection<Repository> pluginRepositories) {
513             this.pluginRepositories = pluginRepositories;
514             return this;
515         }
516 
517         @Nonnull
518         public Builder reporting(Reporting reporting) {
519             this.reporting = reporting;
520             return this;
521         }
522 
523 
524         @Nonnull
525         public Builder location(Object key, InputLocation location) {
526             if (location != null) {
527                 if (!(this.locations instanceof HashMap)) {
528                     this.locations = this.locations != null ? new HashMap<>(this.locations) : new HashMap<>();
529                 }
530                 this.locations.put(key, location);
531             }
532             return this;
533         }
534 
535         @Nonnull
536         public Builder importedFrom(InputLocation importedFrom) {
537             this.importedFrom = importedFrom;
538             return this;
539         }
540 
541         @Nonnull
542         public ModelBase build() {
543             // this method should not contain any logic other than creating (or reusing) an object in order to ease subclassing
544             if (base != null
545                     && (modules == null || modules == base.modules)
546                     && (subprojects == null || subprojects == base.subprojects)
547                     && (distributionManagement == null || distributionManagement == base.distributionManagement)
548                     && (properties == null || properties == base.properties)
549                     && (dependencyManagement == null || dependencyManagement == base.dependencyManagement)
550                     && (dependencies == null || dependencies == base.dependencies)
551                     && (repositories == null || repositories == base.repositories)
552                     && (pluginRepositories == null || pluginRepositories == base.pluginRepositories)
553                     && (reporting == null || reporting == base.reporting)
554             ) {
555                 return base;
556             }
557             return new ModelBase(this);
558         }
559 
560         Map<Object, InputLocation> computeLocations() {
561             Map<Object, InputLocation> newlocs = locations != null ? locations : Map.of();
562             Map<Object, InputLocation> oldlocs = base != null ? base.locations : Map.of();
563             if (newlocs.isEmpty()) {
564                 return Map.copyOf(oldlocs);
565             }
566             if (oldlocs.isEmpty()) {
567                 return Map.copyOf(newlocs);
568             }
569             return Stream.concat(newlocs.entrySet().stream(), oldlocs.entrySet().stream())
570                     // Keep value from newlocs in case of duplicates
571                     .collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1));
572         }
573     }
574 
575 }