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