From 51000a0a1100de445a95cb3325bc09a2c516b6fa Mon Sep 17 00:00:00 2001 From: dreis2211 Date: Fri, 14 Feb 2020 10:56:28 +0100 Subject: [PATCH 1/2] Implement possibility to set custom java home for Gradle tasks See gh-20147 --- .../boot/build/ConventionsPlugin.java | 26 +++++++++++++++++-- 1 file changed, 24 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 b1993b0cc6c6..006518e27c63 100644 --- a/buildSrc/src/main/java/org/springframework/boot/build/ConventionsPlugin.java +++ b/buildSrc/src/main/java/org/springframework/boot/build/ConventionsPlugin.java @@ -16,6 +16,7 @@ package org.springframework.boot.build; +import java.io.File; import java.util.List; import java.util.Map; import java.util.TreeMap; @@ -114,14 +115,27 @@ private void applyJavaConventions(Project project) { project.setProperty("sourceCompatibility", "1.8"); project.getTasks().withType(JavaCompile.class, (compile) -> { compile.getOptions().setEncoding("UTF-8"); + if (hasCustomJavaHome(project)) { + String javaExecutable = getCustomJavaExecutable(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 (hasCustomJavaHome(project)) { + String javaExecutable = getCustomJavaExecutable(project, "/bin/javadoc"); + javadoc.setExecutable(javaExecutable); + } + }); project.getTasks().withType(Test.class, (test) -> { + if (hasCustomJavaHome(project)) { + String javaExecutable = getCustomJavaExecutable(project, "/bin/java"); + test.setExecutable(javaExecutable); + } test.useJUnitPlatform(); test.setMaxHeapSize("1024M"); }); @@ -141,6 +155,14 @@ private void applyJavaConventions(Project project) { }); } + private boolean hasCustomJavaHome(Project project) { + return project.hasProperty("customJavaHome") && !((String) project.property("customJavaHome")).isEmpty(); + } + + private String getCustomJavaExecutable(Project project, String executable) { + return project.property("customJavaHome") + executable; + } + private void configureSpringJavaFormat(Project project) { project.getPlugins().apply(SpringJavaFormatPlugin.class); project.getTasks().withType(FormatTask.class, (formatTask) -> formatTask.setEncoding("UTF-8")); From b8124aabd6e4dd0231ad35dbb8537be3fe8f5c58 Mon Sep 17 00:00:00 2001 From: dreis2211 Date: Wed, 19 Feb 2020 17:51:47 +0100 Subject: [PATCH 2/2] Rename customJavaHome to buildJavaHome --- .../boot/build/ConventionsPlugin.java | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 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 006518e27c63..6ffda4639c69 100644 --- a/buildSrc/src/main/java/org/springframework/boot/build/ConventionsPlugin.java +++ b/buildSrc/src/main/java/org/springframework/boot/build/ConventionsPlugin.java @@ -115,8 +115,8 @@ private void applyJavaConventions(Project project) { project.setProperty("sourceCompatibility", "1.8"); project.getTasks().withType(JavaCompile.class, (compile) -> { compile.getOptions().setEncoding("UTF-8"); - if (hasCustomJavaHome(project)) { - String javaExecutable = getCustomJavaExecutable(project, "/bin/java"); + if (hasCustomBuildJavaHome(project)) { + String javaExecutable = getCustomBuildJavaExecutable(project, "/bin/java"); compile.getOptions().getForkOptions().setJavaHome(new File(javaExecutable)); } List args = compile.getOptions().getCompilerArgs(); @@ -126,14 +126,14 @@ private void applyJavaConventions(Project project) { }); project.getTasks().withType(Javadoc.class, (javadoc) -> { javadoc.getOptions().source("1.8").encoding("UTF-8"); - if (hasCustomJavaHome(project)) { - String javaExecutable = getCustomJavaExecutable(project, "/bin/javadoc"); + if (hasCustomBuildJavaHome(project)) { + String javaExecutable = getCustomBuildJavaExecutable(project, "/bin/javadoc"); javadoc.setExecutable(javaExecutable); } }); project.getTasks().withType(Test.class, (test) -> { - if (hasCustomJavaHome(project)) { - String javaExecutable = getCustomJavaExecutable(project, "/bin/java"); + if (hasCustomBuildJavaHome(project)) { + String javaExecutable = getCustomBuildJavaExecutable(project, "/bin/java"); test.setExecutable(javaExecutable); } test.useJUnitPlatform(); @@ -155,12 +155,12 @@ private void applyJavaConventions(Project project) { }); } - private boolean hasCustomJavaHome(Project project) { - return project.hasProperty("customJavaHome") && !((String) project.property("customJavaHome")).isEmpty(); + private boolean hasCustomBuildJavaHome(Project project) { + return project.hasProperty("buildJavaHome") && !((String) project.property("buildJavaHome")).isEmpty(); } - private String getCustomJavaExecutable(Project project, String executable) { - return project.property("customJavaHome") + executable; + private String getCustomBuildJavaExecutable(Project project, String executable) { + return project.property("buildJavaHome") + executable; } private void configureSpringJavaFormat(Project project) {