|
16 | 16 |
|
17 | 17 | package org.springframework.aot.maven;
|
18 | 18 |
|
| 19 | +import java.io.BufferedWriter; |
19 | 20 | import java.io.File;
|
20 | 21 | import java.io.IOException;
|
| 22 | +import java.nio.charset.StandardCharsets; |
21 | 23 | import java.nio.file.Files;
|
| 24 | +import java.nio.file.Path; |
22 | 25 | import java.util.List;
|
23 | 26 | import java.util.Map;
|
24 | 27 | import java.util.Optional;
|
|
43 | 46 |
|
44 | 47 | /**
|
45 | 48 | * @author Brian Clozel
|
| 49 | + * @uthor Moritz Halbritter |
46 | 50 | */
|
47 | 51 | abstract class AbstractBootstrapMojo extends AbstractMojo {
|
48 | 52 |
|
@@ -174,26 +178,78 @@ protected void recreateGeneratedSourcesFolder(File generatedSourcesFolder) throw
|
174 | 178 |
|
175 | 179 | protected void forkJvm(File workingDirectory, List<String> args, Map<String, String> environmentVariables)
|
176 | 180 | throws MojoExecutionException {
|
| 181 | + Path argFile = createArgFile(); |
177 | 182 | try {
|
| 183 | + writeArgumentsToFile(args, argFile); |
178 | 184 | RunProcess runProcess = new RunProcess(workingDirectory, getJavaExecutable());
|
179 | 185 | Runtime.getRuntime().addShutdownHook(new Thread(new RunProcessKiller(runProcess)));
|
180 |
| - int exitCode = runProcess.run(true, args, environmentVariables); |
| 186 | + int exitCode = runProcess.run(true, List.of("@" + argFile), environmentVariables); |
181 | 187 | if (exitCode == 0 || exitCode == 130) {
|
182 | 188 | return;
|
183 | 189 | }
|
184 | 190 | throw new MojoExecutionException("Bootstrap code generator finished with exit code: " + exitCode);
|
185 | 191 | }
|
186 | 192 | catch (Exception ex) {
|
| 193 | + if (getLog().isDebugEnabled()) { |
| 194 | + getLog().debug("Content of arg file:"); |
| 195 | + getLog().debug(readContent(argFile)); |
| 196 | + } |
187 | 197 | throw new MojoExecutionException("Could not exec java", ex);
|
188 | 198 | }
|
| 199 | + finally { |
| 200 | + deleteFile(argFile); |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + private String readContent(Path file) { |
| 205 | + if (!Files.exists(file)) { |
| 206 | + return "<file doesn't exist>"; |
| 207 | + } |
| 208 | + try { |
| 209 | + return Files.readString(file); |
| 210 | + } |
| 211 | + catch (IOException e) { |
| 212 | + return "<exception while reading file: " + e.getMessage() + ">"; |
| 213 | + } |
| 214 | + } |
| 215 | + |
| 216 | + private void writeArgumentsToFile(List<String> args, Path argFile) throws IOException { |
| 217 | + // See https://docs.oracle.com/en/java/javase/11/tools/java.html#GUID-4856361B-8BFD-4964-AE84-121F5F6CF111 |
| 218 | + try (BufferedWriter writer = Files.newBufferedWriter(argFile, StandardCharsets.UTF_8)) { |
| 219 | + for (String arg : args) { |
| 220 | + String escaped = arg.replace("\\", "\\\\"); |
| 221 | + writer.write('"'); |
| 222 | + writer.write(escaped); |
| 223 | + writer.write('"'); |
| 224 | + writer.newLine(); |
| 225 | + } |
| 226 | + } |
| 227 | + } |
| 228 | + |
| 229 | + private void deleteFile(Path file) { |
| 230 | + try { |
| 231 | + Files.deleteIfExists(file); |
| 232 | + } |
| 233 | + catch (IOException ex) { |
| 234 | + getLog().debug("Failed to delete file " + file, ex); |
| 235 | + } |
| 236 | + } |
| 237 | + |
| 238 | + private Path createArgFile() throws MojoExecutionException { |
| 239 | + try { |
| 240 | + return Files.createTempFile("spring-aot-maven-plugin", ".arg").toAbsolutePath(); |
| 241 | + } |
| 242 | + catch (IOException ex) { |
| 243 | + throw new MojoExecutionException("Failed to generate arg file", ex); |
| 244 | + } |
189 | 245 | }
|
190 | 246 |
|
191 | 247 | protected String getJavaExecutable() {
|
192 | 248 | Toolchain toolchain = this.toolchainManager.getToolchainFromBuildContext("jdk", this.session);
|
193 | 249 | String javaExecutable = (toolchain != null) ? toolchain.findTool("java") : null;
|
194 | 250 | return (javaExecutable != null) ? javaExecutable : new JavaExecutable().toString();
|
195 | 251 | }
|
196 |
| - |
| 252 | + |
197 | 253 | protected static final class RunProcessKiller implements Runnable {
|
198 | 254 |
|
199 | 255 | private final RunProcess runProcess;
|
|
0 commit comments