From 83ecefb75bb6fa0e719893b40c00538c0a32d1f9 Mon Sep 17 00:00:00 2001 From: Ryan Ernst Date: Wed, 20 Mar 2024 10:08:51 -0700 Subject: [PATCH 1/7] Add test sourceset support to MRJAR plugin Mrjar plugin supports main sourcesets automatically configured with newer java versions. This commit adds support for test sourcesets as well. The key difference between main and test is that test sourcesets also extend their main counterparts. --- .../gradle/internal/MrjarPlugin.java | 93 ++++++++++++------- 1 file changed, 59 insertions(+), 34 deletions(-) diff --git a/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/MrjarPlugin.java b/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/MrjarPlugin.java index adf84b63d8689..93b21aa69ca58 100644 --- a/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/MrjarPlugin.java +++ b/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/MrjarPlugin.java @@ -46,7 +46,8 @@ public class MrjarPlugin implements Plugin { - private static final Pattern MRJAR_SOURCESET_PATTERN = Pattern.compile("main(\\d{2})"); + private static final Pattern MRJAR_MAIN_SOURCESET_PATTERN = Pattern.compile("main(\\d{2})"); + private static final Pattern MRJAR_TEST_SOURCESET_PATTERN = Pattern.compile("test(\\d{2})"); private final JavaToolchainService javaToolchains; @@ -60,46 +61,39 @@ public void apply(Project project) { project.getPluginManager().apply(ElasticsearchJavaBasePlugin.class); var javaExtension = project.getExtensions().getByType(JavaPluginExtension.class); - var srcDir = project.getProjectDir().toPath().resolve("src"); - List mainVersions = new ArrayList<>(); - try (var subdirStream = Files.list(srcDir)) { - for (Path sourceset : subdirStream.toList()) { - assert Files.isDirectory(sourceset); - String sourcesetName = sourceset.getFileName().toString(); - Matcher sourcesetMatcher = MRJAR_SOURCESET_PATTERN.matcher(sourcesetName); - if (sourcesetMatcher.matches()) { - mainVersions.add(Integer.parseInt(sourcesetMatcher.group(1))); - } - } - } catch (IOException e) { - throw new UncheckedIOException(e); - } - - Collections.sort(mainVersions); - List parentSourceSets = new ArrayList<>(); - parentSourceSets.add(SourceSet.MAIN_SOURCE_SET_NAME); + List mainVersions = findSourceVersions(project, MRJAR_MAIN_SOURCESET_PATTERN); + List mainSourceSets = new ArrayList<>(); + mainSourceSets.add(SourceSet.MAIN_SOURCE_SET_NAME); for (int javaVersion : mainVersions) { - String sourcesetName = "main" + javaVersion; - addMrjarSourceset(project, javaExtension, sourcesetName, parentSourceSets, javaVersion); - parentSourceSets.add(sourcesetName); + String sourcesetName = SourceSet.MAIN_SOURCE_SET_NAME + javaVersion; + addSourceset(project, javaExtension, sourcesetName, mainSourceSets, javaVersion); + mainSourceSets.add(sourcesetName); } - } - private void addMrjarSourceset( - Project project, - JavaPluginExtension javaExtension, - String sourcesetName, - List parentSourceSets, - int javaVersion - ) { - SourceSet sourceSet = javaExtension.getSourceSets().maybeCreate(sourcesetName); - for (String parentSourceSetName : parentSourceSets) { - GradleUtils.extendSourceSet(project, parentSourceSetName, sourcesetName); + List testVersions = findSourceVersions(project, MRJAR_TEST_SOURCESET_PATTERN); + List testSourceSets = new ArrayList<>(); + testSourceSets.add(SourceSet.MAIN_SOURCE_SET_NAME); + testSourceSets.add(SourceSet.TEST_SOURCE_SET_NAME); + for (int javaVersion : testVersions) { + String sourcesetName = SourceSet.TEST_SOURCE_SET_NAME + javaVersion; + if (mainVersions.contains(javaVersion)) { + // tests extend both their parent test source sets, and all of main sourcesets up to that version + testSourceSets.add(SourceSet.TEST_SOURCE_SET_NAME + javaVersion); + } + addSourceset(project, javaExtension, sourcesetName, testSourceSets, javaVersion); + testSourceSets.add(sourcesetName); } + configureMrjar(project, javaExtension, mainVersions, "main"); + } + + private void configureMrjar(Project project, JavaPluginExtension javaExtension, List versions, String baseSourcesetName) { var jarTask = project.getTasks().withType(Jar.class).named(JavaPlugin.JAR_TASK_NAME); jarTask.configure(task -> { - task.into("META-INF/versions/" + javaVersion, copySpec -> copySpec.from(sourceSet.getOutput())); + for (var version : versions) { + SourceSet sourceSet = javaExtension.getSourceSets().maybeCreate(baseSourcesetName + version); + task.into("META-INF/versions/" + version, copySpec -> copySpec.from(sourceSet.getOutput())); + } task.manifest(manifest -> { manifest.attributes(Map.of("Multi-Release", "true")); }); }); @@ -111,6 +105,17 @@ private void addMrjarSourceset( FileCollection testRuntime = sourceSets.getByName(SourceSet.TEST_SOURCE_SET_NAME).getRuntimeClasspath(); testTask.setClasspath(testRuntime.minus(mainRuntime).plus(project.files(jarTask))); }); + } + + private void addSourceset(Project project, + JavaPluginExtension javaExtension, + String sourcesetName, + List parentSourceSets, + int javaVersion) { + SourceSet sourceSet = javaExtension.getSourceSets().maybeCreate(sourcesetName); + for (String parentSourceSetName : parentSourceSets) { + GradleUtils.extendSourceSet(project, parentSourceSetName, sourcesetName); + } project.getTasks().withType(JavaCompile.class).named(sourceSet.getCompileJavaTaskName()).configure(compileTask -> { compileTask.getJavaCompiler() @@ -134,6 +139,26 @@ private void addMrjarSourceset( }); } + private static List findSourceVersions(Project project, Pattern sourcesetPattern) { + var srcDir = project.getProjectDir().toPath().resolve("src"); + List versions = new ArrayList<>(); + try (var subdirStream = Files.list(srcDir)) { + for (Path sourceset : subdirStream.toList()) { + assert Files.isDirectory(sourceset); + String sourcesetName = sourceset.getFileName().toString(); + Matcher sourcesetMatcher = sourcesetPattern.matcher(sourcesetName); + if (sourcesetMatcher.matches()) { + versions.add(Integer.parseInt(sourcesetMatcher.group(1))); + } + } + } catch (IOException e) { + throw new UncheckedIOException(e); + } + + Collections.sort(versions); + return versions; + } + private static void stripPreviewFromFiles(Path compileDir) { try (Stream fileStream = Files.walk(compileDir)) { fileStream.filter(p -> p.toString().endsWith(".class")).forEach(MrjarPlugin::maybeStripPreview); From a1a9881f58068bf524e0c34f4200cd56d1cbaf60 Mon Sep 17 00:00:00 2001 From: Ryan Ernst Date: Wed, 20 Mar 2024 10:11:46 -0700 Subject: [PATCH 2/7] spotless --- .../elasticsearch/gradle/internal/MrjarPlugin.java | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/MrjarPlugin.java b/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/MrjarPlugin.java index 93b21aa69ca58..b80bce20eff8f 100644 --- a/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/MrjarPlugin.java +++ b/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/MrjarPlugin.java @@ -107,11 +107,13 @@ private void configureMrjar(Project project, JavaPluginExtension javaExtension, }); } - private void addSourceset(Project project, - JavaPluginExtension javaExtension, - String sourcesetName, - List parentSourceSets, - int javaVersion) { + private void addSourceset( + Project project, + JavaPluginExtension javaExtension, + String sourcesetName, + List parentSourceSets, + int javaVersion + ) { SourceSet sourceSet = javaExtension.getSourceSets().maybeCreate(sourcesetName); for (String parentSourceSetName : parentSourceSets) { GradleUtils.extendSourceSet(project, parentSourceSetName, sourcesetName); From 1b2d0558ff5337dffff37867bc226557debac9d5 Mon Sep 17 00:00:00 2001 From: Ryan Ernst Date: Wed, 20 Mar 2024 11:04:01 -0700 Subject: [PATCH 3/7] address feedback --- .../gradle/internal/MrjarPlugin.java | 73 +++++++++++-------- 1 file changed, 42 insertions(+), 31 deletions(-) diff --git a/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/MrjarPlugin.java b/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/MrjarPlugin.java index b80bce20eff8f..e73311bf409f2 100644 --- a/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/MrjarPlugin.java +++ b/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/MrjarPlugin.java @@ -46,8 +46,7 @@ public class MrjarPlugin implements Plugin { - private static final Pattern MRJAR_MAIN_SOURCESET_PATTERN = Pattern.compile("main(\\d{2})"); - private static final Pattern MRJAR_TEST_SOURCESET_PATTERN = Pattern.compile("test(\\d{2})"); + private static final Pattern MRJAR_SOURCESET_PATTERN = Pattern.compile("main(\\d{2})"); private final JavaToolchainService javaToolchains; @@ -61,37 +60,31 @@ public void apply(Project project) { project.getPluginManager().apply(ElasticsearchJavaBasePlugin.class); var javaExtension = project.getExtensions().getByType(JavaPluginExtension.class); - List mainVersions = findSourceVersions(project, MRJAR_MAIN_SOURCESET_PATTERN); + List mainVersions = findSourceVersions(project); List mainSourceSets = new ArrayList<>(); mainSourceSets.add(SourceSet.MAIN_SOURCE_SET_NAME); - for (int javaVersion : mainVersions) { - String sourcesetName = SourceSet.MAIN_SOURCE_SET_NAME + javaVersion; - addSourceset(project, javaExtension, sourcesetName, mainSourceSets, javaVersion); - mainSourceSets.add(sourcesetName); - } - - List testVersions = findSourceVersions(project, MRJAR_TEST_SOURCESET_PATTERN); - List testSourceSets = new ArrayList<>(); - testSourceSets.add(SourceSet.MAIN_SOURCE_SET_NAME); + List testSourceSets = new ArrayList<>(mainSourceSets); testSourceSets.add(SourceSet.TEST_SOURCE_SET_NAME); - for (int javaVersion : testVersions) { - String sourcesetName = SourceSet.TEST_SOURCE_SET_NAME + javaVersion; - if (mainVersions.contains(javaVersion)) { - // tests extend both their parent test source sets, and all of main sourcesets up to that version - testSourceSets.add(SourceSet.TEST_SOURCE_SET_NAME + javaVersion); - } - addSourceset(project, javaExtension, sourcesetName, testSourceSets, javaVersion); - testSourceSets.add(sourcesetName); + for (int javaVersion : mainVersions) { + String mainSourceSetName = SourceSet.MAIN_SOURCE_SET_NAME + javaVersion; + addSourceSet(project, javaExtension, mainSourceSetName, mainSourceSets, javaVersion); + mainSourceSets.add(mainSourceSetName); + testSourceSets.add(SourceSet.MAIN_SOURCE_SET_NAME + javaVersion); + + String testSourceSetName = SourceSet.TEST_SOURCE_SET_NAME + javaVersion; + SourceSet testSourceSet = addSourceSet(project, javaExtension, testSourceSetName, testSourceSets, javaVersion); + testSourceSets.add(testSourceSetName); + createTestTask(project, testSourceSet, javaVersion); } configureMrjar(project, javaExtension, mainVersions, "main"); } - private void configureMrjar(Project project, JavaPluginExtension javaExtension, List versions, String baseSourcesetName) { + private void configureMrjar(Project project, JavaPluginExtension javaExtension, List versions, String baseSourceSetName) { var jarTask = project.getTasks().withType(Jar.class).named(JavaPlugin.JAR_TASK_NAME); jarTask.configure(task -> { for (var version : versions) { - SourceSet sourceSet = javaExtension.getSourceSets().maybeCreate(baseSourcesetName + version); + SourceSet sourceSet = javaExtension.getSourceSets().getByName(baseSourceSetName + version); task.into("META-INF/versions/" + version, copySpec -> copySpec.from(sourceSet.getOutput())); } task.manifest(manifest -> { manifest.attributes(Map.of("Multi-Release", "true")); }); @@ -107,16 +100,16 @@ private void configureMrjar(Project project, JavaPluginExtension javaExtension, }); } - private void addSourceset( + private SourceSet addSourceSet( Project project, JavaPluginExtension javaExtension, - String sourcesetName, + String sourceSetName, List parentSourceSets, int javaVersion ) { - SourceSet sourceSet = javaExtension.getSourceSets().maybeCreate(sourcesetName); + SourceSet sourceSet = javaExtension.getSourceSets().maybeCreate(sourceSetName); for (String parentSourceSetName : parentSourceSets) { - GradleUtils.extendSourceSet(project, parentSourceSetName, sourcesetName); + GradleUtils.extendSourceSet(project, parentSourceSetName, sourceSetName); } project.getTasks().withType(JavaCompile.class).named(sourceSet.getCompileJavaTaskName()).configure(compileTask -> { @@ -139,16 +132,34 @@ private void addSourceset( project.getTasks().withType(CheckForbiddenApisTask.class).named(forbiddenApisTaskName).configure(forbiddenApisTask -> { forbiddenApisTask.setIgnoreMissingClasses(true); }); + + return sourceSet; + } + + private void createTestTask(Project project, SourceSet sourceSet, int javaVersion) { + var jarTask = project.getTasks().withType(Jar.class).named(JavaPlugin.JAR_TASK_NAME); + project.getTasks().register(JavaPlugin.TEST_TASK_NAME + javaVersion, Test.class).configure(testTask -> { + testTask.dependsOn(jarTask); + + SourceSetContainer sourceSets = GradleUtils.getJavaSourceSets(project); + FileCollection mainRuntime = sourceSets.getByName(SourceSet.MAIN_SOURCE_SET_NAME + javaVersion).getOutput(); + FileCollection testRuntime = sourceSet.getRuntimeClasspath(); + testTask.setClasspath(testRuntime.minus(mainRuntime).plus(project.files(jarTask))); + + testTask.getJavaLauncher().set( + javaToolchains.launcherFor(spec -> { spec.getLanguageVersion().set(JavaLanguageVersion.of(javaVersion)); }) + ); + }); } - private static List findSourceVersions(Project project, Pattern sourcesetPattern) { + private static List findSourceVersions(Project project) { var srcDir = project.getProjectDir().toPath().resolve("src"); List versions = new ArrayList<>(); try (var subdirStream = Files.list(srcDir)) { - for (Path sourceset : subdirStream.toList()) { - assert Files.isDirectory(sourceset); - String sourcesetName = sourceset.getFileName().toString(); - Matcher sourcesetMatcher = sourcesetPattern.matcher(sourcesetName); + for (Path sourceSetPath : subdirStream.toList()) { + assert Files.isDirectory(sourceSetPath); + String sourcesetName = sourceSetPath.getFileName().toString(); + Matcher sourcesetMatcher = MRJAR_SOURCESET_PATTERN.matcher(sourcesetName); if (sourcesetMatcher.matches()) { versions.add(Integer.parseInt(sourcesetMatcher.group(1))); } From 077ecbe9cb1d2c7a04f61986df2d70d19a1bb8f4 Mon Sep 17 00:00:00 2001 From: Ryan Ernst Date: Wed, 20 Mar 2024 13:54:10 -0700 Subject: [PATCH 4/7] spotless --- .../java/org/elasticsearch/gradle/internal/MrjarPlugin.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/MrjarPlugin.java b/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/MrjarPlugin.java index e73311bf409f2..993d39f11b179 100644 --- a/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/MrjarPlugin.java +++ b/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/MrjarPlugin.java @@ -146,9 +146,8 @@ private void createTestTask(Project project, SourceSet sourceSet, int javaVersio FileCollection testRuntime = sourceSet.getRuntimeClasspath(); testTask.setClasspath(testRuntime.minus(mainRuntime).plus(project.files(jarTask))); - testTask.getJavaLauncher().set( - javaToolchains.launcherFor(spec -> { spec.getLanguageVersion().set(JavaLanguageVersion.of(javaVersion)); }) - ); + testTask.getJavaLauncher() + .set(javaToolchains.launcherFor(spec -> { spec.getLanguageVersion().set(JavaLanguageVersion.of(javaVersion)); })); }); } From e3e2ac04b90a7146f8df7f5382f5fdb4df121835 Mon Sep 17 00:00:00 2001 From: Ryan Ernst Date: Wed, 20 Mar 2024 15:36:14 -0700 Subject: [PATCH 5/7] round 2 --- .../gradle/internal/MrjarPlugin.java | 39 ++++++++++++------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/MrjarPlugin.java b/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/MrjarPlugin.java index 993d39f11b179..89f4616572955 100644 --- a/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/MrjarPlugin.java +++ b/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/MrjarPlugin.java @@ -17,6 +17,7 @@ import org.gradle.api.plugins.JavaPluginExtension; import org.gradle.api.tasks.SourceSet; import org.gradle.api.tasks.SourceSetContainer; +import org.gradle.api.tasks.TaskProvider; import org.gradle.api.tasks.compile.CompileOptions; import org.gradle.api.tasks.compile.JavaCompile; import org.gradle.api.tasks.testing.Test; @@ -67,26 +68,23 @@ public void apply(Project project) { testSourceSets.add(SourceSet.TEST_SOURCE_SET_NAME); for (int javaVersion : mainVersions) { String mainSourceSetName = SourceSet.MAIN_SOURCE_SET_NAME + javaVersion; - addSourceSet(project, javaExtension, mainSourceSetName, mainSourceSets, javaVersion); + SourceSet mainSourceSet = addSourceSet(project, javaExtension, mainSourceSetName, mainSourceSets, javaVersion); + configureSourceSetInJar(project, mainSourceSet, javaVersion); mainSourceSets.add(mainSourceSetName); testSourceSets.add(SourceSet.MAIN_SOURCE_SET_NAME + javaVersion); String testSourceSetName = SourceSet.TEST_SOURCE_SET_NAME + javaVersion; SourceSet testSourceSet = addSourceSet(project, javaExtension, testSourceSetName, testSourceSets, javaVersion); testSourceSets.add(testSourceSetName); - createTestTask(project, testSourceSet, javaVersion); + createTestTask(project, testSourceSet, javaVersion, mainSourceSets); } - configureMrjar(project, javaExtension, mainVersions, "main"); + configureMrjar(project); } - private void configureMrjar(Project project, JavaPluginExtension javaExtension, List versions, String baseSourceSetName) { + private void configureMrjar(Project project) { var jarTask = project.getTasks().withType(Jar.class).named(JavaPlugin.JAR_TASK_NAME); jarTask.configure(task -> { - for (var version : versions) { - SourceSet sourceSet = javaExtension.getSourceSets().getByName(baseSourceSetName + version); - task.into("META-INF/versions/" + version, copySpec -> copySpec.from(sourceSet.getOutput())); - } task.manifest(manifest -> { manifest.attributes(Map.of("Multi-Release", "true")); }); }); @@ -136,18 +134,33 @@ private SourceSet addSourceSet( return sourceSet; } - private void createTestTask(Project project, SourceSet sourceSet, int javaVersion) { + private void configureSourceSetInJar(Project project, SourceSet sourceSet, int javaVersion) { + var jarTask = project.getTasks().withType(Jar.class).named(JavaPlugin.JAR_TASK_NAME); + jarTask.configure(task -> { + task.into("META-INF/versions/" + javaVersion, copySpec -> copySpec.from(sourceSet.getOutput())); + }); + } + + private void createTestTask(Project project, SourceSet sourceSet, int javaVersion, List mainSourceSets) { var jarTask = project.getTasks().withType(Jar.class).named(JavaPlugin.JAR_TASK_NAME); - project.getTasks().register(JavaPlugin.TEST_TASK_NAME + javaVersion, Test.class).configure(testTask -> { + var testTaskProvider = project.getTasks().register(JavaPlugin.TEST_TASK_NAME + javaVersion, Test.class); + testTaskProvider.configure(testTask -> { testTask.dependsOn(jarTask); SourceSetContainer sourceSets = GradleUtils.getJavaSourceSets(project); - FileCollection mainRuntime = sourceSets.getByName(SourceSet.MAIN_SOURCE_SET_NAME + javaVersion).getOutput(); FileCollection testRuntime = sourceSet.getRuntimeClasspath(); - testTask.setClasspath(testRuntime.minus(mainRuntime).plus(project.files(jarTask))); + for (String mainSourceSetName : mainSourceSets) { + FileCollection mainRuntime = sourceSets.getByName(mainSourceSetName).getOutput(); + testRuntime = testRuntime.minus(mainRuntime); + } + testTask.setClasspath(testRuntime.plus(project.files(jarTask))); testTask.getJavaLauncher() - .set(javaToolchains.launcherFor(spec -> { spec.getLanguageVersion().set(JavaLanguageVersion.of(javaVersion)); })); + .set(javaToolchains.launcherFor(spec -> spec.getLanguageVersion().set(JavaLanguageVersion.of(javaVersion)))); + }); + + project.getTasks().named("check").configure(checkTask -> { + checkTask.dependsOn(testTaskProvider); }); } From 96a852c990720283519cfd61445d0a79132778f2 Mon Sep 17 00:00:00 2001 From: Ryan Ernst Date: Wed, 20 Mar 2024 15:37:05 -0700 Subject: [PATCH 6/7] spotless --- .../elasticsearch/gradle/internal/MrjarPlugin.java | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/MrjarPlugin.java b/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/MrjarPlugin.java index 89f4616572955..e4a83534459bf 100644 --- a/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/MrjarPlugin.java +++ b/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/MrjarPlugin.java @@ -17,7 +17,6 @@ import org.gradle.api.plugins.JavaPluginExtension; import org.gradle.api.tasks.SourceSet; import org.gradle.api.tasks.SourceSetContainer; -import org.gradle.api.tasks.TaskProvider; import org.gradle.api.tasks.compile.CompileOptions; import org.gradle.api.tasks.compile.JavaCompile; import org.gradle.api.tasks.testing.Test; @@ -84,9 +83,7 @@ public void apply(Project project) { private void configureMrjar(Project project) { var jarTask = project.getTasks().withType(Jar.class).named(JavaPlugin.JAR_TASK_NAME); - jarTask.configure(task -> { - task.manifest(manifest -> { manifest.attributes(Map.of("Multi-Release", "true")); }); - }); + jarTask.configure(task -> { task.manifest(manifest -> { manifest.attributes(Map.of("Multi-Release", "true")); }); }); project.getTasks().withType(Test.class).named(JavaPlugin.TEST_TASK_NAME).configure(testTask -> { testTask.dependsOn(jarTask); @@ -136,9 +133,7 @@ private SourceSet addSourceSet( private void configureSourceSetInJar(Project project, SourceSet sourceSet, int javaVersion) { var jarTask = project.getTasks().withType(Jar.class).named(JavaPlugin.JAR_TASK_NAME); - jarTask.configure(task -> { - task.into("META-INF/versions/" + javaVersion, copySpec -> copySpec.from(sourceSet.getOutput())); - }); + jarTask.configure(task -> { task.into("META-INF/versions/" + javaVersion, copySpec -> copySpec.from(sourceSet.getOutput())); }); } private void createTestTask(Project project, SourceSet sourceSet, int javaVersion, List mainSourceSets) { @@ -159,9 +154,7 @@ private void createTestTask(Project project, SourceSet sourceSet, int javaVersio .set(javaToolchains.launcherFor(spec -> spec.getLanguageVersion().set(JavaLanguageVersion.of(javaVersion)))); }); - project.getTasks().named("check").configure(checkTask -> { - checkTask.dependsOn(testTaskProvider); - }); + project.getTasks().named("check").configure(checkTask -> { checkTask.dependsOn(testTaskProvider); }); } private static List findSourceVersions(Project project) { From 91806bdb26f86e802bc9ed230b030be6bcee671a Mon Sep 17 00:00:00 2001 From: Ryan Ernst Date: Wed, 20 Mar 2024 15:45:58 -0700 Subject: [PATCH 7/7] cleanup --- .../org/elasticsearch/gradle/internal/MrjarPlugin.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/MrjarPlugin.java b/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/MrjarPlugin.java index e4a83534459bf..9e2f44323f914 100644 --- a/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/MrjarPlugin.java +++ b/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/MrjarPlugin.java @@ -70,7 +70,7 @@ public void apply(Project project) { SourceSet mainSourceSet = addSourceSet(project, javaExtension, mainSourceSetName, mainSourceSets, javaVersion); configureSourceSetInJar(project, mainSourceSet, javaVersion); mainSourceSets.add(mainSourceSetName); - testSourceSets.add(SourceSet.MAIN_SOURCE_SET_NAME + javaVersion); + testSourceSets.add(mainSourceSetName); String testSourceSetName = SourceSet.TEST_SOURCE_SET_NAME + javaVersion; SourceSet testSourceSet = addSourceSet(project, javaExtension, testSourceSetName, testSourceSets, javaVersion); @@ -133,7 +133,7 @@ private SourceSet addSourceSet( private void configureSourceSetInJar(Project project, SourceSet sourceSet, int javaVersion) { var jarTask = project.getTasks().withType(Jar.class).named(JavaPlugin.JAR_TASK_NAME); - jarTask.configure(task -> { task.into("META-INF/versions/" + javaVersion, copySpec -> copySpec.from(sourceSet.getOutput())); }); + jarTask.configure(task -> task.into("META-INF/versions/" + javaVersion, copySpec -> copySpec.from(sourceSet.getOutput()))); } private void createTestTask(Project project, SourceSet sourceSet, int javaVersion, List mainSourceSets) { @@ -149,12 +149,13 @@ private void createTestTask(Project project, SourceSet sourceSet, int javaVersio testRuntime = testRuntime.minus(mainRuntime); } testTask.setClasspath(testRuntime.plus(project.files(jarTask))); + testTask.setTestClassesDirs(sourceSet.getOutput().getClassesDirs()); testTask.getJavaLauncher() .set(javaToolchains.launcherFor(spec -> spec.getLanguageVersion().set(JavaLanguageVersion.of(javaVersion)))); }); - project.getTasks().named("check").configure(checkTask -> { checkTask.dependsOn(testTaskProvider); }); + project.getTasks().named("check").configure(checkTask -> checkTask.dependsOn(testTaskProvider)); } private static List findSourceVersions(Project project) {