Skip to content

Commit 3ced763

Browse files
authored
Fix unit test classpath when testing shadow jar projects (#74783) (#74791)
1 parent ca7999d commit 3ced763

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/ElasticsearchTestBasePlugin.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.gradle.api.Task;
2424
import org.gradle.api.artifacts.Configuration;
2525
import org.gradle.api.file.FileCollection;
26+
import org.gradle.api.plugins.JavaPlugin;
2627
import org.gradle.api.tasks.SourceSet;
2728
import org.gradle.api.tasks.SourceSetContainer;
2829
import org.gradle.api.tasks.testing.Test;
@@ -194,17 +195,17 @@ public void execute(Task t) {
194195
* compiled class output and dependency jars. This better emulates the runtime environment of consumers.
195196
*/
196197
project.getPluginManager().withPlugin("com.github.johnrengelman.shadow", p -> {
197-
// Remove output class files and any other dependencies from the test classpath, since the shadow JAR includes these
198-
FileCollection mainRuntime = project.getExtensions()
199-
.getByType(SourceSetContainer.class)
200-
.getByName(SourceSet.MAIN_SOURCE_SET_NAME)
201-
.getRuntimeClasspath();
202-
// Add any "shadow" dependencies. These are dependencies that are *not* bundled into the shadow JAR
203-
Configuration shadowConfig = project.getConfigurations().getByName(ShadowBasePlugin.getCONFIGURATION_NAME());
204-
// Add the shadow JAR artifact itself
205-
FileCollection shadowJar = project.files(project.getTasks().named("shadowJar"));
206-
207-
test.setClasspath(test.getClasspath().minus(mainRuntime).plus(shadowConfig).plus(shadowJar));
198+
if (test.getName().equals(JavaPlugin.TEST_TASK_NAME)) {
199+
// Remove output class files and any other dependencies from the test classpath, since the shadow JAR includes these
200+
SourceSetContainer sourceSets = project.getExtensions().getByType(SourceSetContainer.class);
201+
FileCollection mainRuntime = sourceSets.getByName(SourceSet.MAIN_SOURCE_SET_NAME).getRuntimeClasspath();
202+
// Add any "shadow" dependencies. These are dependencies that are *not* bundled into the shadow JAR
203+
Configuration shadowConfig = project.getConfigurations().getByName(ShadowBasePlugin.getCONFIGURATION_NAME());
204+
// Add the shadow JAR artifact itself
205+
FileCollection shadowJar = project.files(project.getTasks().named("shadowJar"));
206+
FileCollection testRuntime = sourceSets.getByName(SourceSet.TEST_SOURCE_SET_NAME).getRuntimeClasspath();
207+
test.setClasspath(testRuntime.minus(mainRuntime).plus(shadowConfig).plus(shadowJar));
208+
}
208209
});
209210
});
210211
}

0 commit comments

Comments
 (0)