16
16
17
17
package org .springframework .boot .build .autoconfigure ;
18
18
19
+ import java .util .Arrays ;
19
20
import java .util .Collections ;
20
- import java .util .Map ;
21
+ import java .util .Set ;
22
+ import java .util .stream .Collectors ;
21
23
22
24
import org .gradle .api .Plugin ;
23
25
import org .gradle .api .Project ;
24
26
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 ;
25
30
import org .gradle .api .plugins .JavaPlugin ;
26
31
import org .gradle .api .plugins .JavaPluginExtension ;
27
32
import org .gradle .api .tasks .SourceSet ;
33
+ import org .gradle .api .tasks .TaskContainer ;
28
34
import org .gradle .api .tasks .TaskProvider ;
29
35
30
36
import org .springframework .boot .build .DeployedPlugin ;
31
- import org .springframework .boot .build .architecture .ArchitecturePlugin ;
32
37
import org .springframework .boot .build .optional .OptionalDependenciesPlugin ;
33
38
34
39
/**
41
46
* <li>Defines a task that produces metadata describing the auto-configuration. The
42
47
* metadata is made available as an artifact in the {@code autoConfigurationMetadata}
43
48
* 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>
50
50
* </ul>
51
51
*
52
52
* @author Andy Wilkinson
@@ -62,61 +62,101 @@ public class AutoConfigurationPlugin implements Plugin<Project> {
62
62
@ Override
63
63
public void apply (Project project ) {
64
64
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 ()
69
77
.getByType (JavaPluginExtension .class )
70
78
.getSourceSets ()
71
79
.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 ()));
100
94
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 ()
111
100
.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
+
120
160
}
121
161
122
162
}
0 commit comments