1 | |
package org.apache.maven.artifact; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import org.apache.maven.artifact.handler.ArtifactHandler; |
23 | |
import org.apache.maven.artifact.metadata.ArtifactMetadata; |
24 | |
import org.apache.maven.artifact.repository.ArtifactRepository; |
25 | |
import org.apache.maven.artifact.resolver.filter.ArtifactFilter; |
26 | |
import org.apache.maven.artifact.versioning.ArtifactVersion; |
27 | |
import org.apache.maven.artifact.versioning.OverConstrainedVersionException; |
28 | |
import org.apache.maven.artifact.versioning.VersionRange; |
29 | |
import org.codehaus.plexus.util.StringUtils; |
30 | |
|
31 | |
import java.io.File; |
32 | |
import java.util.Collection; |
33 | |
import java.util.Collections; |
34 | |
import java.util.HashMap; |
35 | |
import java.util.Iterator; |
36 | |
import java.util.List; |
37 | |
import java.util.Map; |
38 | |
import java.util.regex.Matcher; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
public class DefaultArtifact |
46 | |
implements Artifact |
47 | |
{ |
48 | |
private String groupId; |
49 | |
|
50 | |
private String artifactId; |
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
private String baseVersion; |
58 | |
|
59 | |
private final String type; |
60 | |
|
61 | |
private final String classifier; |
62 | |
|
63 | |
private String scope; |
64 | |
|
65 | |
private File file; |
66 | |
|
67 | |
private ArtifactRepository repository; |
68 | |
|
69 | |
private String downloadUrl; |
70 | |
|
71 | |
private ArtifactFilter dependencyFilter; |
72 | |
|
73 | |
private ArtifactHandler artifactHandler; |
74 | |
|
75 | |
private List dependencyTrail; |
76 | |
|
77 | |
private String version; |
78 | |
|
79 | |
private VersionRange versionRange; |
80 | |
|
81 | |
private boolean resolved; |
82 | |
|
83 | |
private boolean release; |
84 | |
|
85 | |
private List availableVersions; |
86 | |
|
87 | |
private Map metadataMap; |
88 | |
|
89 | |
private boolean optional; |
90 | |
|
91 | |
public DefaultArtifact( String groupId, String artifactId, VersionRange versionRange, String scope, String type, |
92 | |
String classifier, ArtifactHandler artifactHandler ) |
93 | |
{ |
94 | 30 | this( groupId, artifactId, versionRange, scope, type, classifier, artifactHandler, false ); |
95 | 30 | } |
96 | |
|
97 | |
public DefaultArtifact( String groupId, String artifactId, VersionRange versionRange, String scope, String type, |
98 | |
String classifier, ArtifactHandler artifactHandler, boolean optional ) |
99 | 801 | { |
100 | 801 | this.groupId = groupId; |
101 | |
|
102 | 801 | this.artifactId = artifactId; |
103 | |
|
104 | 801 | this.versionRange = versionRange; |
105 | |
|
106 | 801 | selectVersionFromNewRangeIfAvailable(); |
107 | |
|
108 | 801 | this.artifactHandler = artifactHandler; |
109 | |
|
110 | 801 | this.scope = scope; |
111 | |
|
112 | 801 | this.type = type; |
113 | |
|
114 | 801 | if ( classifier == null ) |
115 | |
{ |
116 | 773 | classifier = artifactHandler.getClassifier(); |
117 | |
} |
118 | |
|
119 | 801 | this.classifier = classifier; |
120 | |
|
121 | 801 | this.optional = optional; |
122 | |
|
123 | 801 | validateIdentity(); |
124 | 801 | } |
125 | |
|
126 | |
private void validateIdentity() |
127 | |
{ |
128 | 801 | if ( empty( groupId ) ) |
129 | |
{ |
130 | 0 | throw new InvalidArtifactRTException( groupId, artifactId, getVersion(), type, |
131 | |
"The groupId cannot be empty." ); |
132 | |
} |
133 | |
|
134 | 801 | if ( artifactId == null ) |
135 | |
{ |
136 | 0 | throw new InvalidArtifactRTException( groupId, artifactId, getVersion(), type, |
137 | |
"The artifactId cannot be empty." ); |
138 | |
} |
139 | |
|
140 | 801 | if ( type == null ) |
141 | |
{ |
142 | 0 | throw new InvalidArtifactRTException( groupId, artifactId, getVersion(), type, |
143 | |
"The type cannot be empty." ); |
144 | |
} |
145 | |
|
146 | 801 | if ( ( version == null ) && ( versionRange == null ) ) |
147 | |
{ |
148 | 0 | throw new InvalidArtifactRTException( groupId, artifactId, getVersion(), type, |
149 | |
"The version cannot be empty." ); |
150 | |
} |
151 | 801 | } |
152 | |
|
153 | |
private boolean empty( String value ) |
154 | |
{ |
155 | 801 | return ( value == null ) || ( value.trim().length() < 1 ); |
156 | |
} |
157 | |
|
158 | |
public String getClassifier() |
159 | |
{ |
160 | 306 | return classifier; |
161 | |
} |
162 | |
|
163 | |
public boolean hasClassifier() |
164 | |
{ |
165 | 6422 | return StringUtils.isNotEmpty( classifier ); |
166 | |
} |
167 | |
|
168 | |
public String getScope() |
169 | |
{ |
170 | 4816 | return scope; |
171 | |
} |
172 | |
|
173 | |
public String getGroupId() |
174 | |
{ |
175 | 6879 | return groupId; |
176 | |
} |
177 | |
|
178 | |
public String getArtifactId() |
179 | |
{ |
180 | 7076 | return artifactId; |
181 | |
} |
182 | |
|
183 | |
public String getVersion() |
184 | |
{ |
185 | 4096 | return version; |
186 | |
} |
187 | |
|
188 | |
public void setVersion( String version ) |
189 | |
{ |
190 | 23 | this.version = version; |
191 | 23 | setBaseVersionInternal( version ); |
192 | 23 | versionRange = null; |
193 | 23 | } |
194 | |
|
195 | |
public String getType() |
196 | |
{ |
197 | 6769 | return type; |
198 | |
} |
199 | |
|
200 | |
public void setFile( File file ) |
201 | |
{ |
202 | 0 | this.file = file; |
203 | 0 | } |
204 | |
|
205 | |
public File getFile() |
206 | |
{ |
207 | 0 | return file; |
208 | |
} |
209 | |
|
210 | |
public ArtifactRepository getRepository() |
211 | |
{ |
212 | 0 | return repository; |
213 | |
} |
214 | |
|
215 | |
public void setRepository( ArtifactRepository repository ) |
216 | |
{ |
217 | 0 | this.repository = repository; |
218 | 0 | } |
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
public String getId() |
225 | |
{ |
226 | 1917 | return getDependencyConflictId() + ":" + getBaseVersion(); |
227 | |
} |
228 | |
|
229 | |
public String getDependencyConflictId() |
230 | |
{ |
231 | 6418 | StringBuffer sb = new StringBuffer(); |
232 | 6418 | sb.append( getGroupId() ); |
233 | 6418 | sb.append( ":" ); |
234 | 6418 | appendArtifactTypeClassifierString( sb ); |
235 | 6418 | return sb.toString(); |
236 | |
} |
237 | |
|
238 | |
private void appendArtifactTypeClassifierString( StringBuffer sb ) |
239 | |
{ |
240 | 6422 | sb.append( getArtifactId() ); |
241 | 6422 | sb.append( ":" ); |
242 | 6422 | sb.append( getType() ); |
243 | 6422 | if ( hasClassifier() ) |
244 | |
{ |
245 | 11 | sb.append( ":" ); |
246 | 11 | sb.append( getClassifier() ); |
247 | |
} |
248 | 6422 | } |
249 | |
|
250 | |
public void addMetadata( ArtifactMetadata metadata ) |
251 | |
{ |
252 | 0 | if ( metadataMap == null ) |
253 | |
{ |
254 | 0 | metadataMap = new HashMap(); |
255 | |
} |
256 | |
|
257 | 0 | ArtifactMetadata m = (ArtifactMetadata) metadataMap.get( metadata.getKey() ); |
258 | 0 | if ( m != null ) |
259 | |
{ |
260 | 0 | m.merge( metadata ); |
261 | |
} |
262 | |
else |
263 | |
{ |
264 | 0 | metadataMap.put( metadata.getKey(), metadata ); |
265 | |
} |
266 | 0 | } |
267 | |
|
268 | |
public ArtifactMetadata getMetadata( Class metadataClass ) |
269 | |
{ |
270 | 0 | Collection metadata = getMetadataList(); |
271 | |
|
272 | 0 | if ( metadata != null ) |
273 | |
{ |
274 | 0 | for ( Iterator it = metadata.iterator(); it.hasNext(); ) |
275 | |
{ |
276 | 0 | ArtifactMetadata m = (ArtifactMetadata) it.next(); |
277 | 0 | if ( metadataClass.isAssignableFrom( m.getClass() ) ) |
278 | |
{ |
279 | 0 | return m; |
280 | |
} |
281 | |
} |
282 | |
} |
283 | |
|
284 | 0 | return null; |
285 | |
} |
286 | |
|
287 | |
public Collection getMetadataList() |
288 | |
{ |
289 | 0 | return metadataMap == null ? Collections.EMPTY_LIST : metadataMap.values(); |
290 | |
} |
291 | |
|
292 | |
|
293 | |
|
294 | |
|
295 | |
|
296 | |
public String toString() |
297 | |
{ |
298 | 4 | StringBuffer sb = new StringBuffer(); |
299 | 4 | if ( getGroupId() != null ) |
300 | |
{ |
301 | 3 | sb.append( getGroupId() ); |
302 | 3 | sb.append( ":" ); |
303 | |
} |
304 | 4 | appendArtifactTypeClassifierString( sb ); |
305 | 4 | sb.append( ":" ); |
306 | 4 | if ( getBaseVersionInternal() != null ) |
307 | |
{ |
308 | 4 | sb.append( getBaseVersionInternal() ); |
309 | |
} |
310 | |
else |
311 | |
{ |
312 | 0 | sb.append( versionRange.toString() ); |
313 | |
} |
314 | 4 | if ( scope != null ) |
315 | |
{ |
316 | 3 | sb.append( ":" ); |
317 | 3 | sb.append( scope ); |
318 | |
} |
319 | 4 | return sb.toString(); |
320 | |
} |
321 | |
|
322 | |
public int hashCode() |
323 | |
{ |
324 | 1290 | int result = 17; |
325 | 1290 | result = 37 * result + groupId.hashCode(); |
326 | 1290 | result = 37 * result + artifactId.hashCode(); |
327 | 1290 | result = 37 * result + type.hashCode(); |
328 | 1290 | if ( version != null ) |
329 | |
{ |
330 | 1260 | result = 37 * result + version.hashCode(); |
331 | |
} |
332 | 1290 | result = 37 * result + ( classifier != null ? classifier.hashCode() : 0 ); |
333 | 1290 | return result; |
334 | |
} |
335 | |
|
336 | |
public boolean equals( Object o ) |
337 | |
{ |
338 | 598 | if ( o == this ) |
339 | |
{ |
340 | 561 | return true; |
341 | |
} |
342 | |
|
343 | 37 | if ( !( o instanceof Artifact ) ) |
344 | |
{ |
345 | 0 | return false; |
346 | |
} |
347 | |
|
348 | 37 | Artifact a = (Artifact) o; |
349 | |
|
350 | 37 | if ( !a.getGroupId().equals( groupId ) ) |
351 | |
{ |
352 | 0 | return false; |
353 | |
} |
354 | 37 | else if ( !a.getArtifactId().equals( artifactId ) ) |
355 | |
{ |
356 | 0 | return false; |
357 | |
} |
358 | 37 | else if ( !a.getVersion().equals( version ) ) |
359 | |
{ |
360 | 0 | return false; |
361 | |
} |
362 | 37 | else if ( !a.getType().equals( type ) ) |
363 | |
{ |
364 | 0 | return false; |
365 | |
} |
366 | 37 | else if ( a.getClassifier() == null ? classifier != null : !a.getClassifier().equals( classifier ) ) |
367 | |
{ |
368 | 0 | return false; |
369 | |
} |
370 | |
|
371 | |
|
372 | |
|
373 | 37 | return true; |
374 | |
} |
375 | |
|
376 | |
public String getBaseVersion() |
377 | |
{ |
378 | 1918 | if ( baseVersion == null ) |
379 | |
{ |
380 | 0 | if ( version == null ) |
381 | |
{ |
382 | 0 | throw new NullPointerException( "version was null for " + groupId + ":" + artifactId ); |
383 | |
} |
384 | 0 | setBaseVersionInternal( version ); |
385 | |
} |
386 | 1918 | return baseVersion; |
387 | |
} |
388 | |
|
389 | |
protected String getBaseVersionInternal() |
390 | |
{ |
391 | 8 | if ( ( baseVersion == null ) && ( version != null ) ) |
392 | |
{ |
393 | 0 | setBaseVersionInternal( version ); |
394 | |
} |
395 | |
|
396 | 8 | return baseVersion; |
397 | |
} |
398 | |
|
399 | |
public void setBaseVersion( String baseVersion ) |
400 | |
{ |
401 | 0 | setBaseVersionInternal( baseVersion ); |
402 | 0 | } |
403 | |
|
404 | |
protected void setBaseVersionInternal( String baseVersion ) |
405 | |
{ |
406 | 1083 | Matcher m = VERSION_FILE_PATTERN.matcher( baseVersion ); |
407 | 1083 | if ( m.matches() ) |
408 | |
{ |
409 | 9 | this.baseVersion = m.group( 1 ) + "-" + SNAPSHOT_VERSION; |
410 | |
} |
411 | |
else |
412 | |
{ |
413 | 1074 | this.baseVersion = baseVersion; |
414 | |
} |
415 | 1083 | } |
416 | |
|
417 | |
public int compareTo( Object o ) |
418 | |
{ |
419 | 0 | Artifact a = (Artifact) o; |
420 | |
|
421 | 0 | int result = groupId.compareTo( a.getGroupId() ); |
422 | 0 | if ( result == 0 ) |
423 | |
{ |
424 | 0 | result = artifactId.compareTo( a.getArtifactId() ); |
425 | 0 | if ( result == 0 ) |
426 | |
{ |
427 | 0 | result = type.compareTo( a.getType() ); |
428 | 0 | if ( result == 0 ) |
429 | |
{ |
430 | 0 | if ( classifier == null ) |
431 | |
{ |
432 | 0 | if ( a.getClassifier() != null ) |
433 | |
{ |
434 | 0 | result = 1; |
435 | |
} |
436 | |
} |
437 | |
else |
438 | |
{ |
439 | 0 | if ( a.getClassifier() != null ) |
440 | |
{ |
441 | 0 | result = classifier.compareTo( a.getClassifier() ); |
442 | |
} |
443 | |
else |
444 | |
{ |
445 | 0 | result = -1; |
446 | |
} |
447 | |
} |
448 | 0 | if ( result == 0 ) |
449 | |
{ |
450 | |
|
451 | 0 | result = version.compareTo( a.getVersion() ); |
452 | |
} |
453 | |
} |
454 | |
} |
455 | |
} |
456 | 0 | return result; |
457 | |
} |
458 | |
|
459 | |
public void updateVersion( String version, ArtifactRepository localRepository ) |
460 | |
{ |
461 | 0 | setResolvedVersion( version ); |
462 | 0 | setFile( new File( localRepository.getBasedir(), localRepository.pathOf( this ) ) ); |
463 | 0 | } |
464 | |
|
465 | |
public String getDownloadUrl() |
466 | |
{ |
467 | 0 | return downloadUrl; |
468 | |
} |
469 | |
|
470 | |
public void setDownloadUrl( String downloadUrl ) |
471 | |
{ |
472 | 0 | this.downloadUrl = downloadUrl; |
473 | 0 | } |
474 | |
|
475 | |
public ArtifactFilter getDependencyFilter() |
476 | |
{ |
477 | 1096 | return dependencyFilter; |
478 | |
} |
479 | |
|
480 | |
public void setDependencyFilter( ArtifactFilter artifactFilter ) |
481 | |
{ |
482 | 306 | dependencyFilter = artifactFilter; |
483 | 306 | } |
484 | |
|
485 | |
public ArtifactHandler getArtifactHandler() |
486 | |
{ |
487 | 0 | return artifactHandler; |
488 | |
} |
489 | |
|
490 | |
public List getDependencyTrail() |
491 | |
{ |
492 | 4 | return dependencyTrail; |
493 | |
} |
494 | |
|
495 | |
public void setDependencyTrail( List dependencyTrail ) |
496 | |
{ |
497 | 943 | this.dependencyTrail = dependencyTrail; |
498 | 943 | } |
499 | |
|
500 | |
public void setScope( String scope ) |
501 | |
{ |
502 | 25 | this.scope = scope; |
503 | 25 | } |
504 | |
|
505 | |
public VersionRange getVersionRange() |
506 | |
{ |
507 | |
|
508 | |
|
509 | |
|
510 | 925 | if ( versionRange == null ) |
511 | |
{ |
512 | 9 | versionRange = VersionRange.createFromVersion( version ); |
513 | |
} |
514 | |
|
515 | 925 | return versionRange; |
516 | |
} |
517 | |
|
518 | |
public void setVersionRange( VersionRange versionRange ) |
519 | |
{ |
520 | 280 | this.versionRange = versionRange; |
521 | |
|
522 | 280 | selectVersionFromNewRangeIfAvailable(); |
523 | 280 | } |
524 | |
|
525 | |
private void selectVersionFromNewRangeIfAvailable() |
526 | |
{ |
527 | 1081 | if ( ( versionRange != null ) && ( versionRange.getRecommendedVersion() != null ) ) |
528 | |
{ |
529 | 1041 | selectVersion( versionRange.getRecommendedVersion().toString() ); |
530 | |
} |
531 | |
else |
532 | |
{ |
533 | 40 | version = null; |
534 | 40 | baseVersion = null; |
535 | |
} |
536 | 1081 | } |
537 | |
|
538 | |
public void selectVersion( String version ) |
539 | |
{ |
540 | 1060 | this.version = version; |
541 | 1060 | setBaseVersionInternal( version ); |
542 | 1060 | } |
543 | |
|
544 | |
public void setGroupId( String groupId ) |
545 | |
{ |
546 | 2 | this.groupId = groupId; |
547 | 2 | } |
548 | |
|
549 | |
public void setArtifactId( String artifactId ) |
550 | |
{ |
551 | 0 | this.artifactId = artifactId; |
552 | 0 | } |
553 | |
|
554 | |
public boolean isSnapshot() |
555 | |
{ |
556 | 0 | if ( getBaseVersion() != null ) |
557 | |
{ |
558 | 0 | return getBaseVersion().endsWith( SNAPSHOT_VERSION ) || getBaseVersion().equals( LATEST_VERSION ); |
559 | |
} |
560 | |
else |
561 | |
{ |
562 | 0 | return false; |
563 | |
} |
564 | |
} |
565 | |
|
566 | |
public void setResolved( boolean resolved ) |
567 | |
{ |
568 | 0 | this.resolved = resolved; |
569 | 0 | } |
570 | |
|
571 | |
public boolean isResolved() |
572 | |
{ |
573 | 0 | return resolved; |
574 | |
} |
575 | |
|
576 | |
public void setResolvedVersion( String version ) |
577 | |
{ |
578 | 0 | this.version = version; |
579 | |
|
580 | 0 | } |
581 | |
|
582 | |
public void setArtifactHandler( ArtifactHandler artifactHandler ) |
583 | |
{ |
584 | 0 | this.artifactHandler = artifactHandler; |
585 | 0 | } |
586 | |
|
587 | |
public void setRelease( boolean release ) |
588 | |
{ |
589 | 0 | this.release = release; |
590 | 0 | } |
591 | |
|
592 | |
public boolean isRelease() |
593 | |
{ |
594 | 0 | return release; |
595 | |
} |
596 | |
|
597 | |
public List getAvailableVersions() |
598 | |
{ |
599 | 33 | return availableVersions; |
600 | |
} |
601 | |
|
602 | |
public void setAvailableVersions( List availableVersions ) |
603 | |
{ |
604 | 17 | this.availableVersions = availableVersions; |
605 | 17 | } |
606 | |
|
607 | |
public boolean isOptional() |
608 | |
{ |
609 | 959 | return optional; |
610 | |
} |
611 | |
|
612 | |
public ArtifactVersion getSelectedVersion() |
613 | |
throws OverConstrainedVersionException |
614 | |
{ |
615 | 0 | return versionRange.getSelectedVersion( this ); |
616 | |
} |
617 | |
|
618 | |
public boolean isSelectedVersionKnown() |
619 | |
throws OverConstrainedVersionException |
620 | |
{ |
621 | 13 | return versionRange.isSelectedVersionKnown( this ); |
622 | |
} |
623 | |
|
624 | |
public void setOptional( boolean optional ) |
625 | |
{ |
626 | 0 | this.optional = optional; |
627 | 0 | } |
628 | |
} |