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