Skip to content
This repository was archived by the owner on Feb 23, 2023. It is now read-only.

Commit d6d3c5d

Browse files
committed
Clear AOT sources only before AOT generation
Prior to this commit, AOT generated sources would be cleared as soon as the AOT plugin is applied (even if no AOT taks is invoked). This would lead to AOT-generated sources disappearing at various unexpected time during the development lifecycle of an application. This commit ensures that sources are only cleared right before the AOT generation task is invoked. Fixes gh-1552
1 parent b055eb1 commit d6d3c5d

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

spring-aot-gradle-plugin/src/main/java/org/springframework/aot/gradle/SpringAotGradlePlugin.java

+12-7
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public void apply(final Project project) {
108108
// Automatically add the spring-native dependency to the implementation configuration
109109
// as it's required for hints
110110
addSpringNativeDependency(project);
111-
Path generatedFilesPath = createGeneratedSourcesFolder(project);
111+
Path generatedFilesPath = resolveGeneratedSourcesFolder(project);
112112

113113
// deprecation replaced with new API introduced in Gradle 7.1
114114
//noinspection deprecation
@@ -117,6 +117,7 @@ public void apply(final Project project) {
117117
// Create a detached configuration that holds dependencies for AOT generation
118118
SourceSet aotMainSourceSet = createAotMainSourceSet(sourceSets, project.getConfigurations(), generatedFilesPath);
119119
GenerateAotSources generateAotSources = createGenerateAotSourcesTask(project, sourceSets);
120+
generateAotSources.doFirst(task -> clearGeneratedSourcesFolder(project));
120121
project.getTasks().named(aotMainSourceSet.getCompileJavaTaskName(), JavaCompile.class, (aotCompileJava) -> {
121122
aotCompileJava.source(generateAotSources.getSourcesOutputDirectory());
122123
});
@@ -198,19 +199,23 @@ private void addSpringNativeDependency(Project project) {
198199
}
199200

200201
/**
201-
* Recreate a folder for generated sources and resources, deleting files
202-
* possibly contributed in previous builds.
202+
* Resolve the path to the folder for generated sources and resources.
203203
*/
204-
private Path createGeneratedSourcesFolder(Project project) {
204+
private Path resolveGeneratedSourcesFolder(Project project) {
205205
String buildPath = project.getBuildDir().getAbsolutePath();
206+
return Paths.get(buildPath, "generated");
207+
}
208+
209+
private void clearGeneratedSourcesFolder(Project project) {
210+
Path generatedSourcesFolder = resolveGeneratedSourcesFolder(project);
206211
try {
207-
Path generatedSourcesFolder = Paths.get(buildPath, "generated");
208212
FileSystemUtils.deleteRecursively(generatedSourcesFolder);
209-
return Files.createDirectories(generatedSourcesFolder);
213+
Files.createDirectories(generatedSourcesFolder);
210214
}
211215
catch (IOException exc) {
212-
throw new GradleException("Failed to recreate folder '" + buildPath + "/generated/'", exc);
216+
throw new GradleException("Failed to recreate folder '" + generatedSourcesFolder + "'", exc);
213217
}
218+
214219
}
215220

216221
private SourceSet createAotMainSourceSet(SourceSetContainer sourceSets, ConfigurationContainer configurations, Path generatedFilesPath) {

0 commit comments

Comments
 (0)