Skip to content

Commit d47f1d2

Browse files
committed
Polish AutoConfigurationPlugin
1 parent 5464812 commit d47f1d2

File tree

2 files changed

+98
-60
lines changed

2 files changed

+98
-60
lines changed

buildSrc/src/main/java/org/springframework/boot/build/autoconfigure/AutoConfigurationPlugin.java

Lines changed: 98 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,24 @@
1616

1717
package org.springframework.boot.build.autoconfigure;
1818

19+
import java.util.Arrays;
1920
import java.util.Collections;
20-
import java.util.Map;
21+
import java.util.Set;
22+
import java.util.stream.Collectors;
2123

2224
import org.gradle.api.Plugin;
2325
import org.gradle.api.Project;
2426
import org.gradle.api.artifacts.Configuration;
27+
import org.gradle.api.artifacts.ConfigurationContainer;
28+
import org.gradle.api.artifacts.Dependency;
29+
import org.gradle.api.plugins.JavaBasePlugin;
2530
import org.gradle.api.plugins.JavaPlugin;
2631
import org.gradle.api.plugins.JavaPluginExtension;
2732
import org.gradle.api.tasks.SourceSet;
33+
import org.gradle.api.tasks.TaskContainer;
2834
import org.gradle.api.tasks.TaskProvider;
2935

3036
import org.springframework.boot.build.DeployedPlugin;
31-
import org.springframework.boot.build.architecture.ArchitecturePlugin;
3237
import org.springframework.boot.build.optional.OptionalDependenciesPlugin;
3338

3439
/**
@@ -41,12 +46,7 @@
4146
* <li>Defines a task that produces metadata describing the auto-configuration. The
4247
* metadata is made available as an artifact in the {@code autoConfigurationMetadata}
4348
* configuration.
44-
* <li>Reacts to the {@link ArchitecturePlugin} being applied and:
45-
* <ul>
46-
* <li>Adds a rule to the {@code checkArchitectureMain} task to verify that all
47-
* {@code AutoConfiguration} classes are listed in the {@code AutoConfiguration.imports}
48-
* file.
49-
* </ul>
49+
* <li>Add checks to ensure import files and annotations are correct</li>
5050
* </ul>
5151
*
5252
* @author Andy Wilkinson
@@ -62,61 +62,101 @@ public class AutoConfigurationPlugin implements Plugin<Project> {
6262
@Override
6363
public void apply(Project project) {
6464
project.getPlugins().apply(DeployedPlugin.class);
65-
project.getPlugins().withType(JavaPlugin.class, (javaPlugin) -> {
66-
Configuration annotationProcessors = project.getConfigurations()
67-
.getByName(JavaPlugin.ANNOTATION_PROCESSOR_CONFIGURATION_NAME);
68-
SourceSet main = project.getExtensions()
65+
project.getPlugins().withType(JavaPlugin.class, (javaPlugin) -> new Configurer(project).configure());
66+
}
67+
68+
private static class Configurer {
69+
70+
private final Project project;
71+
72+
private SourceSet main;
73+
74+
Configurer(Project project) {
75+
this.project = project;
76+
this.main = project.getExtensions()
6977
.getByType(JavaPluginExtension.class)
7078
.getSourceSets()
7179
.getByName(SourceSet.MAIN_SOURCE_SET_NAME);
72-
annotationProcessors.getDependencies()
73-
.add(project.getDependencies()
74-
.project(Collections.singletonMap("path",
75-
":spring-boot-project:spring-boot-tools:spring-boot-autoconfigure-processor")));
76-
annotationProcessors.getDependencies()
77-
.add(project.getDependencies()
78-
.project(Collections.singletonMap("path",
79-
":spring-boot-project:spring-boot-tools:spring-boot-configuration-processor")));
80-
project.getTasks().register("autoConfigurationMetadata", AutoConfigurationMetadata.class, (task) -> {
81-
task.setSourceSet(main);
82-
task.dependsOn(main.getClassesTaskName());
83-
task.getOutputFile()
84-
.set(project.getLayout().getBuildDirectory().file("auto-configuration-metadata.properties"));
85-
project.getArtifacts()
86-
.add(AutoConfigurationPlugin.AUTO_CONFIGURATION_METADATA_CONFIGURATION_NAME, task.getOutputFile(),
87-
(artifact) -> artifact.builtBy(task));
88-
});
89-
project.getTasks()
90-
.register("checkAutoConfigurationImports", CheckAutoConfigurationImports.class, (task) -> {
91-
task.setSource(main.getResources());
92-
task.setClasspath(main.getOutput().getClassesDirs());
93-
task.setDescription("Checks the %s file of the main source set."
94-
.formatted(AutoConfigurationImportsTask.IMPORTS_FILE));
95-
});
96-
Configuration requiredClasspath = project.getConfigurations()
97-
.create("autoConfigurationRequiredClasspath")
98-
.extendsFrom(project.getConfigurations().getByName(main.getImplementationConfigurationName()),
99-
project.getConfigurations().getByName(main.getRuntimeOnlyConfigurationName()));
80+
}
81+
82+
void configure() {
83+
addAnnotationProcessorsDependencies();
84+
TaskContainer tasks = this.project.getTasks();
85+
ConfigurationContainer configurations = this.project.getConfigurations();
86+
tasks.register("autoConfigurationMetadata", AutoConfigurationMetadata.class,
87+
this::configureAutoConfigurationMetadata);
88+
TaskProvider<CheckAutoConfigurationImports> checkAutoConfigurationImports = tasks.register(
89+
"checkAutoConfigurationImports", CheckAutoConfigurationImports.class,
90+
this::configureCheckAutoConfigurationImports);
91+
Configuration requiredClasspath = configurations.create("autoConfigurationRequiredClasspath")
92+
.extendsFrom(configurations.getByName(this.main.getImplementationConfigurationName()),
93+
configurations.getByName(this.main.getRuntimeOnlyConfigurationName()));
10094
requiredClasspath.getDependencies()
101-
.add(project.getDependencies()
102-
.project(Map.of("path", ":spring-boot-project:spring-boot-autoconfigure")));
103-
TaskProvider<CheckAutoConfigurationClasses> checkAutoConfigurationClasses = project.getTasks()
104-
.register("checkAutoConfigurationClasses", CheckAutoConfigurationClasses.class, (task) -> {
105-
task.setSource(main.getResources());
106-
task.setClasspath(main.getOutput().getClassesDirs());
107-
task.setRequiredDependencies(requiredClasspath);
108-
task.setDescription("Checks the auto-configuration classes of the main source set.");
109-
});
110-
project.getPlugins()
95+
.add(projectDependency(":spring-boot-project:spring-boot-autoconfigure"));
96+
TaskProvider<CheckAutoConfigurationClasses> checkAutoConfigurationClasses = tasks.register(
97+
"checkAutoConfigurationClasses", CheckAutoConfigurationClasses.class,
98+
(task) -> configureCheckAutoConfigurationClasses(requiredClasspath, task));
99+
this.project.getPlugins()
111100
.withType(OptionalDependenciesPlugin.class,
112-
(plugin) -> checkAutoConfigurationClasses.configure((check) -> {
113-
Configuration optionalClasspath = project.getConfigurations()
114-
.create("autoConfigurationOptionalClassPath")
115-
.extendsFrom(project.getConfigurations()
116-
.getByName(OptionalDependenciesPlugin.OPTIONAL_CONFIGURATION_NAME));
117-
check.setOptionalDependencies(optionalClasspath);
118-
}));
119-
});
101+
(plugin) -> configureCheckAutoConfigurationClassesForOptionalDependencies(configurations,
102+
checkAutoConfigurationClasses));
103+
this.project.getTasks()
104+
.getByName(JavaBasePlugin.CHECK_TASK_NAME)
105+
.dependsOn(checkAutoConfigurationImports, checkAutoConfigurationClasses);
106+
}
107+
108+
private void addAnnotationProcessorsDependencies() {
109+
this.project.getConfigurations()
110+
.getByName(JavaPlugin.ANNOTATION_PROCESSOR_CONFIGURATION_NAME)
111+
.getDependencies()
112+
.addAll(projectDependencies(
113+
":spring-boot-project:spring-boot-tools:spring-boot-autoconfigure-processor",
114+
":spring-boot-project:spring-boot-tools:spring-boot-configuration-processor"));
115+
}
116+
117+
private void configureAutoConfigurationMetadata(AutoConfigurationMetadata task) {
118+
task.setSourceSet(this.main);
119+
task.dependsOn(this.main.getClassesTaskName());
120+
task.getOutputFile()
121+
.set(this.project.getLayout().getBuildDirectory().file("auto-configuration-metadata.properties"));
122+
this.project.getArtifacts()
123+
.add(AutoConfigurationPlugin.AUTO_CONFIGURATION_METADATA_CONFIGURATION_NAME, task.getOutputFile(),
124+
(artifact) -> artifact.builtBy(task));
125+
}
126+
127+
private void configureCheckAutoConfigurationImports(CheckAutoConfigurationImports task) {
128+
task.setSource(this.main.getResources());
129+
task.setClasspath(this.main.getOutput().getClassesDirs());
130+
task.setDescription(
131+
"Checks the %s file of the main source set.".formatted(AutoConfigurationImportsTask.IMPORTS_FILE));
132+
}
133+
134+
private void configureCheckAutoConfigurationClasses(Configuration requiredClasspath,
135+
CheckAutoConfigurationClasses task) {
136+
task.setSource(this.main.getResources());
137+
task.setClasspath(this.main.getOutput().getClassesDirs());
138+
task.setRequiredDependencies(requiredClasspath);
139+
task.setDescription("Checks the auto-configuration classes of the main source set.");
140+
}
141+
142+
private void configureCheckAutoConfigurationClassesForOptionalDependencies(
143+
ConfigurationContainer configurations,
144+
TaskProvider<CheckAutoConfigurationClasses> checkAutoConfigurationClasses) {
145+
checkAutoConfigurationClasses.configure((check) -> {
146+
Configuration optionalClasspath = configurations.create("autoConfigurationOptionalClassPath")
147+
.extendsFrom(configurations.getByName(OptionalDependenciesPlugin.OPTIONAL_CONFIGURATION_NAME));
148+
check.setOptionalDependencies(optionalClasspath);
149+
});
150+
}
151+
152+
private Set<Dependency> projectDependencies(String... paths) {
153+
return Arrays.stream(paths).map((path) -> projectDependency(path)).collect(Collectors.toSet());
154+
}
155+
156+
private Dependency projectDependency(String path) {
157+
return this.project.getDependencies().project(Collections.singletonMap("path", path));
158+
}
159+
120160
}
121161

122162
}

buildSrc/src/main/java/org/springframework/boot/build/autoconfigure/DocumentAutoConfigurationClasses.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,12 @@ private void writeTable(AutoConfiguration autoConfigurationClasses) throws IOExc
8080
writer.println("[cols=\"4,1\"]");
8181
writer.println("|===");
8282
writer.println("| Configuration Class | Links");
83-
8483
for (AutoConfigurationClass autoConfigurationClass : autoConfigurationClasses.classes) {
8584
writer.println();
8685
writer.printf("| {code-spring-boot}/spring-boot-project/%s/src/main/java/%s.java[`%s`]%n",
8786
autoConfigurationClasses.module, autoConfigurationClass.path, autoConfigurationClass.name);
8887
writer.printf("| xref:api:java/%s.html[javadoc]%n", autoConfigurationClass.path);
8988
}
90-
9189
writer.println("|===");
9290
}
9391
}

0 commit comments

Comments
 (0)