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     public InputLocation getLocation(Object key) {
226         return locations.get(key);
227     }
228 
229     /**
230      * Gets the keys of the locations of the input source.
231      */
232     public Set<Object> getLocationKeys() {
233         return locations.keySet();
234     }
235 
236     protected Stream<Object> getLocationKeyStream() {
237         return locations.keySet().stream();
238     }
239 
240     /**
241      * Gets the input location that caused this model to be read.
242      */
243     public InputLocation getImportedFrom() {
244         return importedFrom;
245     }
246 
247     /**
248      * Creates a new builder with this object as the basis.
249      *
250      * @return a {@code Builder}
251      */
252     @Nonnull
253     public Builder with() {
254         return newBuilder(this);
255     }
256     /**
257      * Creates a new {@code ModelBase} instance using the specified modules.
258      *
259      * @param modules the new {@code Collection<String>} to use
260      * @return a {@code ModelBase} with the specified modules
261      */
262     @Deprecated(since = "4.0.0")
263     @Nonnull
264     public ModelBase withModules(Collection<String> modules) {
265         return newBuilder(this, true).modules(modules).build();
266     }
267     /**
268      * Creates a new {@code ModelBase} instance using the specified subprojects.
269      *
270      * @param subprojects the new {@code Collection<String>} to use
271      * @return a {@code ModelBase} with the specified subprojects
272      */
273     @Nonnull
274     public ModelBase withSubprojects(Collection<String> subprojects) {
275         return newBuilder(this, true).subprojects(subprojects).build();
276     }
277     /**
278      * Creates a new {@code ModelBase} instance using the specified distributionManagement.
279      *
280      * @param distributionManagement the new {@code DistributionManagement} to use
281      * @return a {@code ModelBase} with the specified distributionManagement
282      */
283     @Nonnull
284     public ModelBase withDistributionManagement(DistributionManagement distributionManagement) {
285         return newBuilder(this, true).distributionManagement(distributionManagement).build();
286     }
287     /**
288      * Creates a new {@code ModelBase} instance using the specified properties.
289      *
290      * @param properties the new {@code Map<String, String>} to use
291      * @return a {@code ModelBase} with the specified properties
292      */
293     @Nonnull
294     public ModelBase withProperties(Map<String, String> properties) {
295         return newBuilder(this, true).properties(properties).build();
296     }
297     /**
298      * Creates a new {@code ModelBase} instance using the specified dependencyManagement.
299      *
300      * @param dependencyManagement the new {@code DependencyManagement} to use
301      * @return a {@code ModelBase} with the specified dependencyManagement
302      */
303     @Nonnull
304     public ModelBase withDependencyManagement(DependencyManagement dependencyManagement) {
305         return newBuilder(this, true).dependencyManagement(dependencyManagement).build();
306     }
307     /**
308      * Creates a new {@code ModelBase} instance using the specified dependencies.
309      *
310      * @param dependencies the new {@code Collection<Dependency>} to use
311      * @return a {@code ModelBase} with the specified dependencies
312      */
313     @Nonnull
314     public ModelBase withDependencies(Collection<Dependency> dependencies) {
315         return newBuilder(this, true).dependencies(dependencies).build();
316     }
317     /**
318      * Creates a new {@code ModelBase} instance using the specified repositories.
319      *
320      * @param repositories the new {@code Collection<Repository>} to use
321      * @return a {@code ModelBase} with the specified repositories
322      */
323     @Nonnull
324     public ModelBase withRepositories(Collection<Repository> repositories) {
325         return newBuilder(this, true).repositories(repositories).build();
326     }
327     /**
328      * Creates a new {@code ModelBase} instance using the specified pluginRepositories.
329      *
330      * @param pluginRepositories the new {@code Collection<Repository>} to use
331      * @return a {@code ModelBase} with the specified pluginRepositories
332      */
333     @Nonnull
334     public ModelBase withPluginRepositories(Collection<Repository> pluginRepositories) {
335         return newBuilder(this, true).pluginRepositories(pluginRepositories).build();
336     }
337     /**
338      * Creates a new {@code ModelBase} instance using the specified reporting.
339      *
340      * @param reporting the new {@code Reporting} to use
341      * @return a {@code ModelBase} with the specified reporting
342      */
343     @Nonnull
344     public ModelBase withReporting(Reporting reporting) {
345         return newBuilder(this, true).reporting(reporting).build();
346     }
347 
348     /**
349      * Creates a new {@code ModelBase} instance.
350      * Equivalent to {@code newInstance(true)}.
351      * @see #newInstance(boolean)
352      *
353      * @return a new {@code ModelBase}
354      */
355     @Nonnull
356     public static ModelBase newInstance() {
357         return newInstance(true);
358     }
359 
360     /**
361      * Creates a new {@code ModelBase} instance using default values or not.
362      * Equivalent to {@code newBuilder(withDefaults).build()}.
363      *
364      * @param withDefaults the boolean indicating whether default values should be used
365      * @return a new {@code ModelBase}
366      */
367     @Nonnull
368     public static ModelBase newInstance(boolean withDefaults) {
369         return newBuilder(withDefaults).build();
370     }
371 
372     /**
373      * Creates a new {@code ModelBase} builder instance.
374      * Equivalent to {@code newBuilder(true)}.
375      * @see #newBuilder(boolean)
376      *
377      * @return a new {@code Builder}
378      */
379     @Nonnull
380     public static Builder newBuilder() {
381         return newBuilder(true);
382     }
383 
384     /**
385      * Creates a new {@code ModelBase} builder instance using default values or not.
386      *
387      * @param withDefaults the boolean indicating whether default values should be used
388      * @return a new {@code Builder}
389      */
390     @Nonnull
391     public static Builder newBuilder(boolean withDefaults) {
392         return new Builder(withDefaults);
393     }
394 
395     /**
396      * Creates a new {@code ModelBase} builder instance using the specified object as a basis.
397      * Equivalent to {@code newBuilder(from, false)}.
398      *
399      * @param from the {@code ModelBase} instance to use as a basis
400      * @return a new {@code Builder}
401      */
402     @Nonnull
403     public static Builder newBuilder(ModelBase from) {
404         return newBuilder(from, false);
405     }
406 
407     /**
408      * Creates a new {@code ModelBase} builder instance using the specified object as a basis.
409      *
410      * @param from the {@code ModelBase} instance to use as a basis
411      * @param forceCopy the boolean indicating if a copy should be forced
412      * @return a new {@code Builder}
413      */
414     @Nonnull
415     public static Builder newBuilder(ModelBase from, boolean forceCopy) {
416         return new Builder(from, forceCopy);
417     }
418 
419     /**
420      * Builder class used to create ModelBase instances.
421      * @see #with()
422      * @see #newBuilder()
423      */
424     @NotThreadSafe
425     public static class Builder
426     {
427         ModelBase base;
428         Collection<String> modules;
429         Collection<String> subprojects;
430         DistributionManagement distributionManagement;
431         Map<String, String> properties;
432         DependencyManagement dependencyManagement;
433         Collection<Dependency> dependencies;
434         Collection<Repository> repositories;
435         Collection<Repository> pluginRepositories;
436         Reporting reporting;
437         Map<Object, InputLocation> locations;
438         InputLocation importedFrom;
439 
440         protected Builder(boolean withDefaults) {
441             if (withDefaults) {
442             }
443         }
444 
445         protected Builder(ModelBase base, boolean forceCopy) {
446             if (forceCopy) {
447                 this.modules = base.modules;
448                 this.subprojects = base.subprojects;
449                 this.distributionManagement = base.distributionManagement;
450                 this.properties = base.properties;
451                 this.dependencyManagement = base.dependencyManagement;
452                 this.dependencies = base.dependencies;
453                 this.repositories = base.repositories;
454                 this.pluginRepositories = base.pluginRepositories;
455                 this.reporting = base.reporting;
456                 this.locations = base.locations;
457                 this.importedFrom = base.importedFrom;
458             } else {
459                 this.base = base;
460             }
461         }
462 
463         @Deprecated(since = "4.0.0")
464         @Nonnull
465         public Builder modules(Collection<String> modules) {
466             this.modules = modules;
467             return this;
468         }
469 
470         @Nonnull
471         public Builder subprojects(Collection<String> subprojects) {
472             this.subprojects = subprojects;
473             return this;
474         }
475 
476         @Nonnull
477         public Builder distributionManagement(DistributionManagement distributionManagement) {
478             this.distributionManagement = distributionManagement;
479             return this;
480         }
481 
482         @Nonnull
483         public Builder properties(Map<String, String> properties) {
484             this.properties = properties;
485             return this;
486         }
487 
488         @Nonnull
489         public Builder dependencyManagement(DependencyManagement dependencyManagement) {
490             this.dependencyManagement = dependencyManagement;
491             return this;
492         }
493 
494         @Nonnull
495         public Builder dependencies(Collection<Dependency> dependencies) {
496             this.dependencies = dependencies;
497             return this;
498         }
499 
500         @Nonnull
501         public Builder repositories(Collection<Repository> repositories) {
502             this.repositories = repositories;
503             return this;
504         }
505 
506         @Nonnull
507         public Builder pluginRepositories(Collection<Repository> pluginRepositories) {
508             this.pluginRepositories = pluginRepositories;
509             return this;
510         }
511 
512         @Nonnull
513         public Builder reporting(Reporting reporting) {
514             this.reporting = reporting;
515             return this;
516         }
517 
518 
519         @Nonnull
520         public Builder location(Object key, InputLocation location) {
521             if (location != null) {
522                 if (!(this.locations instanceof HashMap)) {
523                     this.locations = this.locations != null ? new HashMap<>(this.locations) : new HashMap<>();
524                 }
525                 this.locations.put(key, location);
526             }
527             return this;
528         }
529 
530         @Nonnull
531         public Builder importedFrom(InputLocation importedFrom) {
532             this.importedFrom = importedFrom;
533             return this;
534         }
535 
536         @Nonnull
537         public ModelBase build() {
538             // this method should not contain any logic other than creating (or reusing) an object in order to ease subclassing
539             if (base != null
540                     && (modules == null || modules == base.modules)
541                     && (subprojects == null || subprojects == base.subprojects)
542                     && (distributionManagement == null || distributionManagement == base.distributionManagement)
543                     && (properties == null || properties == base.properties)
544                     && (dependencyManagement == null || dependencyManagement == base.dependencyManagement)
545                     && (dependencies == null || dependencies == base.dependencies)
546                     && (repositories == null || repositories == base.repositories)
547                     && (pluginRepositories == null || pluginRepositories == base.pluginRepositories)
548                     && (reporting == null || reporting == base.reporting)
549             ) {
550                 return base;
551             }
552             return new ModelBase(this);
553         }
554 
555         Map<Object, InputLocation> computeLocations() {
556             Map<Object, InputLocation> newlocs = locations != null ? locations : Map.of();
557             Map<Object, InputLocation> oldlocs = base != null ? base.locations : Map.of();
558             if (newlocs.isEmpty()) {
559                 return Map.copyOf(oldlocs);
560             }
561             if (oldlocs.isEmpty()) {
562                 return Map.copyOf(newlocs);
563             }
564             return Stream.concat(newlocs.entrySet().stream(), oldlocs.entrySet().stream())
565                     // Keep value from newlocs in case of duplicates
566                     .collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1));
567         }
568     }
569 
570 }