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