|
26 | 26 | import java.util.List;
|
27 | 27 | import java.util.Map;
|
28 | 28 | import java.util.TreeMap;
|
| 29 | +import java.util.function.Consumer; |
29 | 30 |
|
30 | 31 | import io.spring.javaformat.gradle.FormatTask;
|
31 | 32 | import io.spring.javaformat.gradle.SpringJavaFormatPlugin;
|
@@ -127,27 +128,22 @@ private void applyJavaConventions(Project project) {
|
127 | 128 | project.setProperty("sourceCompatibility", "1.8");
|
128 | 129 | project.getTasks().withType(JavaCompile.class, (compile) -> {
|
129 | 130 | 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().setFork(true); |
| 133 | + compile.getOptions().getForkOptions().setJavaHome(new File(javaHome)); |
| 134 | + compile.getOptions().getForkOptions().setExecutable(javaHome + "/bin/javac"); |
| 135 | + }); |
134 | 136 | List<String> args = compile.getOptions().getCompilerArgs();
|
135 | 137 | if (!args.contains("-parameters")) {
|
136 | 138 | args.add("-parameters");
|
137 | 139 | }
|
138 | 140 | });
|
139 | 141 | project.getTasks().withType(Javadoc.class, (javadoc) -> {
|
140 | 142 | javadoc.getOptions().source("1.8").encoding("UTF-8");
|
141 |
| - if (hasCustomBuildJavaHome(project)) { |
142 |
| - String javaExecutable = getCustomBuildJavaExecutable(project, "/bin/javadoc"); |
143 |
| - javadoc.setExecutable(javaExecutable); |
144 |
| - } |
| 143 | + withOptionalBuildJavaHome(project, (javaHome) -> javadoc.setExecutable(javaHome + "/bin/javadoc")); |
145 | 144 | });
|
146 | 145 | project.getTasks().withType(Test.class, (test) -> {
|
147 |
| - if (hasCustomBuildJavaHome(project)) { |
148 |
| - String javaExecutable = getCustomBuildJavaExecutable(project, "/bin/java"); |
149 |
| - test.setExecutable(javaExecutable); |
150 |
| - } |
| 146 | + withOptionalBuildJavaHome(project, (javaHome) -> test.setExecutable(javaHome + "/bin/java")); |
151 | 147 | test.useJUnitPlatform();
|
152 | 148 | test.setMaxHeapSize("1024M");
|
153 | 149 | });
|
@@ -205,12 +201,11 @@ private File createLegalFile(File source, String filename) {
|
205 | 201 | return legalFile;
|
206 | 202 | }
|
207 | 203 |
|
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; |
| 204 | + private void withOptionalBuildJavaHome(Project project, Consumer<String> consumer) { |
| 205 | + String buildJavaHome = (String) project.findProperty("buildJavaHome"); |
| 206 | + if (buildJavaHome != null && !buildJavaHome.isEmpty()) { |
| 207 | + consumer.accept(buildJavaHome); |
| 208 | + } |
214 | 209 | }
|
215 | 210 |
|
216 | 211 | private void configureSpringJavaFormat(Project project) {
|
|
0 commit comments