Allow Gradle tasks to be executed with a custom Java home

See gh-20179
This commit is contained in:
dreis2211 2020-02-14 10:56:28 +01:00 committed by Andy Wilkinson
parent ea66940be1
commit e599ed01c9

View File

@ -127,14 +127,27 @@ public class ConventionsPlugin implements Plugin<Project> {
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<String> 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<Project> {
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"));