From 39c4e681b8f5829fc97a072b4102c24ace74f260 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 17 Sep 2024 09:24:57 +0100 Subject: [PATCH] Consider spring.build-type when configuring the conventions --- README.md | 5 ++ .../gradle/DevelocityConventionsPlugin.java | 57 ++++++++++++++----- ...cityConventionsPluginIntegrationTests.java | 38 ++++++++++++- 3 files changed, 85 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index b26cb55..6ac9651 100644 --- a/README.md +++ b/README.md @@ -130,6 +130,11 @@ plugins { } ``` +The behavior of the conventions can be controlled with a `spring.build-type` property in `gradle.properties`. +If the property is present, it must have a value of `oss` in order to publish build scans and use the build cache. +Any other value will disable both build scan publishing and the build cache. +For example, you may want to update `gradle.properties` and set `spring.build-type` to `cve` when working on a fix for a security vulnerability. + ### Maven To use the conventions, create a `.mvn/extensions.xml` file in the root of the project: diff --git a/develocity-conventions-gradle-plugin/src/main/java/io/spring/develocity/conventions/gradle/DevelocityConventionsPlugin.java b/develocity-conventions-gradle-plugin/src/main/java/io/spring/develocity/conventions/gradle/DevelocityConventionsPlugin.java index b370b3b..f813b8b 100644 --- a/develocity-conventions-gradle-plugin/src/main/java/io/spring/develocity/conventions/gradle/DevelocityConventionsPlugin.java +++ b/develocity-conventions-gradle-plugin/src/main/java/io/spring/develocity/conventions/gradle/DevelocityConventionsPlugin.java @@ -17,6 +17,11 @@ package io.spring.develocity.conventions.gradle; import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.io.Reader; +import java.io.UncheckedIOException; +import java.util.Properties; import javax.inject.Inject; @@ -49,19 +54,52 @@ public DevelocityConventionsPlugin(ProcessOperations processOperations) { public void apply(Settings settings) { settings.getPlugins().apply(DevelocityPlugin.class); DevelocityConfiguration extension = settings.getExtensions().getByType(DevelocityConfiguration.class); - configureBuildScanConventions(extension, extension.getBuildScan(), settings.getStartParameter(), - settings.getRootDir()); + if (!isOssBuild(settings)) { + extension + .buildScan((buildScan) -> buildScan.publishing((publishing) -> publishing.onlyIf((context) -> false))); + return; + } + if (isBuildScanEnabled(settings)) { + configureBuildScanConventions(extension, extension.getBuildScan(), settings.getStartParameter(), + settings.getRootDir()); + } if (settings.getStartParameter().isBuildCacheEnabled()) { settings.buildCache((buildCacheConfiguration) -> new BuildCacheConventions() .execute(new GradleConfigurableBuildCache(extension.getBuildCache(), buildCacheConfiguration))); } } + private boolean isOssBuild(Settings settings) { + Properties properties = new Properties(); + File propertiesFile = new File(settings.getRootDir(), "gradle.properties"); + if (propertiesFile.exists()) { + try (Reader reader = new FileReader(propertiesFile)) { + properties.load(reader); + } + catch (IOException ex) { + throw new UncheckedIOException(ex); + } + } + String buildType = properties.getProperty("spring.build-type"); + return buildType == null || "oss".equals(buildType); + } + + private boolean isBuildScanEnabled(Settings settings) { + StartParameter startParameter = settings.getStartParameter(); + return !startParameter.isNoBuildScan() && !containsPropertiesTask(startParameter); + } + + private boolean containsPropertiesTask(StartParameter startParameter) { + for (String taskName : startParameter.getTaskNames()) { + if (taskName.equals("properties") || taskName.endsWith(":properties")) { + return true; + } + } + return false; + } + private void configureBuildScanConventions(DevelocityConfiguration develocity, BuildScanConfiguration buildScan, StartParameter startParameter, File rootDir) { - if (startParameter.isNoBuildScan() || containsPropertiesTask(startParameter)) { - return; - } ProcessOperationsProcessRunner processRunner = new ProcessOperationsProcessRunner( new WorkingDirectoryProcessOperations(this.processOperations, rootDir)); if (startParameter.isBuildScan()) { @@ -88,13 +126,4 @@ protected String getJdkVersion() { } } - private boolean containsPropertiesTask(StartParameter startParameter) { - for (String taskName : startParameter.getTaskNames()) { - if (taskName.equals("properties") || taskName.endsWith(":properties")) { - return true; - } - } - return false; - } - } diff --git a/develocity-conventions-gradle-plugin/src/test/java/io/spring/develocity/conventions/gradle/DevelocityConventionsPluginIntegrationTests.java b/develocity-conventions-gradle-plugin/src/test/java/io/spring/develocity/conventions/gradle/DevelocityConventionsPluginIntegrationTests.java index 37396b4..db39a92 100644 --- a/develocity-conventions-gradle-plugin/src/test/java/io/spring/develocity/conventions/gradle/DevelocityConventionsPluginIntegrationTests.java +++ b/develocity-conventions-gradle-plugin/src/test/java/io/spring/develocity/conventions/gradle/DevelocityConventionsPluginIntegrationTests.java @@ -67,6 +67,25 @@ void whenThePluginIsAppliedAndBuildScansAreDisabledThenBuildScanConventionsAreNo assertThat(result.getOutput()).contains("Capture task input files: false"); } + @Test + void whenThePluginIsAppliedAndTheSpringBuildTypeIsNotOssThenBuildScanConventionsAreNotApplied( + @TempDir File projectDir) { + prepareProject(projectDir); + write(new File(projectDir, "gradle.properties"), (writer) -> writer.println("spring.build-type=other")); + BuildResult result = build(projectDir, "6.0.1", "verifyBuildScanConfig"); + assertThat(result.getOutput()).contains("Build scan server: null"); + assertThat(result.getOutput()).contains("Capture task input files: false"); + } + + @Test + void whenThePluginIsAppliedAndTheSpringBuildTypeIsOssThenBuildScanConventionsAreApplied(@TempDir File projectDir) { + prepareProject(projectDir); + write(new File(projectDir, "gradle.properties"), (writer) -> writer.println("spring.build-type=oss")); + BuildResult result = build(projectDir, "6.0.1", "verifyBuildScanConfig"); + assertThat(result.getOutput()).contains("Build scan server: https://ge.spring.io"); + assertThat(result.getOutput()).contains("Capture task input files: true"); + } + @Test void whenThePluginIsAppliedAndPropertiesTaskIsExecutedThenBuildScanConventionsAreNotApplied( @TempDir File projectDir) { @@ -100,6 +119,23 @@ void whenThePluginIsAppliedAndBuildCacheIsDisabledThenBuildCacheConventionsAreNo assertThat(result.getOutput()).contains("Build cache server: null"); } + @Test + void whenThePluginIsAppliedAndTheSpringBuildTypeIsNotOssThenBuildCacheConventionsAreNotApplied( + @TempDir File projectDir) { + prepareProject(projectDir); + write(new File(projectDir, "gradle.properties"), (writer) -> writer.println("spring.build-type=other")); + BuildResult result = build(projectDir, "6.0.1", "verifyBuildCacheConfig"); + assertThat(result.getOutput()).contains("Build cache server: null"); + } + + @Test + void whenThePluginIsAppliedAndTheSpringBuildTypeIsOssThenBuildCacheConventionsAreApplied(@TempDir File projectDir) { + prepareProject(projectDir); + write(new File(projectDir, "gradle.properties"), (writer) -> writer.println("spring.build-type=oss")); + BuildResult result = build(projectDir, "6.0.1", "verifyBuildCacheConfig"); + assertThat(result.getOutput()).contains("Build cache server: https://ge.spring.io"); + } + private void prepareProject(File projectDir) { write(new File(projectDir, "gradle.properties"), (writer) -> writer.println("org.gradle.caching=true")); write(new File(projectDir, "settings.gradle"), (writer) -> { @@ -151,7 +187,7 @@ private void prepareMultiModuleProject(File projectDir) { private void write(File file, Consumer consumer) { file.getParentFile().mkdirs(); - try (PrintWriter writer = new PrintWriter(new FileWriter(file))) { + try (PrintWriter writer = new PrintWriter(new FileWriter(file, true))) { consumer.accept(writer); } catch (IOException ex) {