Skip to content

Commit db0aeb8

Browse files
committed
Add support for snapshotted Maven artifacts
Artifacts with snapshots follow a different naming scheme than non-snapshoted ones. For snapshotted artifacts, the version component in the URL is the generic *-SNAPSHOT name, whereas the version component in the file name is the specific version number. Fixes #901.
1 parent e6c1ff2 commit db0aeb8

File tree

5 files changed

+86
-17
lines changed

5 files changed

+86
-17
lines changed

private/rules/v2_lock_file.bzl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,13 @@ def _compute_lock_file_hash(lock_file_contents):
4343
return hash(repr(to_hash))
4444

4545
def _to_m2_path(unpacked):
46-
path = "{group}/{artifact}/{version}/{artifact}-{version}".format(
46+
version = unpacked["version"]
47+
dir_version = getattr(unpacked, "dirVersion", version)
48+
path = "{group}/{artifact}/{dir_version}/{artifact}-{version}".format(
4749
artifact = unpacked["artifactId"],
4850
group = unpacked["groupId"].replace(".", "/"),
49-
version = unpacked["version"],
51+
version = version,
52+
dir_version = dir_version,
5053
)
5154

5255
classifier = unpacked.get("scope", "jar")
@@ -104,6 +107,8 @@ def _get_artifacts(lock_file_contents):
104107
"artifactId": parts[1],
105108
"version": data["version"],
106109
}
110+
if "dirVersion" in data:
111+
root_unpacked["dirVersion"] = data["dirVersion"]
107112
if len(parts) > 2:
108113
root_unpacked["type"] = parts[2]
109114
else:

private/tools/java/com/github/bazelbuild/rules_jvm_external/Coordinates.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,15 @@
55
/**
66
* Represents a Maven coordinate using Maven's standard schema of
77
* <groupId>:<artifactId>[:<extension>[:<classifier>][:<version>].
8+
*
9+
* <p>The optional <tt>dirVersion</tt> property is used for snapshotted artifacts. For those,
10+
* directory version component in the repository URL is of the <tt>*-SNAPSHOT</tt> * form
11+
* whereas the version in the artifact itself numeric.</p>
812
*/
913
public class Coordinates implements Comparable<Coordinates> {
1014
private final String groupId;
1115
private final String artifactId;
16+
private final String dirVersion;
1217
private final String version;
1318
private final String classifier;
1419
private final String extension;
@@ -27,6 +32,7 @@ public Coordinates(String coordinates) {
2732

2833
groupId = Objects.requireNonNull(parts[0]);
2934
artifactId = Objects.requireNonNull(parts[1]);
35+
dirVersion = null;
3036

3137
if (parts.length == 2) {
3238
extension = "jar";
@@ -48,12 +54,13 @@ public Coordinates(String coordinates) {
4854
}
4955

5056
public Coordinates(
51-
String groupId, String artifactId, String extension, String classifier, String version) {
57+
String groupId, String artifactId, String extension, String classifier, String version, String dirVersion) {
5258
this.groupId = Objects.requireNonNull(groupId, "Group ID");
5359
this.artifactId = Objects.requireNonNull(artifactId, "Artifact ID");
5460
this.extension = extension == null || extension.isEmpty() ? "jar" : extension;
5561
this.classifier = classifier == null || classifier.isEmpty() ? "" : classifier;
5662
this.version = version == null || version.isEmpty() ? "" : version;
63+
this.dirVersion = dirVersion;
5764
}
5865

5966
public String getGroupId() {
@@ -64,6 +71,10 @@ public String getArtifactId() {
6471
return artifactId;
6572
}
6673

74+
public String getDirVersion() {
75+
return dirVersion;
76+
}
77+
6778
public String getVersion() {
6879
return version;
6980
}
@@ -73,11 +84,13 @@ public String getClassifier() {
7384
}
7485

7586
public Coordinates setClassifier(String classifier) {
76-
return new Coordinates(getGroupId(), getArtifactId(), getExtension(), classifier, getVersion());
87+
return new Coordinates(
88+
getGroupId(), getArtifactId(), getExtension(), classifier, getVersion(), getDirVersion());
7789
}
7890

7991
public Coordinates setExtension(String extension) {
80-
return new Coordinates(getGroupId(), getArtifactId(), extension, getClassifier(), getVersion());
92+
return new Coordinates(
93+
getGroupId(), getArtifactId(), extension, getClassifier(), getVersion(), getDirVersion());
8194
}
8295

8396
public String getExtension() {
@@ -132,6 +145,7 @@ public boolean equals(Object o) {
132145
Coordinates that = (Coordinates) o;
133146
return getGroupId().equals(that.getGroupId())
134147
&& getArtifactId().equals(that.getArtifactId())
148+
&& Objects.equals(getDirVersion(), that.getDirVersion())
135149
&& Objects.equals(getVersion(), that.getVersion())
136150
&& Objects.equals(getClassifier(), that.getClassifier())
137151
&& Objects.equals(getExtension(), that.getExtension());
@@ -140,6 +154,7 @@ && getArtifactId().equals(that.getArtifactId())
140154
@Override
141155
public int hashCode() {
142156
return Objects.hash(
143-
getGroupId(), getArtifactId(), getVersion(), getClassifier(), getExtension());
157+
getGroupId(), getArtifactId(), getDirVersion(), getVersion(), getClassifier(),
158+
getExtension());
144159
}
145160
}

private/tools/java/com/github/bazelbuild/rules_jvm_external/coursier/LockFileConverter.java

Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -256,21 +256,66 @@ private Map<Coordinates, Coordinates> deriveCoordinateMappings(Map<String, Objec
256256
// After the version, there should be nothing left but a file name
257257
pathSubstring = pathSubstring.substring(version.length() + 1);
258258

259-
// Now we know the version, we can calculate the expected file name. For now, ignore
260-
// the fact that there may be a classifier. We're going to derive that if necessary.
261-
String expectedFileName = coord.getArtifactId() + "-" + version;
259+
String remainder;
260+
String dirVersion;
261+
if (version.endsWith("-SNAPSHOT")) {
262+
String baseVersion = version.substring(0, version.length() - "-SNAPSHOT".length());
262263

263-
index = pathSubstring.indexOf(expectedFileName);
264-
if (index == -1) {
265-
throw new IllegalArgumentException(
266-
String.format(
267-
"Expected file name (%s) not found in path (%s). Current coordinates are %s",
268-
expectedFileName, file, coord));
264+
String expectedFileName = coord.getArtifactId() + "-" + baseVersion + "YYYYMMDD-HHMMSS.N+";
265+
266+
index = pathSubstring.indexOf(baseVersion);
267+
if (index == -1) {
268+
throw new IllegalArgumentException(
269+
String.format(
270+
"Expected artifact (%s) not found in path (%s). Current coordinates are %s",
271+
expectedFileName, file, coord));
272+
}
273+
index = index + baseVersion.length();
274+
int snapshotVersionStart = index;
275+
276+
// Skip over the YYYYMMDD part.
277+
if (pathSubstring.charAt(index + 9) != '.') {
278+
throw new IllegalArgumentException(
279+
String.format(
280+
"Expected artifact (%s) not found in path (%s). Current coordinates are %s",
281+
expectedFileName, file, coord));
282+
}
283+
index = index + 9;
284+
285+
// Skip over the HHMMSS part.
286+
if (pathSubstring.charAt(index + 7) != '-') {
287+
throw new IllegalArgumentException(
288+
String.format(
289+
"Expected artifact (%s) not found in path (%s). Current coordinates are %s",
290+
expectedFileName, file, coord));
291+
}
292+
index = index + 7;
293+
294+
// Skip over the variable-length build number.
295+
index = pathSubstring.indexOf(".", index + 1);
296+
297+
remainder = pathSubstring.substring(index);
298+
dirVersion = version;
299+
version = baseVersion + pathSubstring.substring(snapshotVersionStart, index);
300+
} else {
301+
// Now we know the version, we can calculate the expected file name. For now, ignore
302+
// the fact that there may be a classifier. We're going to derive that if necessary.
303+
String expectedFileName = coord.getArtifactId() + "-" + version;
304+
305+
index = pathSubstring.indexOf(expectedFileName);
306+
if (index == -1) {
307+
throw new IllegalArgumentException(
308+
String.format(
309+
"Expected file name (%s) not found in path (%s). Current coordinates are %s",
310+
expectedFileName, file, coord));
311+
}
312+
313+
remainder = pathSubstring.substring(expectedFileName.length());
314+
dirVersion = null;
269315
}
270316

271317
String classifier = "";
272318
String extension = "";
273-
String remainder = pathSubstring.substring(expectedFileName.length());
274319

275320
if (remainder.isEmpty()) {
276321
throw new IllegalArgumentException(
@@ -303,7 +348,7 @@ private Map<Coordinates, Coordinates> deriveCoordinateMappings(Map<String, Objec
303348
toReturn.put(
304349
coord,
305350
new Coordinates(
306-
coord.getGroupId(), coord.getArtifactId(), extension, classifier, version));
351+
coord.getGroupId(), coord.getArtifactId(), extension, classifier, version, dirVersion));
307352
}
308353

309354
return toReturn;

private/tools/java/com/github/bazelbuild/rules_jvm_external/coursier/NebulaFormat.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ public Map<String, Object> render(Set<DependencyInfo> infos, Map<String, Object>
5757
Map<String, Object> artifactValue =
5858
artifacts.computeIfAbsent(shortKey, k -> new TreeMap<>());
5959
artifactValue.put("version", coords.getVersion());
60+
String dirVersion = coords.getDirVersion();
61+
if (dirVersion != null) {
62+
artifactValue.put("dirVersion", coords.getDirVersion());
63+
}
6064

6165
String classifier = coords.getClassifier();
6266
if (classifier == null || classifier.isEmpty()) {
123 KB
Binary file not shown.

0 commit comments

Comments
 (0)