diff --git a/buildSrc/src/main/java/org/springframework/boot/build/JavaConventions.java b/buildSrc/src/main/java/org/springframework/boot/build/JavaConventions.java index 6ef28b12413f..6e03dca09d74 100644 --- a/buildSrc/src/main/java/org/springframework/boot/build/JavaConventions.java +++ b/buildSrc/src/main/java/org/springframework/boot/build/JavaConventions.java @@ -16,14 +16,12 @@ package org.springframework.boot.build; -import java.io.File; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Set; import java.util.TreeMap; -import java.util.function.Consumer; import java.util.stream.Collectors; import io.spring.javaformat.gradle.FormatTask; @@ -49,6 +47,7 @@ import org.springframework.boot.build.optional.OptionalDependenciesPlugin; import org.springframework.boot.build.testing.TestFailuresPlugin; +import org.springframework.boot.build.toolchain.ToolchainPlugin; /** * Conventions that are applied in the presence of the {@link JavaBasePlugin}. When the @@ -103,6 +102,7 @@ void apply(Project project) { configureTestConventions(project); configureJarManifestConventions(project); configureDependencyManagement(project); + configureToolchain(project); }); } @@ -145,7 +145,6 @@ private String determineImplementationTitle(Project project, Set sourceJ private void configureTestConventions(Project project) { project.getTasks().withType(Test.class, (test) -> { - withOptionalBuildJavaHome(project, (javaHome) -> test.setExecutable(javaHome + "/bin/java")); test.useJUnitPlatform(); test.setMaxHeapSize("1024M"); }); @@ -165,38 +164,25 @@ private boolean isCi() { } private void configureJavadocConventions(Project project) { - project.getTasks().withType(Javadoc.class, (javadoc) -> { - javadoc.getOptions().source("1.8").encoding("UTF-8"); - withOptionalBuildJavaHome(project, (javaHome) -> javadoc.setExecutable(javaHome + "/bin/javadoc")); - }); + project.getTasks().withType(Javadoc.class, (javadoc) -> javadoc.getOptions().source("1.8").encoding("UTF-8")); } private void configureJavaCompileConventions(Project project) { project.getTasks().withType(JavaCompile.class, (compile) -> { compile.getOptions().setEncoding("UTF-8"); - withOptionalBuildJavaHome(project, (javaHome) -> { - compile.getOptions().setFork(true); - compile.getOptions().getForkOptions().setJavaHome(new File(javaHome)); - compile.getOptions().getForkOptions().setExecutable(javaHome + "/bin/javac"); - }); + compile.setSourceCompatibility("1.8"); + compile.setTargetCompatibility("1.8"); List args = compile.getOptions().getCompilerArgs(); if (!args.contains("-parameters")) { args.add("-parameters"); } - if (JavaVersion.current() == JavaVersion.VERSION_1_8) { + if (!project.hasProperty("toolchainVersion") && JavaVersion.current() == JavaVersion.VERSION_1_8) { args.addAll(Arrays.asList("-Werror", "-Xlint:unchecked", "-Xlint:deprecation", "-Xlint:rawtypes", "-Xlint:varargs")); } }); } - private void withOptionalBuildJavaHome(Project project, Consumer consumer) { - String buildJavaHome = (String) project.findProperty("buildJavaHome"); - if (buildJavaHome != null && !buildJavaHome.isEmpty()) { - consumer.accept(buildJavaHome); - } - } - private void configureSpringJavaFormat(Project project) { project.getPlugins().apply(SpringJavaFormatPlugin.class); project.getTasks().withType(FormatTask.class, (formatTask) -> formatTask.setEncoding("UTF-8")); @@ -228,4 +214,8 @@ private void configureDependencyManagement(Project project) { .getByName(OptionalDependenciesPlugin.OPTIONAL_CONFIGURATION_NAME).extendsFrom(dependencyManagement)); } + private void configureToolchain(Project project) { + project.getPlugins().apply(ToolchainPlugin.class); + } + } 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 new file mode 100644 index 000000000000..eb1a7d1b48d0 --- /dev/null +++ b/buildSrc/src/main/java/org/springframework/boot/build/toolchain/ToolchainExtension.java @@ -0,0 +1,68 @@ +/* + * Copyright 2012-2021 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.build.toolchain; + +import java.util.Optional; + +import org.gradle.api.Project; +import org.gradle.jvm.toolchain.JavaLanguageVersion; + +/** + * DSL extension for {@link ToolchainPlugin}. + * + * @author Christoph Dreis + */ +public class ToolchainExtension { + + private final Project project; + + private int maximumCompatibleJavaVersion; + + public ToolchainExtension(Project project) { + this.project = project; + } + + public void setMaximumCompatibleJavaVersion(int maximumVersion) { + this.maximumCompatibleJavaVersion = maximumVersion; + } + + public Optional getToolchainVersion() { + String toolchainVersion = (String) this.project.findProperty("toolchainVersion"); + if (toolchainVersion == null) { + return Optional.empty(); + } + int version = Integer.parseInt(toolchainVersion); + return getJavaLanguageVersion(version); + } + + public boolean isJavaVersionSupported() { + Optional maximumVersion = getJavaLanguageVersion(this.maximumCompatibleJavaVersion); + if (!maximumVersion.isPresent()) { + return true; + } + Optional toolchainVersion = getToolchainVersion(); + return toolchainVersion.isPresent() && maximumVersion.get().canCompileOrRun(toolchainVersion.get()); + } + + private Optional getJavaLanguageVersion(int version) { + if (version >= 8) { + return Optional.of(JavaLanguageVersion.of(version)); + } + return Optional.empty(); + } + +} 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 new file mode 100644 index 000000000000..aedacfaf1146 --- /dev/null +++ b/buildSrc/src/main/java/org/springframework/boot/build/toolchain/ToolchainPlugin.java @@ -0,0 +1,116 @@ +/* + * Copyright 2012-2021 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.build.toolchain; + +import java.util.Arrays; +import java.util.List; +import java.util.Optional; + +import org.gradle.api.Action; +import org.gradle.api.Plugin; +import org.gradle.api.Project; +import org.gradle.api.tasks.compile.JavaCompile; +import org.gradle.api.tasks.javadoc.Javadoc; +import org.gradle.api.tasks.testing.Test; +import org.gradle.jvm.toolchain.JavaLanguageVersion; +import org.gradle.jvm.toolchain.JavaToolchainService; +import org.gradle.jvm.toolchain.JavaToolchainSpec; + +/** + * {@link Plugin} for customizing Gradle's toolchain support. + * + * @author Christoph Dreis + */ +public class ToolchainPlugin implements Plugin { + + @Override + public void apply(Project project) { + configureToolchain(project); + } + + private void configureToolchain(Project project) { + ToolchainExtension toolchain = project.getExtensions().create("toolchain", ToolchainExtension.class, project); + project.afterEvaluate((evaluated) -> { + Optional toolchainVersion = toolchain.getToolchainVersion(); + if (toolchainVersion.isPresent()) { + if (!toolchain.isJavaVersionSupported()) { + disableToolchainTasks(project); + } + else { + configureJavaCompileToolchain(project, toolchain); + configureJavadocToolchain(project, toolchain); + configureTestToolchain(project, toolchain); + } + } + }); + } + + private void disableToolchainTasks(Project project) { + project.getTasks().withType(JavaCompile.class, (task) -> task.setEnabled(false)); + project.getTasks().withType(Javadoc.class, (task) -> task.setEnabled(false)); + project.getTasks().withType(Test.class, (task) -> task.setEnabled(false)); + } + + private void configureJavaCompileToolchain(Project project, ToolchainExtension toolchain) { + project.getTasks().withType(JavaCompile.class, (compile) -> { + withOptionalJavaToolchain(toolchain).ifPresent((action) -> { + JavaToolchainService service = getJavaToolchainService(project); + compile.getJavaCompiler().set(service.compilerFor(action)); + compile.getOptions().setFork(true); + // See https://github.com/gradle/gradle/issues/15538 + List forkArgs = Arrays.asList("--add-opens", + "jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED"); + compile.getOptions().getForkOptions().getJvmArgs().addAll(forkArgs); + }); + }); + } + + private void configureJavadocToolchain(Project project, ToolchainExtension toolchain) { + project.getTasks().withType(Javadoc.class, (javadoc) -> { + withOptionalJavaToolchain(toolchain).ifPresent((action) -> { + JavaToolchainService service = getJavaToolchainService(project); + javadoc.getJavadocTool().set(service.javadocToolFor(action)); + }); + }); + } + + private void configureTestToolchain(Project project, ToolchainExtension toolchain) { + project.getTasks().withType(Test.class, (test) -> { + withOptionalJavaToolchain(toolchain).ifPresent((action) -> { + JavaToolchainService service = getJavaToolchainService(project); + test.getJavaLauncher().set(service.launcherFor(action)); + // See https://github.com/spring-projects/spring-ldap/issues/570 + List arguments = Arrays.asList("--add-exports=java.naming/com.sun.jndi.ldap=ALL-UNNAMED", + "--illegal-access=warn"); + test.jvmArgs(arguments); + }); + }); + } + + private JavaToolchainService getJavaToolchainService(Project project) { + return project.getExtensions().getByType(JavaToolchainService.class); + } + + private Optional> withOptionalJavaToolchain(ToolchainExtension toolchain) { + return toolchain.getToolchainVersion().map((toolchainVersion) -> { + Action action = (javaToolchainSpec) -> javaToolchainSpec.getLanguageVersion() + .convention(toolchainVersion); + return Optional.of(action); + }).orElse(Optional.empty()); + } + +} diff --git a/settings.gradle b/settings.gradle index 45b4a5f4f32e..301786b0214b 100644 --- a/settings.gradle +++ b/settings.gradle @@ -31,8 +31,8 @@ rootProject.name="spring-boot-build" settings.gradle.projectsLoaded { gradleEnterprise { buildScan { - if (settings.gradle.rootProject.hasProperty('buildJavaHome')) { - value('Build Java home', settings.gradle.rootProject.getProperty('buildJavaHome')) + if (settings.gradle.rootProject.hasProperty('toolchainVersion')) { + value('Toolchain Version', settings.gradle.rootProject.getProperty('toolchainVersion')) } settings.gradle.rootProject.getBuildDir().mkdirs() diff --git a/spring-boot-project/spring-boot-cli/build.gradle b/spring-boot-project/spring-boot-cli/build.gradle index 3172eb5e173b..f0a3b1f261a8 100644 --- a/spring-boot-project/spring-boot-cli/build.gradle +++ b/spring-boot-project/spring-boot-cli/build.gradle @@ -7,6 +7,10 @@ plugins { description = "Spring Boot CLI" +toolchain { + maximumCompatibleJavaVersion = 15 +} + configurations { dependenciesBom loader diff --git a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/json/JsonbTesterTests.java b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/json/JsonbTesterTests.java index 29892aeb8feb..92237238c2c4 100644 --- a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/json/JsonbTesterTests.java +++ b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/json/JsonbTesterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/build.gradle b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/build.gradle index 0153b836477f..5fb4c3a68dfc 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/build.gradle +++ b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/build.gradle @@ -10,6 +10,10 @@ plugins { description = "Spring Boot Gradle Plugin" +toolchain { + maximumCompatibleJavaVersion = 15 +} + configurations { asciidoctorExtensions { extendsFrom dependencyManagement diff --git a/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/build.gradle b/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/build.gradle index 2d162f7079c9..c31d7d4aae5e 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/build.gradle +++ b/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/build.gradle @@ -69,7 +69,7 @@ processResources { } compileJava { - if ((!project.hasProperty("buildJavaHome")) && JavaVersion.current() == JavaVersion.VERSION_1_8) { + if ((!project.hasProperty("toolchainVersion")) && JavaVersion.current() == JavaVersion.VERSION_1_8) { options.compilerArgs += ['-Xlint:-sunapi', '-XDenableSunApiLintControl'] } } diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/java/org/springframework/boot/maven/JarIntegrationTests.java b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/java/org/springframework/boot/maven/JarIntegrationTests.java index c85ecc9c8880..62ec50aaec69 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/java/org/springframework/boot/maven/JarIntegrationTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/intTest/java/org/springframework/boot/maven/JarIntegrationTests.java @@ -25,6 +25,8 @@ import java.util.stream.Collectors; import org.junit.jupiter.api.TestTemplate; +import org.junit.jupiter.api.condition.DisabledForJreRange; +import org.junit.jupiter.api.condition.JRE; import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.boot.loader.tools.FileUtils; @@ -247,6 +249,7 @@ void whenADependencyHasTestScopeItIsNotIncludedInTheRepackagedJar(MavenBuild mav }); } + @DisabledForJreRange(min = JRE.JAVA_16) // Remove this once Kotlin supports Java 16 @TestTemplate void whenAProjectUsesKotlinItsModuleMetadataIsRepackagedIntoBootInfClasses(MavenBuild mavenBuild) { mavenBuild.project("jar-with-kotlin-module").execute((project) -> {