Skip to content

Commit 563083b

Browse files
committed
Polish "Allow Gradle tasks to be executed with a custom Java home"
See spring-projectsgh-20179
1 parent 70e1239 commit 563083b

File tree

1 file changed

+12
-18
lines changed

1 file changed

+12
-18
lines changed

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

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.List;
2727
import java.util.Map;
2828
import java.util.TreeMap;
29+
import java.util.function.Consumer;
2930

3031
import io.spring.javaformat.gradle.FormatTask;
3132
import io.spring.javaformat.gradle.SpringJavaFormatPlugin;
@@ -127,27 +128,21 @@ private void applyJavaConventions(Project project) {
127128
project.setProperty("sourceCompatibility", "1.8");
128129
project.getTasks().withType(JavaCompile.class, (compile) -> {
129130
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-
}
131+
withOptionalBuildJavaHome(project, (javaHome) -> {
132+
compile.getOptions().getForkOptions().setJavaHome(new File(javaHome));
133+
compile.getOptions().getForkOptions().setExecutable(javaHome + "/bin/javac");
134+
});
134135
List<String> args = compile.getOptions().getCompilerArgs();
135136
if (!args.contains("-parameters")) {
136137
args.add("-parameters");
137138
}
138139
});
139140
project.getTasks().withType(Javadoc.class, (javadoc) -> {
140141
javadoc.getOptions().source("1.8").encoding("UTF-8");
141-
if (hasCustomBuildJavaHome(project)) {
142-
String javaExecutable = getCustomBuildJavaExecutable(project, "/bin/javadoc");
143-
javadoc.setExecutable(javaExecutable);
144-
}
142+
withOptionalBuildJavaHome(project, (javaHome) -> javadoc.setExecutable(javaHome + "/bin/javadoc"));
145143
});
146144
project.getTasks().withType(Test.class, (test) -> {
147-
if (hasCustomBuildJavaHome(project)) {
148-
String javaExecutable = getCustomBuildJavaExecutable(project, "/bin/java");
149-
test.setExecutable(javaExecutable);
150-
}
145+
withOptionalBuildJavaHome(project, (javaHome) -> test.setExecutable(javaHome + "/bin/java"));
151146
test.useJUnitPlatform();
152147
test.setMaxHeapSize("1024M");
153148
});
@@ -205,12 +200,11 @@ private File createLegalFile(File source, String filename) {
205200
return legalFile;
206201
}
207202

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;
203+
private void withOptionalBuildJavaHome(Project project, Consumer<String> consumer) {
204+
String buildJavaHome = (String) project.findProperty("buildJavaHome");
205+
if (buildJavaHome != null && !buildJavaHome.isEmpty()) {
206+
consumer.accept(buildJavaHome);
207+
}
214208
}
215209

216210
private void configureSpringJavaFormat(Project project) {

0 commit comments

Comments
 (0)