Skip to content

Commit e599ed0

Browse files
dreis2211wilkinsona
authored andcommitted
Allow Gradle tasks to be executed with a custom Java home
See gh-20179
1 parent ea66940 commit e599ed0

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

buildSrc/src/main/java/org/springframework/boot/build/ConventionsPlugin.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,27 @@ private void applyJavaConventions(Project project) {
127127
project.setProperty("sourceCompatibility", "1.8");
128128
project.getTasks().withType(JavaCompile.class, (compile) -> {
129129
compile.getOptions().setEncoding("UTF-8");
130+
if (hasCustomBuildJavaHome(project)) {
131+
String javaExecutable = getCustomBuildJavaExecutable(project, "/bin/java");
132+
compile.getOptions().getForkOptions().setJavaHome(new File(javaExecutable));
133+
}
130134
List<String> args = compile.getOptions().getCompilerArgs();
131135
if (!args.contains("-parameters")) {
132136
args.add("-parameters");
133137
}
134138
});
135-
project.getTasks().withType(Javadoc.class,
136-
(javadoc) -> javadoc.getOptions().source("1.8").encoding("UTF-8"));
139+
project.getTasks().withType(Javadoc.class, (javadoc) -> {
140+
javadoc.getOptions().source("1.8").encoding("UTF-8");
141+
if (hasCustomBuildJavaHome(project)) {
142+
String javaExecutable = getCustomBuildJavaExecutable(project, "/bin/javadoc");
143+
javadoc.setExecutable(javaExecutable);
144+
}
145+
});
137146
project.getTasks().withType(Test.class, (test) -> {
147+
if (hasCustomBuildJavaHome(project)) {
148+
String javaExecutable = getCustomBuildJavaExecutable(project, "/bin/java");
149+
test.setExecutable(javaExecutable);
150+
}
138151
test.useJUnitPlatform();
139152
test.setMaxHeapSize("1024M");
140153
});
@@ -192,6 +205,14 @@ private File createLegalFile(File source, String filename) {
192205
return legalFile;
193206
}
194207

208+
private boolean hasCustomBuildJavaHome(Project project) {
209+
return project.hasProperty("buildJavaHome") && !((String) project.property("buildJavaHome")).isEmpty();
210+
}
211+
212+
private String getCustomBuildJavaExecutable(Project project, String executable) {
213+
return project.property("buildJavaHome") + executable;
214+
}
215+
195216
private void configureSpringJavaFormat(Project project) {
196217
project.getPlugins().apply(SpringJavaFormatPlugin.class);
197218
project.getTasks().withType(FormatTask.class, (formatTask) -> formatTask.setEncoding("UTF-8"));

0 commit comments

Comments
 (0)