1 package org.apache.maven.artifact; 2 3 18 19 import org.apache.maven.artifact.versioning.VersionRange; 20 21 import java.util.ArrayList ; 22 import java.util.Collection ; 23 import java.util.HashMap ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 import java.util.Map ; 27 28 public final class ArtifactUtils 29 { 30 31 private ArtifactUtils() 32 { 33 } 34 35 public static boolean isSnapshot( String version ) 36 { 37 return version != null && 38 ( version.toUpperCase().endsWith( "SNAPSHOT" ) || Artifact.VERSION_FILE_PATTERN.matcher( version ) 39 .matches() ); 40 } 41 42 public static String versionlessKey( Artifact artifact ) 43 { 44 return versionlessKey( artifact.getGroupId(), artifact.getArtifactId() ); 45 } 46 47 public static String versionlessKey( String groupId, String artifactId ) 48 { 49 if ( groupId == null ) 50 { 51 throw new NullPointerException ( "groupId was null" ); 52 } 53 if ( artifactId == null ) 54 { 55 throw new NullPointerException ( "artifactId was null" ); 56 } 57 return groupId + ":" + artifactId; 58 } 59 60 public static String artifactId( String groupId, String artifactId, String type, String version ) 61 { 62 return artifactId( groupId, artifactId, type, null, version ); 63 } 64 65 public static String artifactId( String groupId, String artifactId, String type, String classifier, 66 String baseVersion ) 67 { 68 return groupId + ":" + artifactId + ":" + type + ( classifier != null ? ":" + classifier : "" ) + ":" + 69 baseVersion; 70 } 71 72 public static Map artifactMapByVersionlessId( Collection artifacts ) 73 { 74 Map artifactMap = new HashMap (); 75 76 if ( artifacts != null ) 77 { 78 for ( Iterator it = artifacts.iterator(); it.hasNext(); ) 79 { 80 Artifact artifact = (Artifact) it.next(); 81 82 artifactMap.put( versionlessKey( artifact ), artifact ); 83 } 84 } 85 86 return artifactMap; 87 } 88 89 public static Map artifactMapByArtifactId( Collection artifacts ) 90 { 91 Map artifactMap = new HashMap (); 92 93 if ( artifacts != null ) 94 { 95 for ( Iterator it = artifacts.iterator(); it.hasNext(); ) 96 { 97 Artifact artifact = (Artifact) it.next(); 98 99 artifactMap.put( artifact.getId(), artifact ); 100 } 101 } 102 103 return artifactMap; 104 } 105 106 public static Artifact copyArtifact( Artifact artifact ) 107 { 108 VersionRange range = artifact.getVersionRange(); 109 DefaultArtifact clone = new DefaultArtifact( artifact.getGroupId(), artifact.getArtifactId(), range.cloneOf(), 110 artifact.getScope(), artifact.getType(), artifact.getClassifier(), 111 artifact.getArtifactHandler(), artifact.isOptional() ); 112 clone.setRelease( artifact.isRelease() ); 113 clone.setResolvedVersion( artifact.getVersion() ); 114 clone.setResolved( artifact.isResolved() ); 115 clone.setFile( artifact.getFile() ); 116 117 clone.setAvailableVersions( copyList( artifact.getAvailableVersions() ) ); 118 clone.setBaseVersion( artifact.getBaseVersion() ); 119 clone.setDependencyFilter( artifact.getDependencyFilter() ); 120 clone.setDependencyTrail( copyList( artifact.getDependencyTrail() ) ); 121 clone.setDownloadUrl( artifact.getDownloadUrl() ); 122 clone.setRepository( artifact.getRepository() ); 123 124 return clone; 125 } 126 127 private static List copyList( List original ) 128 { 129 List copy = null; 130 131 if ( original != null ) 132 { 133 copy = new ArrayList (); 134 135 if ( !original.isEmpty() ) 136 { 137 copy.addAll( original ); 138 } 139 } 140 141 return copy; 142 } 143 144 } 145 | Popular Tags |