From d1810941e85e500d29dcb8c9aecce83374aed027 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 6 Sep 2022 14:09:28 +0100 Subject: [PATCH] Automatically configure annotation processing in Eclipse Closes gh-32236 --- buildSrc/build.gradle | 5 +++ .../AutoConfigurationPlugin.java | 13 ++++--- .../ConfigurationPropertiesPlugin.java | 6 ++- .../ProcessedAnnotationsPlugin.java | 38 +++++++++++++++++++ 4 files changed, 55 insertions(+), 7 deletions(-) create mode 100644 buildSrc/src/main/java/org/springframework/boot/build/processors/ProcessedAnnotationsPlugin.java diff --git a/buildSrc/build.gradle b/buildSrc/build.gradle index 74682449bb..c1e9018073 100644 --- a/buildSrc/build.gradle +++ b/buildSrc/build.gradle @@ -25,6 +25,7 @@ ext { dependencies { checkstyle "io.spring.javaformat:spring-javaformat-checkstyle:${javaFormatVersion}" implementation(platform("org.springframework:spring-framework-bom:5.3.15")) + implementation("com.diffplug.gradle:goomph:3.37.2") implementation("com.fasterxml.jackson.core:jackson-databind:2.11.4") implementation("commons-codec:commons-codec:1.13") implementation("org.apache.maven:maven-embedder:3.6.2") @@ -91,6 +92,10 @@ gradlePlugin { id = "org.springframework.boot.optional-dependencies" implementationClass = "org.springframework.boot.build.optional.OptionalDependenciesPlugin" } + processedAnnotationsPlugin { + id = "org.springframework.boot.processed-annotations" + implementationClass = "org.springframework.boot.build.processors.ProcessedAnnotationsPlugin" + } starterPlugin { id = "org.springframework.boot.starter" implementationClass = "org.springframework.boot.build.starters.StarterPlugin" diff --git a/buildSrc/src/main/java/org/springframework/boot/build/autoconfigure/AutoConfigurationPlugin.java b/buildSrc/src/main/java/org/springframework/boot/build/autoconfigure/AutoConfigurationPlugin.java index 1386d40c16..74a7542c77 100644 --- a/buildSrc/src/main/java/org/springframework/boot/build/autoconfigure/AutoConfigurationPlugin.java +++ b/buildSrc/src/main/java/org/springframework/boot/build/autoconfigure/AutoConfigurationPlugin.java @@ -57,11 +57,7 @@ public class AutoConfigurationPlugin implements Plugin { project.getPlugins().apply(DeployedPlugin.class); project.getPlugins().withType(JavaPlugin.class, (javaPlugin) -> { project.getPlugins().apply(ConfigurationPropertiesPlugin.class); - Configuration annotationProcessors = project.getConfigurations() - .getByName(JavaPlugin.ANNOTATION_PROCESSOR_CONFIGURATION_NAME); - annotationProcessors.getDependencies() - .add(project.getDependencies().project(Collections.singletonMap("path", - ":spring-boot-project:spring-boot-tools:spring-boot-autoconfigure-processor"))); + configureAutoConfigurationAnnotationProcessor(project); project.getTasks().create("autoConfigurationMetadata", AutoConfigurationMetadata.class, (task) -> { SourceSet main = project.getExtensions().getByType(JavaPluginExtension.class).getSourceSets() .getByName(SourceSet.MAIN_SOURCE_SET_NAME); @@ -74,4 +70,11 @@ public class AutoConfigurationPlugin implements Plugin { }); } + private void configureAutoConfigurationAnnotationProcessor(Project project) { + Configuration annotationProcessors = project.getConfigurations() + .getByName(JavaPlugin.ANNOTATION_PROCESSOR_CONFIGURATION_NAME); + annotationProcessors.getDependencies().add(project.getDependencies().project(Collections.singletonMap("path", + ":spring-boot-project:spring-boot-tools:spring-boot-autoconfigure-processor"))); + } + } diff --git a/buildSrc/src/main/java/org/springframework/boot/build/context/properties/ConfigurationPropertiesPlugin.java b/buildSrc/src/main/java/org/springframework/boot/build/context/properties/ConfigurationPropertiesPlugin.java index 0ce0cce310..776a210252 100644 --- a/buildSrc/src/main/java/org/springframework/boot/build/context/properties/ConfigurationPropertiesPlugin.java +++ b/buildSrc/src/main/java/org/springframework/boot/build/context/properties/ConfigurationPropertiesPlugin.java @@ -34,6 +34,7 @@ import org.gradle.api.tasks.TaskProvider; import org.gradle.api.tasks.compile.JavaCompile; import org.gradle.language.base.plugins.LifecycleBasePlugin; +import org.springframework.boot.build.processors.ProcessedAnnotationsPlugin; import org.springframework.util.StringUtils; /** @@ -76,7 +77,7 @@ public class ConfigurationPropertiesPlugin implements Plugin { @Override public void apply(Project project) { project.getPlugins().withType(JavaPlugin.class, (javaPlugin) -> { - addConfigurationProcessorDependency(project); + configureConfigurationPropertiesAnnotationProcessor(project); disableIncrementalCompilation(project); configureAdditionalMetadataLocationsCompilerArgument(project); registerCheckAdditionalMetadataTask(project); @@ -85,11 +86,12 @@ public class ConfigurationPropertiesPlugin implements Plugin { }); } - private void addConfigurationProcessorDependency(Project project) { + private void configureConfigurationPropertiesAnnotationProcessor(Project project) { Configuration annotationProcessors = project.getConfigurations() .getByName(JavaPlugin.ANNOTATION_PROCESSOR_CONFIGURATION_NAME); annotationProcessors.getDependencies().add(project.getDependencies().project(Collections.singletonMap("path", ":spring-boot-project:spring-boot-tools:spring-boot-configuration-processor"))); + project.getPlugins().apply(ProcessedAnnotationsPlugin.class); } private void disableIncrementalCompilation(Project project) { diff --git a/buildSrc/src/main/java/org/springframework/boot/build/processors/ProcessedAnnotationsPlugin.java b/buildSrc/src/main/java/org/springframework/boot/build/processors/ProcessedAnnotationsPlugin.java new file mode 100644 index 0000000000..6020c5f0bf --- /dev/null +++ b/buildSrc/src/main/java/org/springframework/boot/build/processors/ProcessedAnnotationsPlugin.java @@ -0,0 +1,38 @@ +/* + * Copyright 2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.build.processors; + +import com.diffplug.gradle.eclipse.apt.AptEclipsePlugin; +import org.gradle.api.Plugin; +import org.gradle.api.Project; +import org.gradle.plugins.ide.eclipse.model.EclipseModel; + +/** + * A plugin for a project that uses one or more annotations processors. + * + * @author Andy Wilkinson + */ +public class ProcessedAnnotationsPlugin implements Plugin { + + @Override + public void apply(Project project) { + project.getPlugins().apply(AptEclipsePlugin.class); + project.getExtensions().getByType(EclipseModel.class).synchronizationTasks("eclipseJdtApt", "eclipseJdt", + "eclipseFactorypath"); + } + +}