Create proper relative upload path for maven artifacts, check for sanity of groupId and relative path #11329

This commit is contained in:
Dawid Weiss 2022-11-22 08:16:16 +01:00
parent fa0031bde5
commit af28352d31
1 changed files with 29 additions and 5 deletions

View File

@ -212,11 +212,11 @@ public class StageArtifacts {
.text(); .text();
} }
public void uploadArtifact(String stagingRepoId, Path path) throws IOException { public void uploadArtifact(String stagingRepoId, Path path, String relativePath) throws IOException {
sendPost("/service/local/staging/deployByRepositoryId/" sendPost("/service/local/staging/deployByRepositoryId/"
+ URLEncoder.encode(stagingRepoId, StandardCharsets.UTF_8) + URLEncoder.encode(stagingRepoId, StandardCharsets.UTF_8)
+ "/" + "/"
+ URLEncoder.encode(path.getFileName().toString(), StandardCharsets.UTF_8), + relativePath,
"application/octet-stream", "application/octet-stream",
HttpURLConnection.HTTP_CREATED, HttpURLConnection.HTTP_CREATED,
Files.readAllBytes(path)); Files.readAllBytes(path));
@ -248,7 +248,7 @@ public class StageArtifacts {
HttpResponse<String> response = client.send(req, bodyHandler); HttpResponse<String> response = client.send(req, bodyHandler);
if (response.statusCode() != expectedStatus) { if (response.statusCode() != expectedStatus) {
throw new IOException("Unexpected HTTP error returned: " + response.statusCode() + ", response body: " throw new IOException("Unexpected HTTP error returned: " + response.statusCode() + ", response body: "
+ response.body()); + response.body());
} }
return response.body(); return response.body();
} catch (InterruptedException e) { } catch (InterruptedException e) {
@ -373,6 +373,24 @@ public class StageArtifacts {
.findFirst() .findFirst()
.orElseThrow()); .orElseThrow());
// Sanity check for directory structure - all files should have the groupId folder prefix.
{
String expectedPrefix = pomInfo.groupId.replace('.', '/');
for (Path path : artifacts) {
Path relative = params.mavenDir.relativize(path);
List<String> urlSegments = new ArrayList<>();
for (Path segment : relative) {
urlSegments.add(URLEncoder.encode(segment.toString(), StandardCharsets.UTF_8));
}
String relativeUrl = String.join("/", urlSegments);
if (!relativeUrl.startsWith(expectedPrefix)) {
throw new RuntimeException("Maven folder structure does not match the expected groupId, "
+ "expected prefix: " + expectedPrefix + ", artifact: " + relativeUrl);
}
}
}
System.out.println("Requesting profile ID for artifact: " System.out.println("Requesting profile ID for artifact: "
+ pomInfo.groupId + ":" + pomInfo.artifactId + ":" + pomInfo.version); + pomInfo.groupId + ":" + pomInfo.artifactId + ":" + pomInfo.version);
String profileId = nexus.requestProfileId(pomInfo); String profileId = nexus.requestProfileId(pomInfo);
@ -387,8 +405,14 @@ public class StageArtifacts {
System.out.printf("Uploading %s artifact(s).%n", artifacts.size()); System.out.printf("Uploading %s artifact(s).%n", artifacts.size());
for (Path path : artifacts) { for (Path path : artifacts) {
System.out.println(" => " + params.mavenDir.relativize(path)); Path relative = params.mavenDir.relativize(path);
nexus.uploadArtifact(stagingRepoId, path); List<String> urlSegments = new ArrayList<>();
for (Path segment : relative) {
urlSegments.add(URLEncoder.encode(segment.toString(), StandardCharsets.UTF_8));
}
String relativeUrl = String.join("/", urlSegments);
System.out.println(" => " + relative + ": " + relativeUrl);
nexus.uploadArtifact(stagingRepoId, path, relativeUrl);
} }
System.out.println("Closing the staging repository."); System.out.println("Closing the staging repository.");