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 java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.LinkedHashMap;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.regex.Matcher;
28
29 import org.apache.commons.lang3.Validate;
30 import org.apache.maven.artifact.versioning.VersionRange;
31
32
33
34
35 public final class ArtifactUtils
36 {
37
38 public static boolean isSnapshot( String version )
39 {
40 if ( version != null )
41 {
42 if ( version.regionMatches( true, version.length() - Artifact.SNAPSHOT_VERSION.length(),
43 Artifact.SNAPSHOT_VERSION, 0, Artifact.SNAPSHOT_VERSION.length() ) )
44 {
45 return true;
46 }
47 else if ( Artifact.VERSION_FILE_PATTERN.matcher( version ).matches() )
48 {
49 return true;
50 }
51 }
52 return false;
53 }
54
55 public static String toSnapshotVersion( String version )
56 {
57 notBlank( version, "version can neither be null, empty nor blank" );
58
59 int lastHyphen = version.lastIndexOf( '-' );
60 if ( lastHyphen > 0 )
61 {
62 int prevHyphen = version.lastIndexOf( '-', lastHyphen - 1 );
63 if ( prevHyphen > 0 )
64 {
65 Matcher m = Artifact.VERSION_FILE_PATTERN.matcher( version );
66 if ( m.matches() )
67 {
68 return m.group( 1 ) + "-" + Artifact.SNAPSHOT_VERSION;
69 }
70 }
71 }
72 return version;
73 }
74
75 public static String versionlessKey( Artifact artifact )
76 {
77 return versionlessKey( artifact.getGroupId(), artifact.getArtifactId() );
78 }
79
80 public static String versionlessKey( String groupId, String artifactId )
81 {
82 notBlank( groupId, "groupId can neither be null, empty nor blank" );
83 notBlank( artifactId, "artifactId can neither be null, empty nor blank" );
84
85 return groupId + ":" + artifactId;
86 }
87
88 public static String key( Artifact artifact )
89 {
90 return key( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion() );
91 }
92
93 public static String key( String groupId, String artifactId, String version )
94 {
95 notBlank( groupId, "groupId can neither be null, empty nor blank" );
96 notBlank( artifactId, "artifactId can neither be null, empty nor blank" );
97 notBlank( version, "version can neither be null, empty nor blank" );
98
99 return groupId + ":" + artifactId + ":" + version;
100 }
101
102 private static void notBlank( String str, String message )
103 {
104 int c = str != null && str.length() > 0 ? str.charAt( 0 ) : 0;
105 if ( ( c < '0' || c > '9' ) && ( c < 'a' || c > 'z' ) )
106 {
107 Validate.notBlank( str, message );
108 }
109 }
110
111 public static Map<String, Artifact> artifactMapByVersionlessId( Collection<Artifact> artifacts )
112 {
113 Map<String, Artifact> artifactMap = new LinkedHashMap<>();
114
115 if ( artifacts != null )
116 {
117 for ( Artifact artifact : artifacts )
118 {
119 artifactMap.put( versionlessKey( artifact ), artifact );
120 }
121 }
122
123 return artifactMap;
124 }
125
126 public static Artifact copyArtifactSafe( Artifact artifact )
127 {
128 return ( artifact != null ) ? copyArtifact( artifact ) : null;
129 }
130
131 public static Artifact copyArtifact( Artifact artifact )
132 {
133 VersionRange range = artifact.getVersionRange();
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150 if ( range == null )
151 {
152 range = VersionRange.createFromVersion( artifact.getVersion() );
153 }
154
155 DefaultArtifact clone = new DefaultArtifact( artifact.getGroupId(), artifact.getArtifactId(), range,
156 artifact.getScope(), artifact.getType(), artifact.getClassifier(),
157 artifact.getArtifactHandler(), artifact.isOptional() );
158 clone.setRelease( artifact.isRelease() );
159 clone.setResolvedVersion( artifact.getVersion() );
160 clone.setResolved( artifact.isResolved() );
161 clone.setFile( artifact.getFile() );
162
163 clone.setAvailableVersions( copyList( artifact.getAvailableVersions() ) );
164 if ( artifact.getVersion() != null )
165 {
166 clone.setBaseVersion( artifact.getBaseVersion() );
167 }
168 clone.setDependencyFilter( artifact.getDependencyFilter() );
169 clone.setDependencyTrail( copyList( artifact.getDependencyTrail() ) );
170 clone.setDownloadUrl( artifact.getDownloadUrl() );
171 clone.setRepository( artifact.getRepository() );
172
173 return clone;
174 }
175
176
177 public static <T extends Collection<Artifact>> T copyArtifacts( Collection<Artifact> from, T to )
178 {
179 for ( Artifact artifact : from )
180 {
181 to.add( ArtifactUtils.copyArtifact( artifact ) );
182 }
183 return to;
184 }
185
186 public static <K, T extends Map<K, Artifact>> T copyArtifacts( Map<K, ? extends Artifact> from, T to )
187 {
188 if ( from != null )
189 {
190 for ( Map.Entry<K, ? extends Artifact> entry : from.entrySet() )
191 {
192 to.put( entry.getKey(), ArtifactUtils.copyArtifact( entry.getValue() ) );
193 }
194 }
195
196 return to;
197 }
198
199 private static <T> List<T> copyList( List<T> original )
200 {
201 List<T> copy = null;
202
203 if ( original != null )
204 {
205 copy = new ArrayList<>();
206
207 if ( !original.isEmpty() )
208 {
209 copy.addAll( original );
210 }
211 }
212
213 return copy;
214 }
215
216 }