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