From e599ed01c964761fc87f7b03885f95e9b7f93302 Mon Sep 17 00:00:00 2001 From: dreis2211 Date: Fri, 14 Feb 2020 10:56:28 +0100 Subject: [PATCH 1/2] Allow Gradle tasks to be executed with a custom Java home See gh-20179 --- .../boot/build/ConventionsPlugin.java | 25 +++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) 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..278ccb280c 100644 --- a/buildSrc/src/main/java/org/springframework/boot/build/ConventionsPlugin.java +++ b/buildSrc/src/main/java/org/springframework/boot/build/ConventionsPlugin.java @@ -127,14 +127,27 @@ public class ConventionsPlugin implements Plugin { project.setProperty("sourceCompatibility", "1.8"); project.getTasks().withType(JavaCompile.class, (compile) -> { compile.getOptions().setEncoding("UTF-8"); + if (hasCustomBuildJavaHome(project)) { + String javaExecutable = getCustomBuildJavaExecutable(project, "/bin/java"); + compile.getOptions().getForkOptions().setJavaHome(new File(javaExecutable)); + } 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"); + if (hasCustomBuildJavaHome(project)) { + String javaExecutable = getCustomBuildJavaExecutable(project, "/bin/javadoc"); + javadoc.setExecutable(javaExecutable); + } + }); project.getTasks().withType(Test.class, (test) -> { + if (hasCustomBuildJavaHome(project)) { + String javaExecutable = getCustomBuildJavaExecutable(project, "/bin/java"); + test.setExecutable(javaExecutable); + } test.useJUnitPlatform(); test.setMaxHeapSize("1024M"); }); @@ -192,6 +205,14 @@ public class ConventionsPlugin implements Plugin { return legalFile; } + private boolean hasCustomBuildJavaHome(Project project) { + return project.hasProperty("buildJavaHome") && !((String) project.property("buildJavaHome")).isEmpty(); + } + + private String getCustomBuildJavaExecutable(Project project, String executable) { + return project.property("buildJavaHome") + executable; + } + private void configureSpringJavaFormat(Project project) { project.getPlugins().apply(SpringJavaFormatPlugin.class); project.getTasks().withType(FormatTask.class, (formatTask) -> formatTask.setEncoding("UTF-8")); From 8f44bd89f47cc312ccd67979d6acfb3ab7df2766 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 25 Feb 2020 14:42:21 +0000 Subject: [PATCH 2/2] Polish "Allow Gradle tasks to be executed with a custom Java home" See gh-20179 --- .../boot/build/ConventionsPlugin.java | 31 ++++++++----------- settings.gradle | 10 ++++++ 2 files changed, 23 insertions(+), 18 deletions(-) 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 278ccb280c..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,10 +128,11 @@ public class ConventionsPlugin implements Plugin { project.setProperty("sourceCompatibility", "1.8"); project.getTasks().withType(JavaCompile.class, (compile) -> { compile.getOptions().setEncoding("UTF-8"); - if (hasCustomBuildJavaHome(project)) { - String javaExecutable = getCustomBuildJavaExecutable(project, "/bin/java"); - compile.getOptions().getForkOptions().setJavaHome(new File(javaExecutable)); - } + 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"); @@ -138,16 +140,10 @@ public class ConventionsPlugin implements Plugin { }); project.getTasks().withType(Javadoc.class, (javadoc) -> { javadoc.getOptions().source("1.8").encoding("UTF-8"); - if (hasCustomBuildJavaHome(project)) { - String javaExecutable = getCustomBuildJavaExecutable(project, "/bin/javadoc"); - javadoc.setExecutable(javaExecutable); - } + withOptionalBuildJavaHome(project, (javaHome) -> javadoc.setExecutable(javaHome + "/bin/javadoc")); }); project.getTasks().withType(Test.class, (test) -> { - if (hasCustomBuildJavaHome(project)) { - String javaExecutable = getCustomBuildJavaExecutable(project, "/bin/java"); - test.setExecutable(javaExecutable); - } + withOptionalBuildJavaHome(project, (javaHome) -> test.setExecutable(javaHome + "/bin/java")); test.useJUnitPlatform(); test.setMaxHeapSize("1024M"); }); @@ -205,12 +201,11 @@ public class ConventionsPlugin implements Plugin { return legalFile; } - private boolean hasCustomBuildJavaHome(Project project) { - return project.hasProperty("buildJavaHome") && !((String) project.property("buildJavaHome")).isEmpty(); - } - - private String getCustomBuildJavaExecutable(Project project, String executable) { - return project.property("buildJavaHome") + executable; + 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) { 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"