From 7edc9ebc2875a7fedf43ab69bf04590eb17daee7 Mon Sep 17 00:00:00 2001 From: Fouad Almalki Date: Tue, 12 Sep 2023 06:40:59 +0300 Subject: [PATCH 1/2] Lazily query attributes when copying from base configuration See gh-37343 --- .../boot/gradle/plugin/SpringBootAotPlugin.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/SpringBootAotPlugin.java b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/SpringBootAotPlugin.java index 00ba721849..3c55e245c0 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/SpringBootAotPlugin.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/SpringBootAotPlugin.java @@ -35,6 +35,7 @@ import org.gradle.api.plugins.JavaPlugin; import org.gradle.api.plugins.JavaPluginExtension; import org.gradle.api.plugins.PluginContainer; import org.gradle.api.provider.Provider; +import org.gradle.api.provider.ProviderFactory; import org.gradle.api.tasks.SourceSet; import org.gradle.api.tasks.SourceSetContainer; import org.gradle.api.tasks.TaskProvider; @@ -152,6 +153,7 @@ public class SpringBootAotPlugin implements Plugin { @SuppressWarnings("unchecked") private Configuration createAotProcessingClasspath(Project project, String taskName, SourceSet inputSourceSet) { + ProviderFactory providers = project.getProviders(); Configuration base = project.getConfigurations() .getByName(inputSourceSet.getRuntimeClasspathConfigurationName()); Configuration aotClasspath = project.getConfigurations().create(taskName + "Classpath", (classpath) -> { @@ -162,7 +164,8 @@ public class SpringBootAotPlugin implements Plugin { classpath.attributes((attributes) -> { AttributeContainer baseAttributes = base.getAttributes(); for (Attribute attribute : baseAttributes.keySet()) { - attributes.attribute((Attribute) attribute, baseAttributes.getAttribute(attribute)); + Attribute attr = (Attribute) attribute; + attributes.attributeProvider(attr, providers.provider(() -> baseAttributes.getAttribute(attr))); } }); }); From a795065555e03a5434f01ea55d642d08727eba9d Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Thu, 14 Sep 2023 10:30:49 +0100 Subject: [PATCH 2/2] Polish "Lazily query attributes when copying from base configuration" See gh-37343 --- .../boot/gradle/plugin/SpringBootAotPlugin.java | 10 +++++----- .../plugin/SpringBootAotPluginIntegrationTests.java | 10 ++++++++++ ...ConfigurationOfJavaToolchainLanguageVersion.gradle | 11 +++++++++++ 3 files changed, 26 insertions(+), 5 deletions(-) create mode 100644 spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/resources/org/springframework/boot/gradle/plugin/SpringBootAotPluginIntegrationTests-applyingAotPluginDoesNotPreventConfigurationOfJavaToolchainLanguageVersion.gradle diff --git a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/SpringBootAotPlugin.java b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/SpringBootAotPlugin.java index 3c55e245c0..a4b88c5136 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/SpringBootAotPlugin.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/SpringBootAotPlugin.java @@ -151,9 +151,8 @@ public class SpringBootAotPlugin implements Plugin { task.getArtifactId().set(project.provider(() -> project.getName())); } - @SuppressWarnings("unchecked") + @SuppressWarnings({ "unchecked", "rawtypes" }) private Configuration createAotProcessingClasspath(Project project, String taskName, SourceSet inputSourceSet) { - ProviderFactory providers = project.getProviders(); Configuration base = project.getConfigurations() .getByName(inputSourceSet.getRuntimeClasspathConfigurationName()); Configuration aotClasspath = project.getConfigurations().create(taskName + "Classpath", (classpath) -> { @@ -162,10 +161,11 @@ public class SpringBootAotPlugin implements Plugin { classpath.setDescription("Classpath of the " + taskName + " task."); removeDevelopmentOnly(base.getExtendsFrom()).forEach(classpath::extendsFrom); classpath.attributes((attributes) -> { + ProviderFactory providers = project.getProviders(); AttributeContainer baseAttributes = base.getAttributes(); - for (Attribute attribute : baseAttributes.keySet()) { - Attribute attr = (Attribute) attribute; - attributes.attributeProvider(attr, providers.provider(() -> baseAttributes.getAttribute(attr))); + for (Attribute attribute : baseAttributes.keySet()) { + attributes.attributeProvider(attribute, + providers.provider(() -> baseAttributes.getAttribute(attribute))); } }); }); diff --git a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/plugin/SpringBootAotPluginIntegrationTests.java b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/plugin/SpringBootAotPluginIntegrationTests.java index b3d9932037..aead5ebcc5 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/plugin/SpringBootAotPluginIntegrationTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/plugin/SpringBootAotPluginIntegrationTests.java @@ -23,11 +23,14 @@ import java.util.List; import org.gradle.testkit.runner.TaskOutcome; import org.junit.jupiter.api.TestTemplate; +import org.junit.jupiter.api.condition.EnabledOnJre; +import org.junit.jupiter.api.condition.JRE; import org.springframework.boot.gradle.junit.GradleCompatibility; import org.springframework.boot.testsupport.gradle.testkit.GradleBuild; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatNoException; /** * Integration tests for {@link SpringBootAotPlugin}. @@ -121,6 +124,13 @@ class SpringBootAotPluginIntegrationTests { .isEqualTo(TaskOutcome.NO_SOURCE); } + // gh-37343 + @TestTemplate + @EnabledOnJre(JRE.JAVA_17) + void applyingAotPluginDoesNotPreventConfigurationOfJavaToolchainLanguageVersion() { + assertThatNoException().isThrownBy(() -> this.gradleBuild.build("help").getOutput()); + } + private void writeMainClass(String packageName, String className) throws IOException { File java = new File(this.gradleBuild.getProjectDir(), "src/main/java/" + packageName.replace(".", "/") + "/" + className + ".java"); diff --git a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/resources/org/springframework/boot/gradle/plugin/SpringBootAotPluginIntegrationTests-applyingAotPluginDoesNotPreventConfigurationOfJavaToolchainLanguageVersion.gradle b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/resources/org/springframework/boot/gradle/plugin/SpringBootAotPluginIntegrationTests-applyingAotPluginDoesNotPreventConfigurationOfJavaToolchainLanguageVersion.gradle new file mode 100644 index 0000000000..da216cf930 --- /dev/null +++ b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/resources/org/springframework/boot/gradle/plugin/SpringBootAotPluginIntegrationTests-applyingAotPluginDoesNotPreventConfigurationOfJavaToolchainLanguageVersion.gradle @@ -0,0 +1,11 @@ +plugins { + id 'org.springframework.boot' + id 'org.springframework.boot.aot' + id 'java' +} + +java { + toolchain { + languageVersion.set(JavaLanguageVersion.of(17)) + } +}