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.Collections;
9   import java.util.HashMap;
10  import java.util.Map;
11  import java.util.Set;
12  import org.apache.maven.api.annotations.Experimental;
13  import org.apache.maven.api.annotations.Generated;
14  import org.apache.maven.api.annotations.Immutable;
15  import org.apache.maven.api.annotations.Nonnull;
16  import org.apache.maven.api.annotations.NotThreadSafe;
17  import org.apache.maven.api.annotations.ThreadSafe;
18  
19  /**
20   * The {@code <parent>} element contains information required to locate the parent project from which
21   * this project will inherit from.
22   * <p><strong>Note:</strong> The children of this element are not interpolated and must be given as literal values.</p>
23   */
24  @Experimental
25  @Generated @ThreadSafe @Immutable
26  public class Parent
27      implements Serializable, InputLocationTracker
28  {
29      /**
30       * The group id of the parent project to inherit from.
31       */
32      final String groupId;
33      /**
34       * The artifact id of the parent project to inherit from.
35       */
36      final String artifactId;
37      /**
38       * The version of the parent project to inherit.
39       */
40      final String version;
41      /**
42       * The relative path of the parent {@code pom.xml} file within the checkout.
43       * If not specified, it defaults to {@code ../pom.xml}.
44       * Maven looks for the parent POM first in this location on
45       * the filesystem, then the local repository, and lastly in the remote repo.
46       * {@code relativePath} allows you to select a different location,
47       * for example when your structure is flat, or deeper without an intermediate parent POM.
48       * However, the group ID, artifact ID and version are still required,
49       * and must match the file in the location given, or it will revert to the repository for the POM.
50       * This feature is only for enhancing the development in a local checkout of that project.
51       * Set the value to an empty string in case you want to disable the feature and always resolve
52       * the parent POM from the repositories.
53       */
54      final String relativePath;
55      /** Locations (this potentially hides the same name field from the super class) */
56      final Map<Object, InputLocation> locations;
57      /** Location tracking */
58      final InputLocation importedFrom;
59  
60      /**
61        * Constructor for this class, to be called from its subclasses and {@link Builder}.
62        * @see Builder#build()
63        */
64      protected Parent(Builder builder) {
65          this.groupId = builder.groupId != null ? builder.groupId : (builder.base != null ? builder.base.groupId : null);
66          this.artifactId = builder.artifactId != null ? builder.artifactId : (builder.base != null ? builder.base.artifactId : null);
67          this.version = builder.version != null ? builder.version : (builder.base != null ? builder.base.version : null);
68          this.relativePath = builder.relativePath != null ? builder.relativePath : (builder.base != null ? builder.base.relativePath : null);
69          Map<Object, InputLocation> newlocs = builder.locations != null ? builder.locations : Collections.emptyMap();
70          Map<Object, InputLocation> oldlocs = builder.base != null && builder.base.locations != null ? builder.base.locations : Collections.emptyMap();
71          Map<Object, InputLocation> mutableLocations = new HashMap<>();
72          this.importedFrom = builder.importedFrom;
73          mutableLocations.put("", newlocs.containsKey("") ? newlocs.get("") : oldlocs.get(""));
74          mutableLocations.put("groupId", newlocs.containsKey("groupId") ? newlocs.get("groupId") : oldlocs.get("groupId"));
75          mutableLocations.put("artifactId", newlocs.containsKey("artifactId") ? newlocs.get("artifactId") : oldlocs.get("artifactId"));
76          mutableLocations.put("version", newlocs.containsKey("version") ? newlocs.get("version") : oldlocs.get("version"));
77          mutableLocations.put("relativePath", newlocs.containsKey("relativePath") ? newlocs.get("relativePath") : oldlocs.get("relativePath"));
78          this.locations = Collections.unmodifiableMap(mutableLocations);
79      }
80  
81      /**
82       * The group id of the parent project to inherit from.
83       *
84       * @return a {@code String}
85       */
86      public String getGroupId() {
87          return this.groupId;
88      }
89  
90      /**
91       * The artifact id of the parent project to inherit from.
92       *
93       * @return a {@code String}
94       */
95      public String getArtifactId() {
96          return this.artifactId;
97      }
98  
99      /**
100      * The version of the parent project to inherit.
101      *
102      * @return a {@code String}
103      */
104     public String getVersion() {
105         return this.version;
106     }
107 
108     /**
109      * The relative path of the parent {@code pom.xml} file within the checkout.
110      * If not specified, it defaults to {@code ../pom.xml}.
111      * Maven looks for the parent POM first in this location on
112      * the filesystem, then the local repository, and lastly in the remote repo.
113      * {@code relativePath} allows you to select a different location,
114      * for example when your structure is flat, or deeper without an intermediate parent POM.
115      * However, the group ID, artifact ID and version are still required,
116      * and must match the file in the location given, or it will revert to the repository for the POM.
117      * This feature is only for enhancing the development in a local checkout of that project.
118      * Set the value to an empty string in case you want to disable the feature and always resolve
119      * the parent POM from the repositories.
120      *
121      * @return a {@code String}
122      */
123     public String getRelativePath() {
124         return this.relativePath;
125     }
126 
127     /**
128      * Gets the location of the specified field in the input source.
129      */
130     public InputLocation getLocation(Object key) {
131         return locations != null ? locations.get(key) : null;
132     }
133 
134     /**
135     * Gets the keys of the locations of the input source.
136     */
137     public Set<Object> getLocationKeys() {
138         return locations != null ? locations.keySet() : null;
139     }
140 
141     /**
142      * Gets the input location that caused this model to be read.
143      */
144     public InputLocation getImportedFrom()
145     {
146         return importedFrom;
147     }
148 
149     /**
150      * Creates a new builder with this object as the basis.
151      *
152      * @return a {@code Builder}
153      */
154     @Nonnull
155     public Builder with() {
156         return newBuilder(this);
157     }
158     /**
159      * Creates a new {@code Parent} instance using the specified groupId.
160      *
161      * @param groupId the new {@code String} to use
162      * @return a {@code Parent} with the specified groupId
163      */
164     @Nonnull
165     public Parent withGroupId(String groupId) {
166         return newBuilder(this, true).groupId(groupId).build();
167     }
168     /**
169      * Creates a new {@code Parent} instance using the specified artifactId.
170      *
171      * @param artifactId the new {@code String} to use
172      * @return a {@code Parent} with the specified artifactId
173      */
174     @Nonnull
175     public Parent withArtifactId(String artifactId) {
176         return newBuilder(this, true).artifactId(artifactId).build();
177     }
178     /**
179      * Creates a new {@code Parent} instance using the specified version.
180      *
181      * @param version the new {@code String} to use
182      * @return a {@code Parent} with the specified version
183      */
184     @Nonnull
185     public Parent withVersion(String version) {
186         return newBuilder(this, true).version(version).build();
187     }
188     /**
189      * Creates a new {@code Parent} instance using the specified relativePath.
190      *
191      * @param relativePath the new {@code String} to use
192      * @return a {@code Parent} with the specified relativePath
193      */
194     @Nonnull
195     public Parent withRelativePath(String relativePath) {
196         return newBuilder(this, true).relativePath(relativePath).build();
197     }
198 
199     /**
200      * Creates a new {@code Parent} instance.
201      * Equivalent to {@code newInstance(true)}.
202      * @see #newInstance(boolean)
203      *
204      * @return a new {@code Parent}
205      */
206     @Nonnull
207     public static Parent newInstance() {
208         return newInstance(true);
209     }
210 
211     /**
212      * Creates a new {@code Parent} instance using default values or not.
213      * Equivalent to {@code newBuilder(withDefaults).build()}.
214      *
215      * @param withDefaults the boolean indicating whether default values should be used
216      * @return a new {@code Parent}
217      */
218     @Nonnull
219     public static Parent newInstance(boolean withDefaults) {
220         return newBuilder(withDefaults).build();
221     }
222 
223     /**
224      * Creates a new {@code Parent} builder instance.
225      * Equivalent to {@code newBuilder(true)}.
226      * @see #newBuilder(boolean)
227      *
228      * @return a new {@code Builder}
229      */
230     @Nonnull
231     public static Builder newBuilder() {
232         return newBuilder(true);
233     }
234 
235     /**
236      * Creates a new {@code Parent} builder instance using default values or not.
237      *
238      * @param withDefaults the boolean indicating whether default values should be used
239      * @return a new {@code Builder}
240      */
241     @Nonnull
242     public static Builder newBuilder(boolean withDefaults) {
243         return new Builder(withDefaults);
244     }
245 
246     /**
247      * Creates a new {@code Parent} builder instance using the specified object as a basis.
248      * Equivalent to {@code newBuilder(from, false)}.
249      *
250      * @param from the {@code Parent} instance to use as a basis
251      * @return a new {@code Builder}
252      */
253     @Nonnull
254     public static Builder newBuilder(Parent from) {
255         return newBuilder(from, false);
256     }
257 
258     /**
259      * Creates a new {@code Parent} builder instance using the specified object as a basis.
260      *
261      * @param from the {@code Parent} instance to use as a basis
262      * @param forceCopy the boolean indicating if a copy should be forced
263      * @return a new {@code Builder}
264      */
265     @Nonnull
266     public static Builder newBuilder(Parent from, boolean forceCopy) {
267         return new Builder(from, forceCopy);
268     }
269 
270     /**
271      * Builder class used to create Parent instances.
272      * @see #with()
273      * @see #newBuilder()
274      */
275     @NotThreadSafe
276     public static class Builder
277     {
278         Parent base;
279         String groupId;
280         String artifactId;
281         String version;
282         String relativePath;
283         Map<Object, InputLocation> locations;
284         InputLocation importedFrom;
285 
286         protected Builder(boolean withDefaults) {
287             if (withDefaults) {
288                 this.relativePath = "..";
289             }
290         }
291 
292         protected Builder(Parent base, boolean forceCopy) {
293             if (forceCopy) {
294                 this.groupId = base.groupId;
295                 this.artifactId = base.artifactId;
296                 this.version = base.version;
297                 this.relativePath = base.relativePath;
298                 this.locations = base.locations;
299                 this.importedFrom = base.importedFrom;
300             } else {
301                 this.base = base;
302             }
303         }
304 
305         @Nonnull
306         public Builder groupId(String groupId) {
307             this.groupId = groupId;
308             return this;
309         }
310 
311         @Nonnull
312         public Builder artifactId(String artifactId) {
313             this.artifactId = artifactId;
314             return this;
315         }
316 
317         @Nonnull
318         public Builder version(String version) {
319             this.version = version;
320             return this;
321         }
322 
323         @Nonnull
324         public Builder relativePath(String relativePath) {
325             this.relativePath = relativePath;
326             return this;
327         }
328 
329 
330         @Nonnull
331         public Builder location(Object key, InputLocation location) {
332             if (location != null) {
333                 if (!(this.locations instanceof HashMap)) {
334                     this.locations = this.locations != null ? new HashMap<>(this.locations) : new HashMap<>();
335                 }
336                 this.locations.put(key, location);
337             }
338             return this;
339         }
340 
341         @Nonnull
342         public Builder importedFrom(InputLocation importedFrom) {
343             this.importedFrom = importedFrom;
344             return this;
345         }
346 
347         @Nonnull
348         public Parent build() {
349             // this method should not contain any logic other than creating (or reusing) an object in order to ease subclassing
350             if (base != null
351                     && (groupId == null || groupId == base.groupId)
352                     && (artifactId == null || artifactId == base.artifactId)
353                     && (version == null || version == base.version)
354                     && (relativePath == null || relativePath == base.relativePath)
355             ) {
356                 return base;
357             }
358             return new Parent(this);
359         }
360     }
361 
362 
363             
364     /**
365      * @return the id as {@code groupId:artifactId:version}
366      */
367     public String getId() {
368         return getGroupId() + ":" + getArtifactId() + ":pom:" + getVersion();
369     }
370 
371     @Override
372     public String toString() {
373         return getId();
374     }
375             
376           
377 }