Skip to content

Commit bcc27c5

Browse files
committed
Deleting existing output before running AOT processing
Closes gh-30981
1 parent 5694925 commit bcc27c5

File tree

3 files changed

+42
-8
lines changed

3 files changed

+42
-8
lines changed

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/AotGenerateMojo.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
import org.apache.maven.plugins.annotations.ResolutionScope;
4646

4747
import org.springframework.boot.loader.tools.RunProcess;
48-
import org.springframework.util.FileSystemUtils;
4948

5049
/**
5150
* Invoke the AOT engine on the application.
@@ -83,7 +82,6 @@ public class AotGenerateMojo extends AbstractRunMojo {
8382
protected void run(File workingDirectory, String startClassName, Map<String, String> environmentVariables)
8483
throws MojoExecutionException, MojoFailureException {
8584
try {
86-
deletePreviousAotAssets();
8785
generateAotAssets(workingDirectory, startClassName, environmentVariables);
8886
compileSourceFiles(getClassPathUrls());
8987
copyNativeConfiguration(this.generatedResources.toPath());
@@ -93,12 +91,6 @@ protected void run(File workingDirectory, String startClassName, Map<String, Str
9391
}
9492
}
9593

96-
private void deletePreviousAotAssets() {
97-
FileSystemUtils.deleteRecursively(this.generatedSources);
98-
FileSystemUtils.deleteRecursively(this.generatedResources);
99-
FileSystemUtils.deleteRecursively(this.generatedClasses);
100-
}
101-
10294
private void generateAotAssets(File workingDirectory, String startClassName,
10395
Map<String, String> environmentVariables) throws MojoExecutionException {
10496
List<String> args = new ArrayList<>();

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/AotProcessor.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.springframework.context.support.GenericApplicationContext;
3939
import org.springframework.javapoet.ClassName;
4040
import org.springframework.util.Assert;
41+
import org.springframework.util.FileSystemUtils;
4142

4243
/**
4344
* Entry point for AOT processing of a {@link SpringApplication}.
@@ -95,6 +96,7 @@ public AotProcessor(Class<?> application, String[] applicationArgs, Path sourceO
9596
* Trigger the processing of the application managed by this instance.
9697
*/
9798
public void process() {
99+
deleteExistingOutput();
98100
AotProcessingHook hook = new AotProcessingHook();
99101
SpringApplicationHooks.withHook(hook, this::callApplicationMainMethod);
100102
GenericApplicationContext applicationContext = hook.getApplicationContext();
@@ -103,6 +105,21 @@ public void process() {
103105
performAotProcessing(applicationContext);
104106
}
105107

108+
private void deleteExistingOutput() {
109+
deleteExistingOutput(this.sourceOutput, this.resourceOutput, this.classOutput);
110+
}
111+
112+
private void deleteExistingOutput(Path... paths) {
113+
for (Path path : paths) {
114+
try {
115+
FileSystemUtils.deleteRecursively(path);
116+
}
117+
catch (IOException ex) {
118+
throw new RuntimeException("Failed to delete existing output in '" + path + "'");
119+
}
120+
}
121+
}
122+
106123
private void callApplicationMainMethod() {
107124
try {
108125
this.application.getMethod("main", String[].class).invoke(null, new Object[] { this.applicationArgs });

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/AotProcessorTests.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.boot;
1818

19+
import java.io.IOException;
20+
import java.nio.file.Files;
1921
import java.nio.file.Path;
2022
import java.util.function.Consumer;
2123

@@ -76,6 +78,29 @@ void invokeMainWithMissingArguments() {
7678
.withMessageContaining("Usage:");
7779
}
7880

81+
@Test
82+
void processingDeletesExistingOutput(@TempDir Path directory) throws IOException {
83+
Path sourceOutput = directory.resolve("source");
84+
Path resourceOutput = directory.resolve("resource");
85+
Path classOutput = directory.resolve("class");
86+
Path existingSourceOutput = createExisting(sourceOutput);
87+
Path existingResourceOutput = createExisting(resourceOutput);
88+
Path existingClassOutput = createExisting(classOutput);
89+
AotProcessor processor = new AotProcessor(SampleApplication.class, new String[0], sourceOutput, resourceOutput,
90+
classOutput, "com.example", "example");
91+
processor.process();
92+
assertThat(existingSourceOutput).doesNotExist();
93+
assertThat(existingResourceOutput).doesNotExist();
94+
assertThat(existingClassOutput).doesNotExist();
95+
}
96+
97+
private Path createExisting(Path directory) throws IOException {
98+
Path existing = directory.resolve("existing");
99+
Files.createDirectories(directory);
100+
Files.createFile(existing);
101+
return existing;
102+
}
103+
79104
private Consumer<Path> hasGeneratedAssetsForSampleApplication() {
80105
return (directory) -> {
81106
assertThat(directory.resolve(

0 commit comments

Comments
 (0)