View Javadoc
1   // =================== DO NOT EDIT THIS FILE ====================
2   //   Generated by Maven, any modifications will be overwritten.
3   // ==============================================================
4   package org.apache.maven.api.model;
5   
6   import java.io.Serializable;
7   import java.util.Collections;
8   import java.util.HashMap;
9   import java.util.Map;
10  import org.apache.maven.api.annotations.Experimental;
11  import org.apache.maven.api.annotations.Generated;
12  import org.apache.maven.api.annotations.Immutable;
13  import org.apache.maven.api.annotations.Nonnull;
14  import org.apache.maven.api.annotations.NotThreadSafe;
15  import org.apache.maven.api.annotations.ThreadSafe;
16  
17  /**
18   * The {@code <parent>} element contains information required to locate the parent project from which
19   * this project will inherit from.
20   * <strong>Note:</strong> The children of this element are not interpolated and must be given as literal values.
21   */
22  @Experimental
23  @Generated @ThreadSafe @Immutable
24  public class Parent
25      implements Serializable, InputLocationTracker
26  {
27      /**
28       * The group id of the parent project to inherit from.
29       */
30      final String groupId;
31      /**
32       * The artifact id of the parent project to inherit from.
33       */
34      final String artifactId;
35      /**
36       * The version of the parent project to inherit.
37       */
38      final String version;
39      /**
40       * The relative path of the parent {@code pom.xml} file within the check out.
41       * If not specified, it defaults to {@code ../pom.xml}.
42       * Maven looks for the parent POM first in this location on
43       * the filesystem, then the local repository, and lastly in the remote repo.
44       * {@code relativePath} allows you to select a different location,
45       * for example when your structure is flat, or deeper without an intermediate parent POM.
46       * However, the group ID, artifact ID and version are still required,
47       * and must match the file in the location given or it will revert to the repository for the POM.
48       * This feature is only for enhancing the development in a local checkout of that project.
49       * Set the value to an empty string in case you want to disable the feature and always resolve
50       * the parent POM from the repositories.
51       */
52      final String relativePath;
53      /** Location of the xml element for this object. */
54      final InputLocation location;
55      /** Location of the xml element for the field groupId. */
56      final InputLocation groupIdLocation;
57      /** Location of the xml element for the field artifactId. */
58      final InputLocation artifactIdLocation;
59      /** Location of the xml element for the field version. */
60      final InputLocation versionLocation;
61      /** Location of the xml element for the field relativePath. */
62      final InputLocation relativePathLocation;
63      /** Other locations */
64      final Map<Object, InputLocation> locations;
65  
66      /**
67        * Constructor for this class, package protected.
68        * @see Builder#build()
69        */
70      Parent(
71          String groupId,
72          String artifactId,
73          String version,
74          String relativePath,
75          Map<Object, InputLocation> locations,
76          InputLocation location,
77          InputLocation groupIdLocation,
78          InputLocation artifactIdLocation,
79          InputLocation versionLocation,
80          InputLocation relativePathLocation
81      )
82      {
83          this.groupId = groupId;
84          this.artifactId = artifactId;
85          this.version = version;
86          this.relativePath = relativePath;
87          this.locations = ImmutableCollections.copy( locations );
88          this.location = location;
89          this.groupIdLocation = groupIdLocation;
90          this.artifactIdLocation = artifactIdLocation;
91          this.versionLocation = versionLocation;
92          this.relativePathLocation = relativePathLocation;
93      }
94  
95      /**
96       * The group id of the parent project to inherit from.
97       *
98       * @return a {@code String}
99       */
100     public String getGroupId()
101     {
102         return this.groupId;
103     }
104 
105     /**
106      * The artifact id of the parent project to inherit from.
107      *
108      * @return a {@code String}
109      */
110     public String getArtifactId()
111     {
112         return this.artifactId;
113     }
114 
115     /**
116      * The version of the parent project to inherit.
117      *
118      * @return a {@code String}
119      */
120     public String getVersion()
121     {
122         return this.version;
123     }
124 
125     /**
126      * The relative path of the parent {@code pom.xml} file within the check out.
127      * If not specified, it defaults to {@code ../pom.xml}.
128      * Maven looks for the parent POM first in this location on
129      * the filesystem, then the local repository, and lastly in the remote repo.
130      * {@code relativePath} allows you to select a different location,
131      * for example when your structure is flat, or deeper without an intermediate parent POM.
132      * However, the group ID, artifact ID and version are still required,
133      * and must match the file in the location given or it will revert to the repository for the POM.
134      * This feature is only for enhancing the development in a local checkout of that project.
135      * Set the value to an empty string in case you want to disable the feature and always resolve
136      * the parent POM from the repositories.
137      *
138      * @return a {@code String}
139      */
140     public String getRelativePath()
141     {
142         return this.relativePath;
143     }
144 
145     /**
146      * Gets the location of the specified field in the input source.
147      */
148     public InputLocation getLocation( Object key )
149     {
150         if ( key instanceof String )
151         {
152             switch ( ( String ) key )
153             {
154                 case "":
155                     return location;
156                 case "groupId":
157                     return groupIdLocation;
158                 case "artifactId":
159                     return artifactIdLocation;
160                 case "version":
161                     return versionLocation;
162                 case "relativePath":
163                     return relativePathLocation;
164             }
165         }
166         return locations != null ? locations.get( key ) : null;
167     }
168 
169     /**
170      * Creates a new builder with this object as the basis.
171      *
172      * @return a {@code Builder}
173      */
174     @Nonnull
175     public Builder with()
176     {
177         return newBuilder( this );
178     }
179     /**
180      * Creates a new {@code Parent} instance using the specified groupId.
181      *
182      * @param groupId the new {@code String} to use
183      * @return a {@code Parent} with the specified groupId
184      */
185     @Nonnull
186     public Parent withGroupId( String groupId )
187     {
188         return with().groupId( groupId ).build();
189     }
190     /**
191      * Creates a new {@code Parent} instance using the specified artifactId.
192      *
193      * @param artifactId the new {@code String} to use
194      * @return a {@code Parent} with the specified artifactId
195      */
196     @Nonnull
197     public Parent withArtifactId( String artifactId )
198     {
199         return with().artifactId( artifactId ).build();
200     }
201     /**
202      * Creates a new {@code Parent} instance using the specified version.
203      *
204      * @param version the new {@code String} to use
205      * @return a {@code Parent} with the specified version
206      */
207     @Nonnull
208     public Parent withVersion( String version )
209     {
210         return with().version( version ).build();
211     }
212     /**
213      * Creates a new {@code Parent} instance using the specified relativePath.
214      *
215      * @param relativePath the new {@code String} to use
216      * @return a {@code Parent} with the specified relativePath
217      */
218     @Nonnull
219     public Parent withRelativePath( String relativePath )
220     {
221         return with().relativePath( relativePath ).build();
222     }
223 
224     /**
225      * Creates a new {@code Parent} instance.
226      * Equivalent to {@code newInstance( true )}.
227      * @see #newInstance(boolean)
228      *
229      * @return a new {@code Parent}
230      */
231     @Nonnull
232     public static Parent newInstance()
233     {
234         return newInstance( true );
235     }
236 
237     /**
238      * Creates a new {@code Parent} instance using default values or not.
239      * Equivalent to {@code newBuilder( withDefaults ).build()}.
240      *
241      * @param withDefaults the boolean indicating whether default values should be used
242      * @return a new {@code Parent}
243      */
244     @Nonnull
245     public static Parent newInstance( boolean withDefaults )
246     {
247         return newBuilder( withDefaults ).build();
248     }
249 
250     /**
251      * Creates a new {@code Parent} builder instance.
252      * Equivalent to {@code newBuilder( true )}.
253      * @see #newBuilder(boolean)
254      *
255      * @return a new {@code Builder}
256      */
257     @Nonnull
258     public static Builder newBuilder()
259     {
260         return newBuilder( true );
261     }
262 
263     /**
264      * Creates a new {@code Parent} builder instance using default values or not.
265      *
266      * @param withDefaults the boolean indicating whether default values should be used
267      * @return a new {@code Builder}
268      */
269     @Nonnull
270     public static Builder newBuilder( boolean withDefaults )
271     {
272         return new Builder( withDefaults );
273     }
274 
275     /**
276      * Creates a new {@code Parent} builder instance using the specified object as a basis.
277      * Equivalent to {@code newBuilder( from, false )}.
278      *
279      * @param from the {@code Parent} instance to use as a basis
280      * @return a new {@code Builder}
281      */
282     @Nonnull
283     public static Builder newBuilder( Parent from )
284     {
285         return newBuilder( from, false );
286     }
287 
288     /**
289      * Creates a new {@code Parent} builder instance using the specified object as a basis.
290      *
291      * @param from the {@code Parent} instance to use as a basis
292      * @param forceCopy the boolean indicating if a copy should be forced
293      * @return a new {@code Builder}
294      */
295     @Nonnull
296     public static Builder newBuilder( Parent from, boolean forceCopy )
297     {
298         return new Builder( from, forceCopy );
299     }
300 
301     /**
302      * Builder class used to create Parent instances.
303      * @see #with()
304      * @see #newBuilder()
305      */
306     @NotThreadSafe
307     public static class Builder
308     {
309         Parent base;
310         String groupId;
311         String artifactId;
312         String version;
313         String relativePath;
314         Map<Object, InputLocation> locations;
315 
316         Builder( boolean withDefaults )
317         {
318             if ( withDefaults )
319             {
320                 this.relativePath = "../pom.xml";
321             }
322         }
323 
324         Builder( Parent base, boolean forceCopy )
325         {
326             if ( forceCopy )
327             {
328                 this.groupId = base.groupId;
329                 this.artifactId = base.artifactId;
330                 this.version = base.version;
331                 this.relativePath = base.relativePath;
332             }
333             else
334             {
335                 this.base = base;
336             }
337         }
338 
339         @Nonnull
340         public Builder groupId( String groupId )
341         {
342             this.groupId = groupId;
343             return this;
344         }
345 
346         @Nonnull
347         public Builder artifactId( String artifactId )
348         {
349             this.artifactId = artifactId;
350             return this;
351         }
352 
353         @Nonnull
354         public Builder version( String version )
355         {
356             this.version = version;
357             return this;
358         }
359 
360         @Nonnull
361         public Builder relativePath( String relativePath )
362         {
363             this.relativePath = relativePath;
364             return this;
365         }
366 
367 
368         @Nonnull
369         public Builder location( Object key, InputLocation location )
370         {
371             if ( location != null )
372             {
373                 if ( this.locations == null )
374                 {
375                     this.locations = new HashMap<>();
376                 }
377                 this.locations.put( key, location );
378             }
379             return this;
380         }
381 
382         @Nonnull
383         public Parent build()
384         {
385             if ( base != null
386                     && ( groupId == null || groupId == base.groupId )
387                     && ( artifactId == null || artifactId == base.artifactId )
388                     && ( version == null || version == base.version )
389                     && ( relativePath == null || relativePath == base.relativePath )
390             )
391             {
392                 return base;
393             }
394             Map<Object, InputLocation> locations = null;
395             InputLocation location = null;
396             InputLocation groupIdLocation = null;
397             InputLocation artifactIdLocation = null;
398             InputLocation versionLocation = null;
399             InputLocation relativePathLocation = null;
400             if ( this.locations != null )
401             {
402                 locations = this.locations;
403                 location = locations.remove( "" );
404                 groupIdLocation = locations.remove( "groupId" );
405                 artifactIdLocation = locations.remove( "artifactId" );
406                 versionLocation = locations.remove( "version" );
407                 relativePathLocation = locations.remove( "relativePath" );
408             }
409             return new Parent(
410                 groupId != null ? groupId : ( base != null ? base.groupId : null ),
411                 artifactId != null ? artifactId : ( base != null ? base.artifactId : null ),
412                 version != null ? version : ( base != null ? base.version : null ),
413                 relativePath != null ? relativePath : ( base != null ? base.relativePath : null ),
414                 locations != null ? locations : ( base != null ? base.locations : null ),
415                 location != null ? location : ( base != null ? base.location : null ),
416                 groupIdLocation != null ? groupIdLocation : ( base != null ? base.groupIdLocation : null ),
417                 artifactIdLocation != null ? artifactIdLocation : ( base != null ? base.artifactIdLocation : null ),
418                 versionLocation != null ? versionLocation : ( base != null ? base.versionLocation : null ),
419                 relativePathLocation != null ? relativePathLocation : ( base != null ? base.relativePathLocation : null )
420             );
421         }
422     }
423 
424 
425             
426     /**
427      * @return the id as {@code groupId:artifactId:version}
428      */
429     public String getId()
430     {
431         StringBuilder id = new StringBuilder( 64 );
432 
433         id.append( getGroupId() );
434         id.append( ":" );
435         id.append( getArtifactId() );
436         id.append( ":" );
437         id.append( "pom" );
438         id.append( ":" );
439         id.append( getVersion() );
440 
441         return id.toString();
442     }
443 
444     @Override
445     public String toString()
446     {
447         return getId();
448     }
449             
450           
451 }