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
pull/27348/head
Andy Wilkinson 3 years ago
parent 66bf0866da
commit e18eb08222

@ -19,7 +19,6 @@ package org.springframework.boot.gradle.plugin;
import java.io.File;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import org.gradle.api.Action;
@ -41,6 +40,7 @@ import org.gradle.api.plugins.JavaPluginConvention;
import org.gradle.api.plugins.JavaPluginExtension;
import org.gradle.api.provider.Provider;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.SourceSetContainer;
import org.gradle.api.tasks.TaskProvider;
import org.gradle.api.tasks.compile.JavaCompile;
import org.gradle.jvm.toolchain.JavaToolchainService;
@ -200,7 +200,11 @@ final class JavaPluginAction implements PluginApplicationAction {
}
private void configureAdditionalMetadataLocations(JavaCompile compile) {
compile.doFirst(new AdditionalMetadataLocationsConfigurer());
SourceSetContainer sourceSets = compile.getProject().getConvention().getPlugin(JavaPluginConvention.class)
.getSourceSets();
sourceSets.stream().filter((candidate) -> candidate.getCompileJavaTaskName().equals(compile.getName()))
.map((match) -> match.getResources().getSrcDirs()).findFirst()
.ifPresent((locations) -> compile.doFirst(new AdditionalMetadataLocationsConfigurer(locations)));
}
private void configureDevelopmentOnlyConfiguration(Project project) {
@ -230,7 +234,13 @@ final class JavaPluginAction implements PluginApplicationAction {
* inner-class rather than a lambda due to
* https://github.com/gradle/gradle/issues/5510.
*/
private static class AdditionalMetadataLocationsConfigurer implements Action<Task> {
private static final class AdditionalMetadataLocationsConfigurer implements Action<Task> {
private final Set<File> locations;
private AdditionalMetadataLocationsConfigurer(Set<File> locations) {
this.locations = locations;
}
@Override
public void execute(Task task) {
@ -239,8 +249,7 @@ final class JavaPluginAction implements PluginApplicationAction {
}
JavaCompile compile = (JavaCompile) task;
if (hasConfigurationProcessorOnClasspath(compile)) {
findMatchingSourceSet(compile)
.ifPresent((sourceSet) -> configureAdditionalMetadataLocations(compile, sourceSet));
configureAdditionalMetadataLocations(compile);
}
}
@ -251,15 +260,10 @@ final class JavaPluginAction implements PluginApplicationAction {
.anyMatch((name) -> name.startsWith("spring-boot-configuration-processor"));
}
private Optional<SourceSet> findMatchingSourceSet(JavaCompile compile) {
return compile.getProject().getConvention().getPlugin(JavaPluginConvention.class).getSourceSets().stream()
.filter((sourceSet) -> sourceSet.getCompileJavaTaskName().equals(compile.getName())).findFirst();
}
private void configureAdditionalMetadataLocations(JavaCompile compile, SourceSet sourceSet) {
String locations = StringUtils.collectionToCommaDelimitedString(sourceSet.getResources().getSrcDirs());
private void configureAdditionalMetadataLocations(JavaCompile compile) {
compile.getOptions().getCompilerArgs()
.add("-Aorg.springframework.boot.configurationprocessor.additionalMetadataLocations=" + locations);
.add("-Aorg.springframework.boot.configurationprocessor.additionalMetadataLocations="
+ StringUtils.collectionToCommaDelimitedString(this.locations));
}
}

@ -31,11 +31,11 @@ import org.springframework.boot.gradle.testkit.GradleBuild;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Integration tests for {@link WarPluginAction}.
* Integration tests for {@link JavaPluginAction}.
*
* @author Andy Wilkinson
*/
@GradleCompatibility
@GradleCompatibility(configurationCache = true)
class JavaPluginActionIntegrationTests {
GradleBuild gradleBuild;

@ -37,9 +37,13 @@ task('configurationAttributes') {
}
task('configurationResolvabilityAndConsumability') {
if (project.hasProperty("configurationName")) {
Configuration configuration = configurations.findByName(configurationName)
def canBeResolved = configuration.canBeResolved
def canBeConsumed = configuration.canBeConsumed
doFirst {
def configuration = configurations.findByName(configurationName)
println "canBeResolved: ${configuration.canBeResolved}"
println "canBeConsumed: ${configuration.canBeConsumed}"
println "canBeResolved: ${canBeResolved}"
println "canBeConsumed: ${canBeConsumed}"
}
}
}
Loading…
Cancel
Save