Skip to content

Commit

Permalink
feat: create Configurations lazily (#996)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
beatbrot authored Oct 15, 2023
1 parent 3ec9803 commit 4b567d9
Showing 1 changed file with 44 additions and 40 deletions.
84 changes: 44 additions & 40 deletions src/main/groovy/com/github/spotbugs/snom/SpotBugsBasePlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
import java.util.Properties;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.DependencySet;
import org.gradle.api.artifacts.ConfigurationContainer;
import org.gradle.api.artifacts.Dependency;
import org.gradle.api.plugins.ReportingBasePlugin;
import org.gradle.util.GradleVersion;

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

SpotBugsExtension extension = createExtension(project);
createConfiguration(project, extension);
createPluginConfiguration(project);
createPluginConfiguration(project.getConfigurations());

String enableWorkerApi = getPropertyOrDefault(project, FEATURE_FLAG_WORKER_API, "true");
String enableHybridWorker = getPropertyOrDefault(project, FEATURE_FLAG_HYBRID_WORKER, "true");
Expand All @@ -70,36 +70,39 @@ private SpotBugsExtension createExtension(Project project) {
private void createConfiguration(Project project, SpotBugsExtension extension) {
Properties props = loadProperties();
extension.getToolVersion().convention(props.getProperty("spotbugs-version"));

Configuration configuration =
project
.getConfigurations()
.create(SpotBugsPlugin.CONFIG_NAME)
.setDescription("configuration for the SpotBugs engine")
.setVisible(false)
.setTransitive(true);

configuration.defaultDependencies(
(DependencySet dependencies) ->
dependencies.add(
project
.getDependencies()
.create("com.github.spotbugs:spotbugs:" + extension.getToolVersion().get())));

Configuration spotbugsSlf4j =
project
.getConfigurations()
.create(SpotBugsPlugin.SLF4J_CONFIG_NAME)
.setDescription("configuration for the SLF4J provider to run SpotBugs")
.setVisible(false)
.setTransitive(true);

spotbugsSlf4j.defaultDependencies(
(DependencySet dependencies) ->
dependencies.add(
project
.getDependencies()
.create("org.slf4j:slf4j-simple:" + props.getProperty("slf4j-version"))));
ConfigurationContainer configs = project.getConfigurations();

configs.register(
SpotBugsPlugin.CONFIG_NAME,
c -> {
c.setDescription("configuration for the SpotBugs engine");
c.setVisible(false);
c.setTransitive(true);
c.defaultDependencies(
d -> {
Dependency dep =
project
.getDependencies()
.create("com.github.spotbugs:spotbugs:" + extension.getToolVersion().get());
d.add(dep);
});
});

configs.register(
SpotBugsPlugin.SLF4J_CONFIG_NAME,
c -> {
c.setDescription("configuration for the SLF4J provider to run SpotBugs");
c.setVisible(false);
c.setTransitive(true);
c.defaultDependencies(
d -> {
Dependency dep =
project
.getDependencies()
.create("org.slf4j:slf4j-simple:" + props.getProperty("slf4j-version"));
d.add(dep);
});
});
}

Properties loadProperties() {
Expand All @@ -114,13 +117,14 @@ Properties loadProperties() {
}
}

private Configuration createPluginConfiguration(Project project) {
return project
.getConfigurations()
.create(SpotBugsPlugin.PLUGINS_CONFIG_NAME)
.setDescription("configuration for the external SpotBugs plugins")
.setVisible(false)
.setTransitive(false);
private void createPluginConfiguration(ConfigurationContainer configs) {
configs.register(
SpotBugsPlugin.PLUGINS_CONFIG_NAME,
c -> {
c.setDescription("configuration for the external SpotBugs plugins");
c.setVisible(false);
c.setTransitive(false);
});
}

void verifyGradleVersion(GradleVersion version) {
Expand Down

0 comments on commit 4b567d9

Please sign in to comment.