Skip to content

Commit

Permalink
Consider spring.build-type when configuring the conventions
Browse files Browse the repository at this point in the history
  • Loading branch information
wilkinsona committed Sep 17, 2024
1 parent 5629b57 commit 39c4e68
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 15 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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()) {
Expand All @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) -> {
Expand Down Expand Up @@ -151,7 +187,7 @@ private void prepareMultiModuleProject(File projectDir) {

private void write(File file, Consumer<PrintWriter> 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) {
Expand Down

0 comments on commit 39c4e68

Please sign in to comment.