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 org.apache.maven.api.annotations.Experimental;
15  import org.apache.maven.api.annotations.Generated;
16  import org.apache.maven.api.annotations.Immutable;
17  import org.apache.maven.api.annotations.Nonnull;
18  import org.apache.maven.api.annotations.NotThreadSafe;
19  import org.apache.maven.api.annotations.ThreadSafe;
20  
21  /**
22   * The {@code <dependency>} element contains information about a dependency
23   * of the project.
24   */
25  @Experimental
26  @Generated @ThreadSafe @Immutable
27  public class Dependency
28      implements Serializable, InputLocationTracker
29  {
30      /**
31       * The project group that produced the dependency, e.g.
32       * {@code org.apache.maven}.
33       */
34      final String groupId;
35      /**
36       * The unique id for an artifact produced by the project group, e.g.
37       * {@code maven-artifact}.
38       */
39      final String artifactId;
40      /**
41       * The version of the dependency, e.g. {@code 3.2.1}. In Maven 2, this can also be
42       * specified as a range of versions.
43       */
44      final String version;
45      /**
46       * The type of dependency, that will be mapped to a file extension, an optional classifier, and a few other attributes.
47       * Some examples are {@code jar}, {@code war}, {@code ejb-client}
48       * and {@code test-jar}: see <a href="../maven-core/artifact-handlers.html">default
49       * artifact handlers</a> for a list. New types can be defined by extensions, so this is not a complete list.
50       */
51      final String type;
52      /**
53       * The classifier of the dependency. It is appended to
54       * the filename after the version. This allows:
55       * <ul>
56       * <li>referring to attached artifact, for example {@code sources} and {@code javadoc}:
57       * see <a href="../maven-core/artifact-handlers.html">default artifact handlers</a> for a list,</li>
58       * <li>distinguishing two artifacts
59       * that belong to the same POM but were built differently.
60       * For example, {@code jdk14} and {@code jdk15}.</li>
61       * </ul>
62       */
63      final String classifier;
64      /**
65       * The scope of the dependency - {@code compile}, {@code runtime},
66       * {@code test}, {@code system}, and {@code provided}. Used to
67       * calculate the various classpaths used for compilation, testing, and so on.
68       * It also assists in determining which artifacts to include in a distribution of
69       * this project. For more information, see
70       * <a href="https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html">the
71       * dependency mechanism</a>. The default scope is {@code compile}.
72       */
73      final String scope;
74      /**
75       * FOR SYSTEM SCOPE ONLY. Note that use of this property is <b>discouraged</b>
76       * and may be replaced in later versions. This specifies the path on the filesystem
77       * for this dependency.
78       * Requires an absolute path for the value, not relative.
79       * Use a property that gives the machine specific absolute path,
80       * e.g. {@code ${java.home}}.
81       */
82      final String systemPath;
83      /**
84       * Lists a set of artifacts that should be excluded from this dependency's
85       * artifact list when it comes to calculating transitive dependencies.
86       */
87      final List<Exclusion> exclusions;
88      /**
89       * Indicates the dependency is optional for use of this library. While the
90       * version of the dependency will be taken into account for dependency calculation if the
91       * library is used elsewhere, it will not be passed on transitively. Note: While the type
92       * of this field is {@code String} for technical reasons, the semantic type is actually
93       * {@code Boolean}. Default value is {@code false}.
94       */
95      final String optional;
96      /** Location of the xml element for this object. */
97      final InputLocation location;
98      /** Location of the xml element for the field groupId. */
99      final InputLocation groupIdLocation;
100     /** Location of the xml element for the field artifactId. */
101     final InputLocation artifactIdLocation;
102     /** Location of the xml element for the field version. */
103     final InputLocation versionLocation;
104     /** Location of the xml element for the field type. */
105     final InputLocation typeLocation;
106     /** Location of the xml element for the field classifier. */
107     final InputLocation classifierLocation;
108     /** Location of the xml element for the field scope. */
109     final InputLocation scopeLocation;
110     /** Location of the xml element for the field systemPath. */
111     final InputLocation systemPathLocation;
112     /** Location of the xml element for the field exclusions. */
113     final InputLocation exclusionsLocation;
114     /** Location of the xml element for the field optional. */
115     final InputLocation optionalLocation;
116     /** Other locations */
117     final Map<Object, InputLocation> locations;
118 
119     /**
120       * Constructor for this class, package protected.
121       * @see Builder#build()
122       */
123     Dependency(
124         String groupId,
125         String artifactId,
126         String version,
127         String type,
128         String classifier,
129         String scope,
130         String systemPath,
131         Collection<Exclusion> exclusions,
132         String optional,
133         Map<Object, InputLocation> locations,
134         InputLocation location,
135         InputLocation groupIdLocation,
136         InputLocation artifactIdLocation,
137         InputLocation versionLocation,
138         InputLocation typeLocation,
139         InputLocation classifierLocation,
140         InputLocation scopeLocation,
141         InputLocation systemPathLocation,
142         InputLocation exclusionsLocation,
143         InputLocation optionalLocation
144     )
145     {
146         this.groupId = groupId;
147         this.artifactId = artifactId;
148         this.version = version;
149         this.type = type;
150         this.classifier = classifier;
151         this.scope = scope;
152         this.systemPath = systemPath;
153         this.exclusions = ImmutableCollections.copy( exclusions );
154         this.optional = optional;
155         this.locations = ImmutableCollections.copy( locations );
156         this.location = location;
157         this.groupIdLocation = groupIdLocation;
158         this.artifactIdLocation = artifactIdLocation;
159         this.versionLocation = versionLocation;
160         this.typeLocation = typeLocation;
161         this.classifierLocation = classifierLocation;
162         this.scopeLocation = scopeLocation;
163         this.systemPathLocation = systemPathLocation;
164         this.exclusionsLocation = exclusionsLocation;
165         this.optionalLocation = optionalLocation;
166     }
167 
168     /**
169      * The project group that produced the dependency, e.g.
170      * {@code org.apache.maven}.
171      *
172      * @return a {@code String}
173      */
174     public String getGroupId()
175     {
176         return this.groupId;
177     }
178 
179     /**
180      * The unique id for an artifact produced by the project group, e.g.
181      * {@code maven-artifact}.
182      *
183      * @return a {@code String}
184      */
185     public String getArtifactId()
186     {
187         return this.artifactId;
188     }
189 
190     /**
191      * The version of the dependency, e.g. {@code 3.2.1}. In Maven 2, this can also be
192      * specified as a range of versions.
193      *
194      * @return a {@code String}
195      */
196     public String getVersion()
197     {
198         return this.version;
199     }
200 
201     /**
202      * The type of dependency, that will be mapped to a file extension, an optional classifier, and a few other attributes.
203      * Some examples are {@code jar}, {@code war}, {@code ejb-client}
204      * and {@code test-jar}: see <a href="../maven-core/artifact-handlers.html">default
205      * artifact handlers</a> for a list. New types can be defined by extensions, so this is not a complete list.
206      *
207      * @return a {@code String}
208      */
209     public String getType()
210     {
211         return this.type;
212     }
213 
214     /**
215      * The classifier of the dependency. It is appended to
216      * the filename after the version. This allows:
217      * <ul>
218      * <li>referring to attached artifact, for example {@code sources} and {@code javadoc}:
219      * see <a href="../maven-core/artifact-handlers.html">default artifact handlers</a> for a list,</li>
220      * <li>distinguishing two artifacts
221      * that belong to the same POM but were built differently.
222      * For example, {@code jdk14} and {@code jdk15}.</li>
223      * </ul>
224      *
225      * @return a {@code String}
226      */
227     public String getClassifier()
228     {
229         return this.classifier;
230     }
231 
232     /**
233      * The scope of the dependency - {@code compile}, {@code runtime},
234      * {@code test}, {@code system}, and {@code provided}. Used to
235      * calculate the various classpaths used for compilation, testing, and so on.
236      * It also assists in determining which artifacts to include in a distribution of
237      * this project. For more information, see
238      * <a href="https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html">the
239      * dependency mechanism</a>. The default scope is {@code compile}.
240      *
241      * @return a {@code String}
242      */
243     public String getScope()
244     {
245         return this.scope;
246     }
247 
248     /**
249      * FOR SYSTEM SCOPE ONLY. Note that use of this property is <b>discouraged</b>
250      * and may be replaced in later versions. This specifies the path on the filesystem
251      * for this dependency.
252      * Requires an absolute path for the value, not relative.
253      * Use a property that gives the machine specific absolute path,
254      * e.g. {@code ${java.home}}.
255      *
256      * @return a {@code String}
257      */
258     public String getSystemPath()
259     {
260         return this.systemPath;
261     }
262 
263     /**
264      * Lists a set of artifacts that should be excluded from this dependency's
265      * artifact list when it comes to calculating transitive dependencies.
266      *
267      * @return a {@code List<Exclusion>}
268      */
269     @Nonnull
270     public List<Exclusion> getExclusions()
271     {
272         return this.exclusions;
273     }
274 
275     /**
276      * Indicates the dependency is optional for use of this library. While the
277      * version of the dependency will be taken into account for dependency calculation if the
278      * library is used elsewhere, it will not be passed on transitively. Note: While the type
279      * of this field is {@code String} for technical reasons, the semantic type is actually
280      * {@code Boolean}. Default value is {@code false}.
281      *
282      * @return a {@code String}
283      */
284     public String getOptional()
285     {
286         return this.optional;
287     }
288 
289     /**
290      * Gets the location of the specified field in the input source.
291      */
292     public InputLocation getLocation( Object key )
293     {
294         if ( key instanceof String )
295         {
296             switch ( ( String ) key )
297             {
298                 case "":
299                     return location;
300                 case "groupId":
301                     return groupIdLocation;
302                 case "artifactId":
303                     return artifactIdLocation;
304                 case "version":
305                     return versionLocation;
306                 case "type":
307                     return typeLocation;
308                 case "classifier":
309                     return classifierLocation;
310                 case "scope":
311                     return scopeLocation;
312                 case "systemPath":
313                     return systemPathLocation;
314                 case "exclusions":
315                     return exclusionsLocation;
316                 case "optional":
317                     return optionalLocation;
318             }
319         }
320         return locations != null ? locations.get( key ) : null;
321     }
322 
323     /**
324      * Creates a new builder with this object as the basis.
325      *
326      * @return a {@code Builder}
327      */
328     @Nonnull
329     public Builder with()
330     {
331         return newBuilder( this );
332     }
333     /**
334      * Creates a new {@code Dependency} instance using the specified groupId.
335      *
336      * @param groupId the new {@code String} to use
337      * @return a {@code Dependency} with the specified groupId
338      */
339     @Nonnull
340     public Dependency withGroupId( String groupId )
341     {
342         return with().groupId( groupId ).build();
343     }
344     /**
345      * Creates a new {@code Dependency} instance using the specified artifactId.
346      *
347      * @param artifactId the new {@code String} to use
348      * @return a {@code Dependency} with the specified artifactId
349      */
350     @Nonnull
351     public Dependency withArtifactId( String artifactId )
352     {
353         return with().artifactId( artifactId ).build();
354     }
355     /**
356      * Creates a new {@code Dependency} instance using the specified version.
357      *
358      * @param version the new {@code String} to use
359      * @return a {@code Dependency} with the specified version
360      */
361     @Nonnull
362     public Dependency withVersion( String version )
363     {
364         return with().version( version ).build();
365     }
366     /**
367      * Creates a new {@code Dependency} instance using the specified type.
368      *
369      * @param type the new {@code String} to use
370      * @return a {@code Dependency} with the specified type
371      */
372     @Nonnull
373     public Dependency withType( String type )
374     {
375         return with().type( type ).build();
376     }
377     /**
378      * Creates a new {@code Dependency} instance using the specified classifier.
379      *
380      * @param classifier the new {@code String} to use
381      * @return a {@code Dependency} with the specified classifier
382      */
383     @Nonnull
384     public Dependency withClassifier( String classifier )
385     {
386         return with().classifier( classifier ).build();
387     }
388     /**
389      * Creates a new {@code Dependency} instance using the specified scope.
390      *
391      * @param scope the new {@code String} to use
392      * @return a {@code Dependency} with the specified scope
393      */
394     @Nonnull
395     public Dependency withScope( String scope )
396     {
397         return with().scope( scope ).build();
398     }
399     /**
400      * Creates a new {@code Dependency} instance using the specified systemPath.
401      *
402      * @param systemPath the new {@code String} to use
403      * @return a {@code Dependency} with the specified systemPath
404      */
405     @Nonnull
406     public Dependency withSystemPath( String systemPath )
407     {
408         return with().systemPath( systemPath ).build();
409     }
410     /**
411      * Creates a new {@code Dependency} instance using the specified exclusions.
412      *
413      * @param exclusions the new {@code Collection<Exclusion>} to use
414      * @return a {@code Dependency} with the specified exclusions
415      */
416     @Nonnull
417     public Dependency withExclusions( Collection<Exclusion> exclusions )
418     {
419         return with().exclusions( exclusions ).build();
420     }
421     /**
422      * Creates a new {@code Dependency} instance using the specified optional.
423      *
424      * @param optional the new {@code String} to use
425      * @return a {@code Dependency} with the specified optional
426      */
427     @Nonnull
428     public Dependency withOptional( String optional )
429     {
430         return with().optional( optional ).build();
431     }
432 
433     /**
434      * Creates a new {@code Dependency} instance.
435      * Equivalent to {@code newInstance( true )}.
436      * @see #newInstance(boolean)
437      *
438      * @return a new {@code Dependency}
439      */
440     @Nonnull
441     public static Dependency newInstance()
442     {
443         return newInstance( true );
444     }
445 
446     /**
447      * Creates a new {@code Dependency} instance using default values or not.
448      * Equivalent to {@code newBuilder( withDefaults ).build()}.
449      *
450      * @param withDefaults the boolean indicating whether default values should be used
451      * @return a new {@code Dependency}
452      */
453     @Nonnull
454     public static Dependency newInstance( boolean withDefaults )
455     {
456         return newBuilder( withDefaults ).build();
457     }
458 
459     /**
460      * Creates a new {@code Dependency} builder instance.
461      * Equivalent to {@code newBuilder( true )}.
462      * @see #newBuilder(boolean)
463      *
464      * @return a new {@code Builder}
465      */
466     @Nonnull
467     public static Builder newBuilder()
468     {
469         return newBuilder( true );
470     }
471 
472     /**
473      * Creates a new {@code Dependency} builder instance using default values or not.
474      *
475      * @param withDefaults the boolean indicating whether default values should be used
476      * @return a new {@code Builder}
477      */
478     @Nonnull
479     public static Builder newBuilder( boolean withDefaults )
480     {
481         return new Builder( withDefaults );
482     }
483 
484     /**
485      * Creates a new {@code Dependency} builder instance using the specified object as a basis.
486      * Equivalent to {@code newBuilder( from, false )}.
487      *
488      * @param from the {@code Dependency} instance to use as a basis
489      * @return a new {@code Builder}
490      */
491     @Nonnull
492     public static Builder newBuilder( Dependency from )
493     {
494         return newBuilder( from, false );
495     }
496 
497     /**
498      * Creates a new {@code Dependency} builder instance using the specified object as a basis.
499      *
500      * @param from the {@code Dependency} instance to use as a basis
501      * @param forceCopy the boolean indicating if a copy should be forced
502      * @return a new {@code Builder}
503      */
504     @Nonnull
505     public static Builder newBuilder( Dependency from, boolean forceCopy )
506     {
507         return new Builder( from, forceCopy );
508     }
509 
510     /**
511      * Builder class used to create Dependency instances.
512      * @see #with()
513      * @see #newBuilder()
514      */
515     @NotThreadSafe
516     public static class Builder
517     {
518         Dependency base;
519         String groupId;
520         String artifactId;
521         String version;
522         String type;
523         String classifier;
524         String scope;
525         String systemPath;
526         Collection<Exclusion> exclusions;
527         String optional;
528         Map<Object, InputLocation> locations;
529 
530         Builder( boolean withDefaults )
531         {
532             if ( withDefaults )
533             {
534                 this.type = "jar";
535             }
536         }
537 
538         Builder( Dependency base, boolean forceCopy )
539         {
540             if ( forceCopy )
541             {
542                 this.groupId = base.groupId;
543                 this.artifactId = base.artifactId;
544                 this.version = base.version;
545                 this.type = base.type;
546                 this.classifier = base.classifier;
547                 this.scope = base.scope;
548                 this.systemPath = base.systemPath;
549                 this.exclusions = base.exclusions;
550                 this.optional = base.optional;
551             }
552             else
553             {
554                 this.base = base;
555             }
556         }
557 
558         @Nonnull
559         public Builder groupId( String groupId )
560         {
561             this.groupId = groupId;
562             return this;
563         }
564 
565         @Nonnull
566         public Builder artifactId( String artifactId )
567         {
568             this.artifactId = artifactId;
569             return this;
570         }
571 
572         @Nonnull
573         public Builder version( String version )
574         {
575             this.version = version;
576             return this;
577         }
578 
579         @Nonnull
580         public Builder type( String type )
581         {
582             this.type = type;
583             return this;
584         }
585 
586         @Nonnull
587         public Builder classifier( String classifier )
588         {
589             this.classifier = classifier;
590             return this;
591         }
592 
593         @Nonnull
594         public Builder scope( String scope )
595         {
596             this.scope = scope;
597             return this;
598         }
599 
600         @Nonnull
601         public Builder systemPath( String systemPath )
602         {
603             this.systemPath = systemPath;
604             return this;
605         }
606 
607         @Nonnull
608         public Builder exclusions( Collection<Exclusion> exclusions )
609         {
610             this.exclusions = exclusions;
611             return this;
612         }
613 
614         @Nonnull
615         public Builder optional( String optional )
616         {
617             this.optional = optional;
618             return this;
619         }
620 
621 
622         @Nonnull
623         public Builder location( Object key, InputLocation location )
624         {
625             if ( location != null )
626             {
627                 if ( this.locations == null )
628                 {
629                     this.locations = new HashMap<>();
630                 }
631                 this.locations.put( key, location );
632             }
633             return this;
634         }
635 
636         @Nonnull
637         public Dependency build()
638         {
639             if ( base != null
640                     && ( groupId == null || groupId == base.groupId )
641                     && ( artifactId == null || artifactId == base.artifactId )
642                     && ( version == null || version == base.version )
643                     && ( type == null || type == base.type )
644                     && ( classifier == null || classifier == base.classifier )
645                     && ( scope == null || scope == base.scope )
646                     && ( systemPath == null || systemPath == base.systemPath )
647                     && ( exclusions == null || exclusions == base.exclusions )
648                     && ( optional == null || optional == base.optional )
649             )
650             {
651                 return base;
652             }
653             Map<Object, InputLocation> locations = null;
654             InputLocation location = null;
655             InputLocation groupIdLocation = null;
656             InputLocation artifactIdLocation = null;
657             InputLocation versionLocation = null;
658             InputLocation typeLocation = null;
659             InputLocation classifierLocation = null;
660             InputLocation scopeLocation = null;
661             InputLocation systemPathLocation = null;
662             InputLocation exclusionsLocation = null;
663             InputLocation optionalLocation = null;
664             if ( this.locations != null )
665             {
666                 locations = this.locations;
667                 location = locations.remove( "" );
668                 groupIdLocation = locations.remove( "groupId" );
669                 artifactIdLocation = locations.remove( "artifactId" );
670                 versionLocation = locations.remove( "version" );
671                 typeLocation = locations.remove( "type" );
672                 classifierLocation = locations.remove( "classifier" );
673                 scopeLocation = locations.remove( "scope" );
674                 systemPathLocation = locations.remove( "systemPath" );
675                 exclusionsLocation = locations.remove( "exclusions" );
676                 optionalLocation = locations.remove( "optional" );
677             }
678             return new Dependency(
679                 groupId != null ? groupId : ( base != null ? base.groupId : null ),
680                 artifactId != null ? artifactId : ( base != null ? base.artifactId : null ),
681                 version != null ? version : ( base != null ? base.version : null ),
682                 type != null ? type : ( base != null ? base.type : null ),
683                 classifier != null ? classifier : ( base != null ? base.classifier : null ),
684                 scope != null ? scope : ( base != null ? base.scope : null ),
685                 systemPath != null ? systemPath : ( base != null ? base.systemPath : null ),
686                 exclusions != null ? exclusions : ( base != null ? base.exclusions : null ),
687                 optional != null ? optional : ( base != null ? base.optional : null ),
688                 locations != null ? locations : ( base != null ? base.locations : null ),
689                 location != null ? location : ( base != null ? base.location : null ),
690                 groupIdLocation != null ? groupIdLocation : ( base != null ? base.groupIdLocation : null ),
691                 artifactIdLocation != null ? artifactIdLocation : ( base != null ? base.artifactIdLocation : null ),
692                 versionLocation != null ? versionLocation : ( base != null ? base.versionLocation : null ),
693                 typeLocation != null ? typeLocation : ( base != null ? base.typeLocation : null ),
694                 classifierLocation != null ? classifierLocation : ( base != null ? base.classifierLocation : null ),
695                 scopeLocation != null ? scopeLocation : ( base != null ? base.scopeLocation : null ),
696                 systemPathLocation != null ? systemPathLocation : ( base != null ? base.systemPathLocation : null ),
697                 exclusionsLocation != null ? exclusionsLocation : ( base != null ? base.exclusionsLocation : null ),
698                 optionalLocation != null ? optionalLocation : ( base != null ? base.optionalLocation : null )
699             );
700         }
701     }
702 
703 
704             
705     public boolean isOptional()
706     {
707         return ( getOptional() != null ) ? Boolean.parseBoolean( getOptional() ) : false;
708     }
709 
710             
711           
712 
713             
714     /**
715      * @see java.lang.Object#toString()
716      */
717     public String toString()
718     {
719         return "Dependency {groupId=" + getGroupId() + ", artifactId=" + getArtifactId() + ", version=" + getVersion() + ", type=" + getType() + "}";
720     }
721             
722           
723 
724             
725     private volatile String managementKey;
726 
727     /**
728      * @return the management key as {@code groupId:artifactId:type}
729      */
730     public String getManagementKey()
731     {
732         if ( managementKey == null )
733         {
734             managementKey = getGroupId() + ":" + getArtifactId() + ":" + getType() + ( getClassifier() != null ? ":" + getClassifier() : "" );
735         }
736         return managementKey;
737     }
738             
739           
740 }