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 Validate.notBlank( version, "version can neither be null, empty nor blank" );
58
59 Matcher m = Artifact.VERSION_FILE_PATTERN.matcher( version );
60 if ( m.matches() )
61 {
62 return m.group( 1 ) + "-" + Artifact.SNAPSHOT_VERSION;
63 }
64 else
65 {
66 return version;
67 }
68 }
69
70 public static String versionlessKey( Artifact artifact )
71 {
72 return versionlessKey( artifact.getGroupId(), artifact.getArtifactId() );
73 }
74
75 public static String versionlessKey( String groupId, String artifactId )
76 {
77 Validate.notBlank( groupId, "groupId can neither be null, empty nor blank" );
78 Validate.notBlank( artifactId, "artifactId can neither be null, empty nor blank" );
79
80 return groupId + ":" + artifactId;
81 }
82
83 public static String key( Artifact artifact )
84 {
85 return key( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion() );
86 }
87
88 public static String key( String groupId, String artifactId, String version )
89 {
90 Validate.notBlank( groupId, "groupId can neither be null, empty nor blank" );
91 Validate.notBlank( artifactId, "artifactId can neither be null, empty nor blank" );
92 Validate.notBlank( version, "version can neither be null, empty nor blank" );
93
94 return groupId + ":" + artifactId + ":" + version;
95 }
96
97 public static Map<String, Artifact> artifactMapByVersionlessId( Collection<Artifact> artifacts )
98 {
99 Map<String, Artifact> artifactMap = new LinkedHashMap<>();
100
101 if ( artifacts != null )
102 {
103 for ( Artifact artifact : artifacts )
104 {
105 artifactMap.put( versionlessKey( artifact ), artifact );
106 }
107 }
108
109 return artifactMap;
110 }
111
112 public static Artifact copyArtifactSafe( Artifact artifact )
113 {
114 return ( artifact != null ) ? copyArtifact( artifact ) : null;
115 }
116
117 public static Artifact copyArtifact( Artifact artifact )
118 {
119 VersionRange range = artifact.getVersionRange();
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136 if ( range == null )
137 {
138 range = VersionRange.createFromVersion( artifact.getVersion() );
139 }
140
141 DefaultArtifact clone = new DefaultArtifact( artifact.getGroupId(), artifact.getArtifactId(), range.cloneOf(),
142 artifact.getScope(), artifact.getType(), artifact.getClassifier(),
143 artifact.getArtifactHandler(), artifact.isOptional() );
144 clone.setRelease( artifact.isRelease() );
145 clone.setResolvedVersion( artifact.getVersion() );
146 clone.setResolved( artifact.isResolved() );
147 clone.setFile( artifact.getFile() );
148
149 clone.setAvailableVersions( copyList( artifact.getAvailableVersions() ) );
150 if ( artifact.getVersion() != null )
151 {
152 clone.setBaseVersion( artifact.getBaseVersion() );
153 }
154 clone.setDependencyFilter( artifact.getDependencyFilter() );
155 clone.setDependencyTrail( copyList( artifact.getDependencyTrail() ) );
156 clone.setDownloadUrl( artifact.getDownloadUrl() );
157 clone.setRepository( artifact.getRepository() );
158
159 return clone;
160 }
161
162
163 public static <T extends Collection<Artifact>> T copyArtifacts( Collection<Artifact> from, T to )
164 {
165 for ( Artifact artifact : from )
166 {
167 to.add( ArtifactUtils.copyArtifact( artifact ) );
168 }
169 return to;
170 }
171
172 public static <K, T extends Map<K, Artifact>> T copyArtifacts( Map<K, ? extends Artifact> from, T to )
173 {
174 if ( from != null )
175 {
176 for ( Map.Entry<K, ? extends Artifact> entry : from.entrySet() )
177 {
178 to.put( entry.getKey(), ArtifactUtils.copyArtifact( entry.getValue() ) );
179 }
180 }
181
182 return to;
183 }
184
185 private static <T> List<T> copyList( List<T> original )
186 {
187 List<T> copy = null;
188
189 if ( original != null )
190 {
191 copy = new ArrayList<>();
192
193 if ( !original.isEmpty() )
194 {
195 copy.addAll( original );
196 }
197 }
198
199 return copy;
200 }
201
202 }