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