Skip to content

Commit e18eb08

Browse files
committed
Fix additional metadata locations with config cache
Previously, a project with a dependency on Spring Boot's configuration processor would fail to build when the configuration cache is enabled due to it accessing the Project during task execution. Instead of accessing the project during task execution, this commit updates the code to retrieve the resource locations from the matching source set in advance. The locations are then stored in the action that configures the compile task when needed. Closes gh-26880
1 parent 66bf086 commit e18eb08

File tree

3 files changed

+27
-19
lines changed

3 files changed

+27
-19
lines changed

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/JavaPluginAction.java

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.io.File;
2020
import java.util.Collections;
2121
import java.util.List;
22-
import java.util.Optional;
2322
import java.util.Set;
2423

2524
import org.gradle.api.Action;
@@ -41,6 +40,7 @@
4140
import org.gradle.api.plugins.JavaPluginExtension;
4241
import org.gradle.api.provider.Provider;
4342
import org.gradle.api.tasks.SourceSet;
43+
import org.gradle.api.tasks.SourceSetContainer;
4444
import org.gradle.api.tasks.TaskProvider;
4545
import org.gradle.api.tasks.compile.JavaCompile;
4646
import org.gradle.jvm.toolchain.JavaToolchainService;
@@ -200,7 +200,11 @@ private void configureAdditionalMetadataLocations(Project project) {
200200
}
201201

202202
private void configureAdditionalMetadataLocations(JavaCompile compile) {
203-
compile.doFirst(new AdditionalMetadataLocationsConfigurer());
203+
SourceSetContainer sourceSets = compile.getProject().getConvention().getPlugin(JavaPluginConvention.class)
204+
.getSourceSets();
205+
sourceSets.stream().filter((candidate) -> candidate.getCompileJavaTaskName().equals(compile.getName()))
206+
.map((match) -> match.getResources().getSrcDirs()).findFirst()
207+
.ifPresent((locations) -> compile.doFirst(new AdditionalMetadataLocationsConfigurer(locations)));
204208
}
205209

206210
private void configureDevelopmentOnlyConfiguration(Project project) {
@@ -230,7 +234,13 @@ private void configureDevelopmentOnlyConfiguration(Project project) {
230234
* inner-class rather than a lambda due to
231235
* https://github.com/gradle/gradle/issues/5510.
232236
*/
233-
private static class AdditionalMetadataLocationsConfigurer implements Action<Task> {
237+
private static final class AdditionalMetadataLocationsConfigurer implements Action<Task> {
238+
239+
private final Set<File> locations;
240+
241+
private AdditionalMetadataLocationsConfigurer(Set<File> locations) {
242+
this.locations = locations;
243+
}
234244

235245
@Override
236246
public void execute(Task task) {
@@ -239,8 +249,7 @@ public void execute(Task task) {
239249
}
240250
JavaCompile compile = (JavaCompile) task;
241251
if (hasConfigurationProcessorOnClasspath(compile)) {
242-
findMatchingSourceSet(compile)
243-
.ifPresent((sourceSet) -> configureAdditionalMetadataLocations(compile, sourceSet));
252+
configureAdditionalMetadataLocations(compile);
244253
}
245254
}
246255

@@ -251,15 +260,10 @@ private boolean hasConfigurationProcessorOnClasspath(JavaCompile compile) {
251260
.anyMatch((name) -> name.startsWith("spring-boot-configuration-processor"));
252261
}
253262

254-
private Optional<SourceSet> findMatchingSourceSet(JavaCompile compile) {
255-
return compile.getProject().getConvention().getPlugin(JavaPluginConvention.class).getSourceSets().stream()
256-
.filter((sourceSet) -> sourceSet.getCompileJavaTaskName().equals(compile.getName())).findFirst();
257-
}
258-
259-
private void configureAdditionalMetadataLocations(JavaCompile compile, SourceSet sourceSet) {
260-
String locations = StringUtils.collectionToCommaDelimitedString(sourceSet.getResources().getSrcDirs());
263+
private void configureAdditionalMetadataLocations(JavaCompile compile) {
261264
compile.getOptions().getCompilerArgs()
262-
.add("-Aorg.springframework.boot.configurationprocessor.additionalMetadataLocations=" + locations);
265+
.add("-Aorg.springframework.boot.configurationprocessor.additionalMetadataLocations="
266+
+ StringUtils.collectionToCommaDelimitedString(this.locations));
263267
}
264268

265269
}

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/plugin/JavaPluginActionIntegrationTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@
3131
import static org.assertj.core.api.Assertions.assertThat;
3232

3333
/**
34-
* Integration tests for {@link WarPluginAction}.
34+
* Integration tests for {@link JavaPluginAction}.
3535
*
3636
* @author Andy Wilkinson
3737
*/
38-
@GradleCompatibility
38+
@GradleCompatibility(configurationCache = true)
3939
class JavaPluginActionIntegrationTests {
4040

4141
GradleBuild gradleBuild;

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/resources/org/springframework/boot/gradle/plugin/JavaPluginActionIntegrationTests.gradle

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,13 @@ task('configurationAttributes') {
3737
}
3838

3939
task('configurationResolvabilityAndConsumability') {
40-
doFirst {
41-
def configuration = configurations.findByName(configurationName)
42-
println "canBeResolved: ${configuration.canBeResolved}"
43-
println "canBeConsumed: ${configuration.canBeConsumed}"
40+
if (project.hasProperty("configurationName")) {
41+
Configuration configuration = configurations.findByName(configurationName)
42+
def canBeResolved = configuration.canBeResolved
43+
def canBeConsumed = configuration.canBeConsumed
44+
doFirst {
45+
println "canBeResolved: ${canBeResolved}"
46+
println "canBeConsumed: ${canBeConsumed}"
47+
}
4448
}
4549
}

0 commit comments

Comments
 (0)