Skip to content

Commit 4b567d9

Browse files
authored
feat: create Configurations lazily (#996)
Gradle has powerful [configuration avoidance API](https://docs.gradle.org/current/userguide/task_configuration_avoidance.html). This is already used to great extent in this project. But configurations were still created eagerly. This commit fixes that.
1 parent 3ec9803 commit 4b567d9

File tree

1 file changed

+44
-40
lines changed

1 file changed

+44
-40
lines changed

src/main/groovy/com/github/spotbugs/snom/SpotBugsBasePlugin.java

+44-40
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
import java.util.Properties;
2121
import org.gradle.api.Plugin;
2222
import org.gradle.api.Project;
23-
import org.gradle.api.artifacts.Configuration;
24-
import org.gradle.api.artifacts.DependencySet;
23+
import org.gradle.api.artifacts.ConfigurationContainer;
24+
import org.gradle.api.artifacts.Dependency;
2525
import org.gradle.api.plugins.ReportingBasePlugin;
2626
import org.gradle.util.GradleVersion;
2727

@@ -45,7 +45,7 @@ public void apply(Project project) {
4545

4646
SpotBugsExtension extension = createExtension(project);
4747
createConfiguration(project, extension);
48-
createPluginConfiguration(project);
48+
createPluginConfiguration(project.getConfigurations());
4949

5050
String enableWorkerApi = getPropertyOrDefault(project, FEATURE_FLAG_WORKER_API, "true");
5151
String enableHybridWorker = getPropertyOrDefault(project, FEATURE_FLAG_HYBRID_WORKER, "true");
@@ -70,36 +70,39 @@ private SpotBugsExtension createExtension(Project project) {
7070
private void createConfiguration(Project project, SpotBugsExtension extension) {
7171
Properties props = loadProperties();
7272
extension.getToolVersion().convention(props.getProperty("spotbugs-version"));
73-
74-
Configuration configuration =
75-
project
76-
.getConfigurations()
77-
.create(SpotBugsPlugin.CONFIG_NAME)
78-
.setDescription("configuration for the SpotBugs engine")
79-
.setVisible(false)
80-
.setTransitive(true);
81-
82-
configuration.defaultDependencies(
83-
(DependencySet dependencies) ->
84-
dependencies.add(
85-
project
86-
.getDependencies()
87-
.create("com.github.spotbugs:spotbugs:" + extension.getToolVersion().get())));
88-
89-
Configuration spotbugsSlf4j =
90-
project
91-
.getConfigurations()
92-
.create(SpotBugsPlugin.SLF4J_CONFIG_NAME)
93-
.setDescription("configuration for the SLF4J provider to run SpotBugs")
94-
.setVisible(false)
95-
.setTransitive(true);
96-
97-
spotbugsSlf4j.defaultDependencies(
98-
(DependencySet dependencies) ->
99-
dependencies.add(
100-
project
101-
.getDependencies()
102-
.create("org.slf4j:slf4j-simple:" + props.getProperty("slf4j-version"))));
73+
ConfigurationContainer configs = project.getConfigurations();
74+
75+
configs.register(
76+
SpotBugsPlugin.CONFIG_NAME,
77+
c -> {
78+
c.setDescription("configuration for the SpotBugs engine");
79+
c.setVisible(false);
80+
c.setTransitive(true);
81+
c.defaultDependencies(
82+
d -> {
83+
Dependency dep =
84+
project
85+
.getDependencies()
86+
.create("com.github.spotbugs:spotbugs:" + extension.getToolVersion().get());
87+
d.add(dep);
88+
});
89+
});
90+
91+
configs.register(
92+
SpotBugsPlugin.SLF4J_CONFIG_NAME,
93+
c -> {
94+
c.setDescription("configuration for the SLF4J provider to run SpotBugs");
95+
c.setVisible(false);
96+
c.setTransitive(true);
97+
c.defaultDependencies(
98+
d -> {
99+
Dependency dep =
100+
project
101+
.getDependencies()
102+
.create("org.slf4j:slf4j-simple:" + props.getProperty("slf4j-version"));
103+
d.add(dep);
104+
});
105+
});
103106
}
104107

105108
Properties loadProperties() {
@@ -114,13 +117,14 @@ Properties loadProperties() {
114117
}
115118
}
116119

117-
private Configuration createPluginConfiguration(Project project) {
118-
return project
119-
.getConfigurations()
120-
.create(SpotBugsPlugin.PLUGINS_CONFIG_NAME)
121-
.setDescription("configuration for the external SpotBugs plugins")
122-
.setVisible(false)
123-
.setTransitive(false);
120+
private void createPluginConfiguration(ConfigurationContainer configs) {
121+
configs.register(
122+
SpotBugsPlugin.PLUGINS_CONFIG_NAME,
123+
c -> {
124+
c.setDescription("configuration for the external SpotBugs plugins");
125+
c.setVisible(false);
126+
c.setTransitive(false);
127+
});
124128
}
125129

126130
void verifyGradleVersion(GradleVersion version) {

0 commit comments

Comments
 (0)