From 01b0156fed99766443466e0c40c43ffd60b73558 Mon Sep 17 00:00:00 2001 From: dreis2211 Date: Thu, 24 Jun 2021 20:11:26 +0200 Subject: [PATCH 1/2] Allow additional JVM args when running tests via toolchain See gh-27089 --- .../boot/build/toolchain/ToolchainExtension.java | 8 ++++++++ .../boot/build/toolchain/ToolchainPlugin.java | 15 ++++++--------- spring-boot-project/spring-boot/build.gradle | 4 ++++ 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/buildSrc/src/main/java/org/springframework/boot/build/toolchain/ToolchainExtension.java b/buildSrc/src/main/java/org/springframework/boot/build/toolchain/ToolchainExtension.java index efa3731a43..0de3303cb1 100644 --- a/buildSrc/src/main/java/org/springframework/boot/build/toolchain/ToolchainExtension.java +++ b/buildSrc/src/main/java/org/springframework/boot/build/toolchain/ToolchainExtension.java @@ -17,6 +17,7 @@ package org.springframework.boot.build.toolchain; import org.gradle.api.Project; +import org.gradle.api.provider.ListProperty; import org.gradle.api.provider.Property; import org.gradle.jvm.toolchain.JavaLanguageVersion; @@ -29,10 +30,13 @@ public class ToolchainExtension { private final Property maximumCompatibleJavaVersion; + private final ListProperty testJvmArgs; + private final JavaLanguageVersion javaVersion; public ToolchainExtension(Project project) { this.maximumCompatibleJavaVersion = project.getObjects().property(JavaLanguageVersion.class); + this.testJvmArgs = project.getObjects().listProperty(String.class); String toolchainVersion = (String) project.findProperty("toolchainVersion"); this.javaVersion = (toolchainVersion != null) ? JavaLanguageVersion.of(toolchainVersion) : null; } @@ -41,6 +45,10 @@ public class ToolchainExtension { return this.maximumCompatibleJavaVersion; } + public ListProperty getTestJvmArgs() { + return this.testJvmArgs; + } + JavaLanguageVersion getJavaVersion() { return this.javaVersion; } diff --git a/buildSrc/src/main/java/org/springframework/boot/build/toolchain/ToolchainPlugin.java b/buildSrc/src/main/java/org/springframework/boot/build/toolchain/ToolchainPlugin.java index 9bb259ccda..a8e55e1bc5 100644 --- a/buildSrc/src/main/java/org/springframework/boot/build/toolchain/ToolchainPlugin.java +++ b/buildSrc/src/main/java/org/springframework/boot/build/toolchain/ToolchainPlugin.java @@ -16,9 +16,6 @@ package org.springframework.boot.build.toolchain; -import java.util.Collections; -import java.util.List; - import org.gradle.api.Plugin; import org.gradle.api.Project; import org.gradle.api.plugins.JavaPluginExtension; @@ -56,7 +53,7 @@ public class ToolchainPlugin implements Plugin { JavaToolchainSpec toolchainSpec = project.getExtensions().getByType(JavaPluginExtension.class) .getToolchain(); toolchainSpec.getLanguageVersion().set(toolchain.getJavaVersion()); - configureTestToolchain(project); + configureTestToolchain(project, toolchain); } } @@ -71,11 +68,11 @@ public class ToolchainPlugin implements Plugin { project.getTasks().withType(Test.class, (task) -> task.setEnabled(false)); } - private void configureTestToolchain(Project project) { - project.getTasks().withType(Test.class, (test) -> { - List arguments = Collections.singletonList("--illegal-access=warn"); - test.jvmArgs(arguments); - }); + private void configureTestToolchain(Project project, ToolchainExtension toolchain) { + if (!toolchain.getTestJvmArgs().isPresent()) { + return; + } + project.getTasks().withType(Test.class, (test) -> test.jvmArgs(toolchain.getTestJvmArgs().get())); } } diff --git a/spring-boot-project/spring-boot/build.gradle b/spring-boot-project/spring-boot/build.gradle index 2a887b3099..4f9a991444 100644 --- a/spring-boot-project/spring-boot/build.gradle +++ b/spring-boot-project/spring-boot/build.gradle @@ -9,6 +9,10 @@ plugins { description = "Spring Boot" +toolchain { + testJvmArgs.add("--add-opens=java.base/java.net=ALL-UNNAMED") +} + def tomcatConfigProperties = "$buildDir/tomcat-config-properties" configurations { From 705feeed6a0c4683bffa1e81173361639076e46b Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 13 Jul 2021 15:28:04 +0100 Subject: [PATCH 2/2] Polish "Allow additional JVM args when running tests via toolchain" See gh-27089 --- .../boot/build/toolchain/ToolchainPlugin.java | 8 ++++---- spring-boot-project/spring-boot/build.gradle | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/buildSrc/src/main/java/org/springframework/boot/build/toolchain/ToolchainPlugin.java b/buildSrc/src/main/java/org/springframework/boot/build/toolchain/ToolchainPlugin.java index a8e55e1bc5..848293dde3 100644 --- a/buildSrc/src/main/java/org/springframework/boot/build/toolchain/ToolchainPlugin.java +++ b/buildSrc/src/main/java/org/springframework/boot/build/toolchain/ToolchainPlugin.java @@ -16,6 +16,8 @@ package org.springframework.boot.build.toolchain; +import java.util.Collections; + import org.gradle.api.Plugin; import org.gradle.api.Project; import org.gradle.api.plugins.JavaPluginExtension; @@ -69,10 +71,8 @@ public class ToolchainPlugin implements Plugin { } private void configureTestToolchain(Project project, ToolchainExtension toolchain) { - if (!toolchain.getTestJvmArgs().isPresent()) { - return; - } - project.getTasks().withType(Test.class, (test) -> test.jvmArgs(toolchain.getTestJvmArgs().get())); + project.getTasks().withType(Test.class, + (test) -> test.jvmArgs(toolchain.getTestJvmArgs().getOrElse(Collections.emptyList()))); } } diff --git a/spring-boot-project/spring-boot/build.gradle b/spring-boot-project/spring-boot/build.gradle index 4f9a991444..0881a2bcf5 100644 --- a/spring-boot-project/spring-boot/build.gradle +++ b/spring-boot-project/spring-boot/build.gradle @@ -9,10 +9,6 @@ plugins { description = "Spring Boot" -toolchain { - testJvmArgs.add("--add-opens=java.base/java.net=ALL-UNNAMED") -} - def tomcatConfigProperties = "$buildDir/tomcat-config-properties" configurations { @@ -154,3 +150,7 @@ compileTestKotlin { compileJava { doLast new org.springframework.boot.build.log4j2.ReproducibleLog4j2PluginsDatAction() } + +toolchain { + testJvmArgs.add("--add-opens=java.base/java.net=ALL-UNNAMED") +}