Skip to content

Commit 0952f3f

Browse files
authored
feat: add task to wait until all the artifacts are available on MC (#360)
* feat: add task to wait until all the artifacts in the current release are available on MC * chore: permit test run with empty include/exclude tags
1 parent ba7cafc commit 0952f3f

File tree

5 files changed

+114
-6
lines changed

5 files changed

+114
-6
lines changed

plugins/edc-build/src/main/java/org/eclipse/edc/plugins/edcbuild/EdcBuildPlugin.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import static org.eclipse.edc.plugins.edcbuild.conventions.Conventions.signing;
3737
import static org.eclipse.edc.plugins.edcbuild.conventions.Conventions.swagger;
3838
import static org.eclipse.edc.plugins.edcbuild.conventions.Conventions.tests;
39+
import static org.eclipse.edc.plugins.edcbuild.conventions.Conventions.waitForPublishedArtifacts;
3940

4041
/**
4142
* Adds (opinionated) conventions (=configuration) for various plugins.
@@ -77,7 +78,8 @@ public void apply(Project target) {
7778
tests(),
7879
jar(),
7980
swagger(),
80-
printClasspath()
81+
printClasspath(),
82+
waitForPublishedArtifacts()
8183
).forEach(c -> c.apply(project));
8284
});
8385

plugins/edc-build/src/main/java/org/eclipse/edc/plugins/edcbuild/conventions/Conventions.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,8 @@ public static EdcConvention mavenPublication() {
8282
public static EdcConvention printClasspath() {
8383
return new PrintClasspathConvention();
8484
}
85+
86+
public static EdcConvention waitForPublishedArtifacts() {
87+
return new WaitForPublishedArtifactsConvention();
88+
}
8589
}

plugins/edc-build/src/main/java/org/eclipse/edc/plugins/edcbuild/conventions/TestConvention.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
import org.gradle.api.tasks.testing.logging.TestExceptionFormat;
2020
import org.jetbrains.annotations.NotNull;
2121

22-
import static java.util.Optional.ofNullable;
23-
2422
/**
2523
* Configures the use of JUnit, the tagging mechanism and also configures the test logging
2624
*/
@@ -60,9 +58,11 @@ private static void determineJunitPlatform(Test testTask) {
6058

6159
@NotNull
6260
private static String[] getTags(String tagsSeparatedByComma) {
63-
return ofNullable(tagsSeparatedByComma)
64-
.map(prop -> prop.split(","))
65-
.orElse(new String[0]);
61+
if (tagsSeparatedByComma == null || tagsSeparatedByComma.isBlank()) {
62+
return new String[0];
63+
}
64+
65+
return tagsSeparatedByComma.split(",");
6666
}
6767

6868
@Override
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright (c) 2025 Think-it GmbH
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Apache License, Version 2.0 which is available at
6+
* https://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* SPDX-License-Identifier: Apache-2.0
9+
*
10+
* Contributors:
11+
* Think-it GmbH - initial API and implementation
12+
*
13+
*/
14+
15+
package org.eclipse.edc.plugins.edcbuild.conventions;
16+
17+
import org.eclipse.edc.plugins.edcbuild.tasks.WaitForPublishedArtifacts;
18+
import org.gradle.api.Project;
19+
20+
public class WaitForPublishedArtifactsConvention implements EdcConvention {
21+
@Override
22+
public void apply(Project target) {
23+
target.getTasks().register("waitForPublishedArtifacts", WaitForPublishedArtifacts.class);
24+
}
25+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright (c) 2025 Think-it GmbH
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Apache License, Version 2.0 which is available at
6+
* https://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* SPDX-License-Identifier: Apache-2.0
9+
*
10+
* Contributors:
11+
* Think-it GmbH - initial API and implementation
12+
*
13+
*/
14+
15+
package org.eclipse.edc.plugins.edcbuild.tasks;
16+
17+
import org.gradle.api.DefaultTask;
18+
import org.gradle.api.artifacts.dsl.RepositoryHandler;
19+
import org.gradle.api.publish.PublishingExtension;
20+
import org.gradle.api.publish.maven.MavenPublication;
21+
import org.gradle.api.tasks.TaskAction;
22+
23+
import java.net.URL;
24+
import java.time.LocalDateTime;
25+
26+
import static java.lang.Thread.sleep;
27+
import static org.eclipse.edc.plugins.edcbuild.conventions.ConventionFunctions.requireExtension;
28+
29+
/**
30+
* Task for root project that looks up for published artifacts, waiting if they are not available.
31+
* After a certain timeout the task fails
32+
*/
33+
public class WaitForPublishedArtifacts extends DefaultTask {
34+
35+
private static final int TASK_TIMEOUT_MINUTES = 60;
36+
private static final int RETRY_WAIT_SECONDS = 30;
37+
38+
@TaskAction
39+
public void waitForPublishedArtifacts() {
40+
requireExtension(getProject(), PublishingExtension.class)
41+
.getPublications().stream()
42+
.map(MavenPublication.class::cast)
43+
.forEach(this::lookUpForPublication);
44+
}
45+
46+
private void lookUpForPublication(MavenPublication publication) {
47+
if (publication.getVersion().endsWith("-SNAPSHOT")) {
48+
throw new IllegalStateException("This task can only be executed on proper releases, not on snapshots");
49+
}
50+
var artifact = publication.getName() + " " + publication.getGroupId() + ":" + publication.getArtifactId() + ":" + publication.getVersion();
51+
var repo = RepositoryHandler.MAVEN_CENTRAL_URL;
52+
53+
var timeout = LocalDateTime.now().plusMinutes(TASK_TIMEOUT_MINUTES);
54+
var found = false;
55+
while (!found && timeout.isAfter(LocalDateTime.now())) {
56+
try {
57+
var url = repo + publication.getGroupId().replace('.', '/') +
58+
"/" + publication.getArtifactId() + "/" + publication.getVersion() +
59+
"/" + publication.getArtifactId() + "-" + publication.getVersion() +
60+
".pom";
61+
new URL(url).openStream().close();
62+
found = true;
63+
} catch (Throwable e) {
64+
getLogger().warn("Artifact {} is NOT available on maven central, will try again in {} seconds", artifact, RETRY_WAIT_SECONDS);
65+
try {
66+
sleep(RETRY_WAIT_SECONDS * 1000);
67+
} catch (InterruptedException ex) {
68+
throw new RuntimeException(ex);
69+
}
70+
}
71+
}
72+
73+
if (!found) {
74+
throw new IllegalStateException("Artifact %s has not been found maven central after %s minutes".formatted(artifact, TASK_TIMEOUT_MINUTES));
75+
}
76+
}
77+
}

0 commit comments

Comments
 (0)