diff --git a/buildSrc/src/main/java/org/springframework/boot/build/ConventionsPlugin.java b/buildSrc/src/main/java/org/springframework/boot/build/ConventionsPlugin.java index b4d5231433..633280f0c2 100644 --- a/buildSrc/src/main/java/org/springframework/boot/build/ConventionsPlugin.java +++ b/buildSrc/src/main/java/org/springframework/boot/build/ConventionsPlugin.java @@ -26,6 +26,7 @@ import java.nio.charset.StandardCharsets; import java.util.List; import java.util.Map; import java.util.TreeMap; +import java.util.function.Consumer; import io.spring.javaformat.gradle.FormatTask; import io.spring.javaformat.gradle.SpringJavaFormatPlugin; @@ -127,14 +128,22 @@ public class ConventionsPlugin implements Plugin { project.setProperty("sourceCompatibility", "1.8"); 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"); + }); List args = compile.getOptions().getCompilerArgs(); if (!args.contains("-parameters")) { args.add("-parameters"); } }); - project.getTasks().withType(Javadoc.class, - (javadoc) -> javadoc.getOptions().source("1.8").encoding("UTF-8")); + 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(Test.class, (test) -> { + withOptionalBuildJavaHome(project, (javaHome) -> test.setExecutable(javaHome + "/bin/java")); test.useJUnitPlatform(); test.setMaxHeapSize("1024M"); }); @@ -192,6 +201,13 @@ public class ConventionsPlugin implements Plugin { return legalFile; } + 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")); diff --git a/settings.gradle b/settings.gradle index ad7598ae91..aff1f94cd9 100644 --- a/settings.gradle +++ b/settings.gradle @@ -28,6 +28,16 @@ plugins { 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')) + } + } + } +} + include "spring-boot-project:spring-boot-dependencies" include "spring-boot-project:spring-boot-parent" include "spring-boot-project:spring-boot-tools:spring-boot-antlib"