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