1
2
3
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
23
24
25 @Experimental
26 @Generated @ThreadSafe @Immutable
27 public class Dependency
28 implements Serializable, InputLocationTracker
29 {
30
31
32
33
34 final String groupId;
35
36
37
38
39 final String artifactId;
40
41
42
43
44 final String version;
45
46
47
48
49
50
51 final String type;
52
53
54
55
56
57
58
59
60
61
62
63 final String classifier;
64
65
66
67
68
69
70
71
72
73 final String scope;
74
75
76
77
78
79
80
81
82 final String systemPath;
83
84
85
86
87 final List<Exclusion> exclusions;
88
89
90
91
92
93
94
95 final String optional;
96
97 final Map<Object, InputLocation> locations;
98
99
100
101
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
129
130
131
132
133 public String getGroupId() {
134 return this.groupId;
135 }
136
137
138
139
140
141
142
143 public String getArtifactId() {
144 return this.artifactId;
145 }
146
147
148
149
150
151
152
153 public String getVersion() {
154 return this.version;
155 }
156
157
158
159
160
161
162
163
164
165 public String getType() {
166 return this.type;
167 }
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182 public String getClassifier() {
183 return this.classifier;
184 }
185
186
187
188
189
190
191
192
193
194
195
196
197 public String getScope() {
198 return this.scope;
199 }
200
201
202
203
204
205
206
207
208
209
210
211 public String getSystemPath() {
212 return this.systemPath;
213 }
214
215
216
217
218
219
220
221 @Nonnull
222 public List<Exclusion> getExclusions() {
223 return this.exclusions;
224 }
225
226
227
228
229
230
231
232
233
234
235 public String getOptional() {
236 return this.optional;
237 }
238
239
240
241
242 public InputLocation getLocation(Object key) {
243 return locations != null ? locations.get(key) : null;
244 }
245
246
247
248
249
250
251 @Nonnull
252 public Builder with() {
253 return newBuilder(this);
254 }
255
256
257
258
259
260
261 @Nonnull
262 public Dependency withGroupId(String groupId) {
263 return newBuilder(this, true).groupId(groupId).build();
264 }
265
266
267
268
269
270
271 @Nonnull
272 public Dependency withArtifactId(String artifactId) {
273 return newBuilder(this, true).artifactId(artifactId).build();
274 }
275
276
277
278
279
280
281 @Nonnull
282 public Dependency withVersion(String version) {
283 return newBuilder(this, true).version(version).build();
284 }
285
286
287
288
289
290
291 @Nonnull
292 public Dependency withType(String type) {
293 return newBuilder(this, true).type(type).build();
294 }
295
296
297
298
299
300
301 @Nonnull
302 public Dependency withClassifier(String classifier) {
303 return newBuilder(this, true).classifier(classifier).build();
304 }
305
306
307
308
309
310
311 @Nonnull
312 public Dependency withScope(String scope) {
313 return newBuilder(this, true).scope(scope).build();
314 }
315
316
317
318
319
320
321 @Nonnull
322 public Dependency withSystemPath(String systemPath) {
323 return newBuilder(this, true).systemPath(systemPath).build();
324 }
325
326
327
328
329
330
331 @Nonnull
332 public Dependency withExclusions(Collection<Exclusion> exclusions) {
333 return newBuilder(this, true).exclusions(exclusions).build();
334 }
335
336
337
338
339
340
341 @Nonnull
342 public Dependency withOptional(String optional) {
343 return newBuilder(this, true).optional(optional).build();
344 }
345
346
347
348
349
350
351
352
353 @Nonnull
354 public static Dependency newInstance() {
355 return newInstance(true);
356 }
357
358
359
360
361
362
363
364
365 @Nonnull
366 public static Dependency newInstance(boolean withDefaults) {
367 return newBuilder(withDefaults).build();
368 }
369
370
371
372
373
374
375
376
377 @Nonnull
378 public static Builder newBuilder() {
379 return newBuilder(true);
380 }
381
382
383
384
385
386
387
388 @Nonnull
389 public static Builder newBuilder(boolean withDefaults) {
390 return new Builder(withDefaults);
391 }
392
393
394
395
396
397
398
399
400 @Nonnull
401 public static Builder newBuilder(Dependency from) {
402 return newBuilder(from, false);
403 }
404
405
406
407
408
409
410
411
412 @Nonnull
413 public static Builder newBuilder(Dependency from, boolean forceCopy) {
414 return new Builder(from, forceCopy);
415 }
416
417
418
419
420
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 {
573 return ( getOptional() != null ) ? Boolean.parseBoolean( getOptional() ) : false;
574 }
575
576
577
578
579
580
581
582
583 public String toString()
584 {
585 return "Dependency {groupId=" + getGroupId() + ", artifactId=" + getArtifactId() + ", version=" + getVersion() + ", type=" + getType() + "}";
586 }
587
588
589
590
591 private volatile String managementKey;
592
593
594
595
596 public String getManagementKey()
597 {
598 if ( managementKey == null )
599 {
600 managementKey = getGroupId() + ":" + getArtifactId() + ":" + getType() + ( getClassifier() != null ? ":" + getClassifier() : "" );
601 }
602 return managementKey;
603 }
604
605
606 }