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}. Since 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 /** Locations */
97 final Map<Object, InputLocation> locations;
98
99 /**
100 * Constructor for this class, package protected.
101 * @see Builder#build()
102 */
103 Dependency(
104 String groupId,
105 String artifactId,
106 String version,
107 String type,
108 String classifier,
109 String scope,
110 String systemPath,
111 Collection<Exclusion> exclusions,
112 String optional,
113 Map<Object, InputLocation> locations
114 ) {
115 this.groupId = groupId;
116 this.artifactId = artifactId;
117 this.version = version;
118 this.type = type;
119 this.classifier = classifier;
120 this.scope = scope;
121 this.systemPath = systemPath;
122 this.exclusions = ImmutableCollections.copy(exclusions);
123 this.optional = optional;
124 this.locations = ImmutableCollections.copy(locations);
125 }
126
127 /**
128 * The project group that produced the dependency, e.g.
129 * {@code org.apache.maven}.
130 *
131 * @return a {@code String}
132 */
133 public String getGroupId() {
134 return this.groupId;
135 }
136
137 /**
138 * The unique id for an artifact produced by the project group, e.g.
139 * {@code maven-artifact}.
140 *
141 * @return a {@code String}
142 */
143 public String getArtifactId() {
144 return this.artifactId;
145 }
146
147 /**
148 * The version of the dependency, e.g. {@code 3.2.1}. Since Maven 2, this can also be
149 * specified as a range of versions.
150 *
151 * @return a {@code String}
152 */
153 public String getVersion() {
154 return this.version;
155 }
156
157 /**
158 * The type of dependency, that will be mapped to a file extension, an optional classifier, and a few other attributes.
159 * Some examples are {@code jar}, {@code war}, {@code ejb-client}
160 * and {@code test-jar}: see <a href="../maven-core/artifact-handlers.html">default
161 * artifact handlers</a> for a list. New types can be defined by extensions, so this is not a complete list.
162 *
163 * @return a {@code String}
164 */
165 public String getType() {
166 return this.type;
167 }
168
169 /**
170 * The classifier of the dependency. It is appended to
171 * the filename after the version. This allows:
172 * <ul>
173 * <li>referring to attached artifact, for example {@code sources} and {@code javadoc}:
174 * see <a href="../maven-core/artifact-handlers.html">default artifact handlers</a> for a list,</li>
175 * <li>distinguishing two artifacts
176 * that belong to the same POM but were built differently.
177 * For example, {@code jdk14} and {@code jdk15}.</li>
178 * </ul>
179 *
180 * @return a {@code String}
181 */
182 public String getClassifier() {
183 return this.classifier;
184 }
185
186 /**
187 * The scope of the dependency - {@code compile}, {@code runtime},
188 * {@code test}, {@code system}, and {@code provided}. Used to
189 * calculate the various classpaths used for compilation, testing, and so on.
190 * It also assists in determining which artifacts to include in a distribution of
191 * this project. For more information, see
192 * <a href="https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html">the
193 * dependency mechanism</a>. The default scope is {@code compile}.
194 *
195 * @return a {@code String}
196 */
197 public String getScope() {
198 return this.scope;
199 }
200
201 /**
202 * FOR SYSTEM SCOPE ONLY. Note that use of this property is <b>discouraged</b>
203 * and may be replaced in later versions. This specifies the path on the filesystem
204 * for this dependency.
205 * Requires an absolute path for the value, not relative.
206 * Use a property that gives the machine specific absolute path,
207 * e.g. {@code ${java.home}}.
208 *
209 * @return a {@code String}
210 */
211 public String getSystemPath() {
212 return this.systemPath;
213 }
214
215 /**
216 * Lists a set of artifacts that should be excluded from this dependency's
217 * artifact list when it comes to calculating transitive dependencies.
218 *
219 * @return a {@code List<Exclusion>}
220 */
221 @Nonnull
222 public List<Exclusion> getExclusions() {
223 return this.exclusions;
224 }
225
226 /**
227 * Indicates the dependency is optional for use of this library. While the
228 * version of the dependency will be taken into account for dependency calculation if the
229 * library is used elsewhere, it will not be passed on transitively. Note: While the type
230 * of this field is {@code String} for technical reasons, the semantic type is actually
231 * {@code Boolean}. Default value is {@code false}.
232 *
233 * @return a {@code String}
234 */
235 public String getOptional() {
236 return this.optional;
237 }
238
239 /**
240 * Gets the location of the specified field in the input source.
241 */
242 public InputLocation getLocation(Object key) {
243 return locations != null ? locations.get(key) : null;
244 }
245
246 /**
247 * Creates a new builder with this object as the basis.
248 *
249 * @return a {@code Builder}
250 */
251 @Nonnull
252 public Builder with() {
253 return newBuilder(this);
254 }
255 /**
256 * Creates a new {@code Dependency} instance using the specified groupId.
257 *
258 * @param groupId the new {@code String} to use
259 * @return a {@code Dependency} with the specified groupId
260 */
261 @Nonnull
262 public Dependency withGroupId(String groupId) {
263 return newBuilder(this, true).groupId(groupId).build();
264 }
265 /**
266 * Creates a new {@code Dependency} instance using the specified artifactId.
267 *
268 * @param artifactId the new {@code String} to use
269 * @return a {@code Dependency} with the specified artifactId
270 */
271 @Nonnull
272 public Dependency withArtifactId(String artifactId) {
273 return newBuilder(this, true).artifactId(artifactId).build();
274 }
275 /**
276 * Creates a new {@code Dependency} instance using the specified version.
277 *
278 * @param version the new {@code String} to use
279 * @return a {@code Dependency} with the specified version
280 */
281 @Nonnull
282 public Dependency withVersion(String version) {
283 return newBuilder(this, true).version(version).build();
284 }
285 /**
286 * Creates a new {@code Dependency} instance using the specified type.
287 *
288 * @param type the new {@code String} to use
289 * @return a {@code Dependency} with the specified type
290 */
291 @Nonnull
292 public Dependency withType(String type) {
293 return newBuilder(this, true).type(type).build();
294 }
295 /**
296 * Creates a new {@code Dependency} instance using the specified classifier.
297 *
298 * @param classifier the new {@code String} to use
299 * @return a {@code Dependency} with the specified classifier
300 */
301 @Nonnull
302 public Dependency withClassifier(String classifier) {
303 return newBuilder(this, true).classifier(classifier).build();
304 }
305 /**
306 * Creates a new {@code Dependency} instance using the specified scope.
307 *
308 * @param scope the new {@code String} to use
309 * @return a {@code Dependency} with the specified scope
310 */
311 @Nonnull
312 public Dependency withScope(String scope) {
313 return newBuilder(this, true).scope(scope).build();
314 }
315 /**
316 * Creates a new {@code Dependency} instance using the specified systemPath.
317 *
318 * @param systemPath the new {@code String} to use
319 * @return a {@code Dependency} with the specified systemPath
320 */
321 @Nonnull
322 public Dependency withSystemPath(String systemPath) {
323 return newBuilder(this, true).systemPath(systemPath).build();
324 }
325 /**
326 * Creates a new {@code Dependency} instance using the specified exclusions.
327 *
328 * @param exclusions the new {@code Collection<Exclusion>} to use
329 * @return a {@code Dependency} with the specified exclusions
330 */
331 @Nonnull
332 public Dependency withExclusions(Collection<Exclusion> exclusions) {
333 return newBuilder(this, true).exclusions(exclusions).build();
334 }
335 /**
336 * Creates a new {@code Dependency} instance using the specified optional.
337 *
338 * @param optional the new {@code String} to use
339 * @return a {@code Dependency} with the specified optional
340 */
341 @Nonnull
342 public Dependency withOptional(String optional) {
343 return newBuilder(this, true).optional(optional).build();
344 }
345
346 /**
347 * Creates a new {@code Dependency} instance.
348 * Equivalent to {@code newInstance(true)}.
349 * @see #newInstance(boolean)
350 *
351 * @return a new {@code Dependency}
352 */
353 @Nonnull
354 public static Dependency newInstance() {
355 return newInstance(true);
356 }
357
358 /**
359 * Creates a new {@code Dependency} instance using default values or not.
360 * Equivalent to {@code newBuilder(withDefaults).build()}.
361 *
362 * @param withDefaults the boolean indicating whether default values should be used
363 * @return a new {@code Dependency}
364 */
365 @Nonnull
366 public static Dependency newInstance(boolean withDefaults) {
367 return newBuilder(withDefaults).build();
368 }
369
370 /**
371 * Creates a new {@code Dependency} builder instance.
372 * Equivalent to {@code newBuilder(true)}.
373 * @see #newBuilder(boolean)
374 *
375 * @return a new {@code Builder}
376 */
377 @Nonnull
378 public static Builder newBuilder() {
379 return newBuilder(true);
380 }
381
382 /**
383 * Creates a new {@code Dependency} builder instance using default values or not.
384 *
385 * @param withDefaults the boolean indicating whether default values should be used
386 * @return a new {@code Builder}
387 */
388 @Nonnull
389 public static Builder newBuilder(boolean withDefaults) {
390 return new Builder(withDefaults);
391 }
392
393 /**
394 * Creates a new {@code Dependency} builder instance using the specified object as a basis.
395 * Equivalent to {@code newBuilder(from, false)}.
396 *
397 * @param from the {@code Dependency} instance to use as a basis
398 * @return a new {@code Builder}
399 */
400 @Nonnull
401 public static Builder newBuilder(Dependency from) {
402 return newBuilder(from, false);
403 }
404
405 /**
406 * Creates a new {@code Dependency} builder instance using the specified object as a basis.
407 *
408 * @param from the {@code Dependency} instance to use as a basis
409 * @param forceCopy the boolean indicating if a copy should be forced
410 * @return a new {@code Builder}
411 */
412 @Nonnull
413 public static Builder newBuilder(Dependency from, boolean forceCopy) {
414 return new Builder(from, forceCopy);
415 }
416
417 /**
418 * Builder class used to create Dependency instances.
419 * @see #with()
420 * @see #newBuilder()
421 */
422 @NotThreadSafe
423 public static class Builder
424 {
425 Dependency base;
426 String groupId;
427 String artifactId;
428 String version;
429 String type;
430 String classifier;
431 String scope;
432 String systemPath;
433 Collection<Exclusion> exclusions;
434 String optional;
435 Map<Object, InputLocation> locations;
436
437 Builder(boolean withDefaults) {
438 if (withDefaults) {
439 this.type = "jar";
440 }
441 }
442
443 Builder(Dependency base, boolean forceCopy) {
444 if (forceCopy) {
445 this.groupId = base.groupId;
446 this.artifactId = base.artifactId;
447 this.version = base.version;
448 this.type = base.type;
449 this.classifier = base.classifier;
450 this.scope = base.scope;
451 this.systemPath = base.systemPath;
452 this.exclusions = base.exclusions;
453 this.optional = base.optional;
454 this.locations = base.locations;
455 } else {
456 this.base = base;
457 }
458 }
459
460 @Nonnull
461 public Builder groupId(String groupId) {
462 this.groupId = groupId;
463 return this;
464 }
465
466 @Nonnull
467 public Builder artifactId(String artifactId) {
468 this.artifactId = artifactId;
469 return this;
470 }
471
472 @Nonnull
473 public Builder version(String version) {
474 this.version = version;
475 return this;
476 }
477
478 @Nonnull
479 public Builder type(String type) {
480 this.type = type;
481 return this;
482 }
483
484 @Nonnull
485 public Builder classifier(String classifier) {
486 this.classifier = classifier;
487 return this;
488 }
489
490 @Nonnull
491 public Builder scope(String scope) {
492 this.scope = scope;
493 return this;
494 }
495
496 @Nonnull
497 public Builder systemPath(String systemPath) {
498 this.systemPath = systemPath;
499 return this;
500 }
501
502 @Nonnull
503 public Builder exclusions(Collection<Exclusion> exclusions) {
504 this.exclusions = exclusions;
505 return this;
506 }
507
508 @Nonnull
509 public Builder optional(String optional) {
510 this.optional = optional;
511 return this;
512 }
513
514
515 @Nonnull
516 public Builder location(Object key, InputLocation location) {
517 if (location != null) {
518 if (!(this.locations instanceof HashMap)) {
519 this.locations = this.locations != null ? new HashMap<>(this.locations) : new HashMap<>();
520 }
521 this.locations.put(key, location);
522 }
523 return this;
524 }
525
526 @Nonnull
527 public Dependency build() {
528 if (base != null
529 && (groupId == null || groupId == base.groupId)
530 && (artifactId == null || artifactId == base.artifactId)
531 && (version == null || version == base.version)
532 && (type == null || type == base.type)
533 && (classifier == null || classifier == base.classifier)
534 && (scope == null || scope == base.scope)
535 && (systemPath == null || systemPath == base.systemPath)
536 && (exclusions == null || exclusions == base.exclusions)
537 && (optional == null || optional == base.optional)
538 ) {
539 return base;
540 }
541 Map<Object, InputLocation> newlocs = this.locations != null ? this.locations : Collections.emptyMap();
542 Map<Object, InputLocation> oldlocs = this.base != null && this.base.locations != null ? this.base.locations : Collections.emptyMap();
543 Map<Object, InputLocation> locations = new HashMap<>();
544 locations.put("", newlocs.containsKey("") ? newlocs.get("") : oldlocs.get(""));
545 locations.put("groupId", newlocs.containsKey("groupId") ? newlocs.get("groupId") : oldlocs.get("groupId"));
546 locations.put("artifactId", newlocs.containsKey("artifactId") ? newlocs.get("artifactId") : oldlocs.get("artifactId"));
547 locations.put("version", newlocs.containsKey("version") ? newlocs.get("version") : oldlocs.get("version"));
548 locations.put("type", newlocs.containsKey("type") ? newlocs.get("type") : oldlocs.get("type"));
549 locations.put("classifier", newlocs.containsKey("classifier") ? newlocs.get("classifier") : oldlocs.get("classifier"));
550 locations.put("scope", newlocs.containsKey("scope") ? newlocs.get("scope") : oldlocs.get("scope"));
551 locations.put("systemPath", newlocs.containsKey("systemPath") ? newlocs.get("systemPath") : oldlocs.get("systemPath"));
552 locations.put("exclusions", newlocs.containsKey("exclusions") ? newlocs.get("exclusions") : oldlocs.get("exclusions"));
553 locations.put("optional", newlocs.containsKey("optional") ? newlocs.get("optional") : oldlocs.get("optional"));
554 return new Dependency(
555 groupId != null ? groupId : (base != null ? base.groupId : null),
556 artifactId != null ? artifactId : (base != null ? base.artifactId : null),
557 version != null ? version : (base != null ? base.version : null),
558 type != null ? type : (base != null ? base.type : null),
559 classifier != null ? classifier : (base != null ? base.classifier : null),
560 scope != null ? scope : (base != null ? base.scope : null),
561 systemPath != null ? systemPath : (base != null ? base.systemPath : null),
562 exclusions != null ? exclusions : (base != null ? base.exclusions : null),
563 optional != null ? optional : (base != null ? base.optional : null),
564 locations
565 );
566 }
567 }
568
569
570
571 public boolean isOptional() {
572 return (getOptional() != null) ? Boolean.parseBoolean(getOptional()) : false;
573 }
574
575
576
577
578
579 /**
580 * @see java.lang.Object#toString()
581 */
582 public String toString() {
583 return "Dependency {groupId=" + getGroupId() + ", artifactId=" + getArtifactId() + ", version=" + getVersion() + ", type=" + getType() + "}";
584 }
585
586
587
588
589 private volatile String managementKey;
590
591 /**
592 * @return the management key as {@code groupId:artifactId:type}
593 */
594 public String getManagementKey() {
595 if (managementKey == null) {
596 managementKey = getGroupId() + ":" + getArtifactId() + ":" + getType() + (getClassifier() != null ? ":" + getClassifier() : "");
597 }
598 return managementKey;
599 }
600
601
602 }