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