Skip to content

Commit

Permalink
Support com.gradle.develocity plugin in place of com.gradle.enterprise
Browse files Browse the repository at this point in the history
Closes gh-74
  • Loading branch information
wilkinsona committed Apr 5, 2024
1 parent 634754a commit e830ee8
Show file tree
Hide file tree
Showing 12 changed files with 340 additions and 230 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ The username and password environment variables should be set using the `gradle_

## Build scan conventions

When applied alongside the [Gradle Enterprise Plugin](https://plugins.gradle.org/plugin/com.gradle.enterprise), the plugin will configure publishing of build scans to [ge.spring.io](https://ge.spring.io) when authenticated.
When applied alongside the [Develocity Plugin](https://plugins.gradle.org/plugin/com.gradle.develocity), the plugin will configure publishing of build scans to [ge.spring.io](https://ge.spring.io) when authenticated.
The build scans will be customized to:

- Add tags:
Expand All @@ -59,7 +59,7 @@ The build scans will be customized to:
- Add links:
- `CI build` when building on Bamboo, GitHub Actions, or Jenkins, linking to the build on the CI server.
- `Git commit build scans`, linking to scans for other builds of the same git commit.
- Enable capturing of task (Gradle) or goal (Maven) input files
- Enable capturing of file fingerprints
- Upload build scans in the foreground when running on CI

### Build scan publishing credentials
Expand Down Expand Up @@ -125,12 +125,12 @@ pluginManagement {

In the example above, `gradlePluginPortal()` is declared to allow other plugins to continue to be resolved from the portal.

Now apply the plugin in `settings.gradle`, alongside the `com.gradle.enterprise` plugin:
Now apply the plugin in `settings.gradle`, alongside the `com.gradle.develocity` plugin:

```groovy
plugins {
// …
id "com.gradle.enterprise" version "<<version>>"
id "com.gradle.develocity" version "<<version>>"
id "io.spring.ge.conventions" version "<<version>>"
// …
}
Expand Down
21 changes: 2 additions & 19 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ plugins {
id "io.spring.javaformat" version "$javaFormatVersion"
id "java-gradle-plugin"
id "maven-publish"
id "io.spring.compatibility-test" version "0.0.2"
}

description = "Gradle Enterprise Conventions Gradle plugin"
Expand All @@ -17,16 +16,12 @@ repositories {
gradlePluginPortal()
}

configurations {
compatibilityTestImplementation
}

dependencies {
checkstyle("io.spring.javaformat:spring-javaformat-checkstyle:${javaFormatVersion}")

compileOnly("com.gradle:gradle-enterprise-gradle-plugin:${gradleEnterprisePluginVersion}")
compileOnly("com.gradle:develocity-gradle-plugin:${gradleEnterprisePluginVersion}")

testImplementation("com.gradle:gradle-enterprise-gradle-plugin:${gradleEnterprisePluginVersion}")
testImplementation("com.gradle:develocity-gradle-plugin:${gradleEnterprisePluginVersion}")
testImplementation("org.assertj:assertj-core:3.24.2")
testImplementation("org.junit.jupiter:junit-jupiter:5.10.0")
testImplementation("org.mockito:mockito-core:4.11.0")
Expand All @@ -43,18 +38,6 @@ gradlePlugin {
}
}

compatibilityTest {
useJUnitPlatform()
dependency('Gradle Enterprise Plugin') { gePlugin ->
gePlugin.groupId = 'com.gradle'
gePlugin.artifactId = 'gradle-enterprise-gradle-plugin'
gePlugin.versions = [
'3.15.1',
'3.14.1'
]
}
}

tasks.withType(Test) { testTask ->
if (testTask.name.startsWith("compatibilityTest_")) {
testTask.filter {
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version=0.0.17-SNAPSHOT

gradleEnterprisePluginVersion=3.16.1
gradleEnterprisePluginVersion=3.17
javaFormatVersion=0.0.39
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020-2021 the original author or authors.
* Copyright 2020-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,7 +18,8 @@

import java.util.Map;

import com.gradle.scan.plugin.BuildScanExtension;
import com.gradle.develocity.agent.gradle.DevelocityConfiguration;
import com.gradle.develocity.agent.gradle.scan.BuildScanConfiguration;

/**
* Conventions for build scans that are published anonymously to
Expand All @@ -28,16 +29,17 @@
*/
public class AnonymousPublicationBuildScanConventions extends BuildScanConventions {

public AnonymousPublicationBuildScanConventions(ProcessRunner processRunner, Map<String, String> env) {
super(processRunner, env);
public AnonymousPublicationBuildScanConventions(DevelocityConfiguration develocity, ProcessRunner processRunner,
Map<String, String> env) {
super(develocity, processRunner, env);
}

public AnonymousPublicationBuildScanConventions(ProcessRunner processRunner) {
super(processRunner);
public AnonymousPublicationBuildScanConventions(DevelocityConfiguration develocity, ProcessRunner processRunner) {
super(develocity, processRunner);
}

@Override
protected void configurePublishing(BuildScanExtension buildScan) {
protected void configurePublishing(BuildScanConfiguration buildScan) {
// Use Gradle's defaults
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,34 @@
import java.util.function.Consumer;
import java.util.stream.Collectors;

import com.gradle.scan.plugin.BuildScanExtension;
import com.gradle.develocity.agent.gradle.DevelocityConfiguration;
import com.gradle.develocity.agent.gradle.scan.BuildScanConfiguration;
import org.gradle.api.Action;

/**
* {@link Action} that configures the {@link BuildScanExtension build scan} with Spring
* conventions.
* {@link Action} that configures the {@link BuildScanConfiguration build scan} with
* Spring conventions.
*
* @author Andy Wilkinson
*/
class BuildScanConventions implements Action<BuildScanExtension> {
class BuildScanConventions implements Action<BuildScanConfiguration> {

private static final String BAMBOO_RESULTS_ENV_VAR = "bamboo_resultsUrl";

private static final String CIRCLECI_BUILD_URL_ENV_VAR = "CIRCLE_BUILD_URL";

private final Map<String, String> env;
private final DevelocityConfiguration develocity;

private final ProcessRunner processRunner;

BuildScanConventions(ProcessRunner processRunner) {
this(processRunner, System.getenv());
private final Map<String, String> env;

BuildScanConventions(DevelocityConfiguration develocity, ProcessRunner processRunner) {
this(develocity, processRunner, System.getenv());
}

BuildScanConventions(ProcessRunner processRunner, Map<String, String> env) {
BuildScanConventions(DevelocityConfiguration develocity, ProcessRunner processRunner, Map<String, String> env) {
this.develocity = develocity;
this.processRunner = processRunner;
this.env = env;
}
Expand All @@ -56,21 +60,15 @@ class BuildScanConventions implements Action<BuildScanExtension> {
* @param buildScan build scan to be configured
*/
@Override
@SuppressWarnings("deprecation")
public void execute(BuildScanExtension buildScan) {
public void execute(BuildScanConfiguration buildScan) {
buildScan.obfuscation((obfuscation) -> obfuscation
.ipAddresses((addresses) -> addresses.stream().map((address) -> "0.0.0.0").collect(Collectors.toList())));
configurePublishing(buildScan);
tagBuildScan(buildScan);
buildScan.background(this::addGitMetadata);
addCiMetadata(buildScan);
buildScan.setUploadInBackground(!isCi());
try {
buildScan.capture((settings) -> settings.setTaskInputFiles(true));
}
catch (NoSuchMethodError ex) {
buildScan.setCaptureTaskInputFiles(true);
}
buildScan.getUploadInBackground().set(!isCi());
buildScan.capture((settings) -> settings.getFileFingerprints().set(true));
}

/**
Expand All @@ -79,24 +77,18 @@ public void execute(BuildScanExtension buildScan) {
* {@code https://ge.spring.io}.
* @param buildScan build scan to configure
*/
protected void configurePublishing(BuildScanExtension buildScan) {
buildScan.publishAlways();
try {
buildScan.getClass().getMethod("publishIfAuthenticated").invoke(buildScan);
}
catch (Exception ex) {
throw new RuntimeException("Failed to invoke publishIfAuthenticated()", ex);
}
buildScan.setServer("https://ge.spring.io");
protected void configurePublishing(BuildScanConfiguration buildScan) {
buildScan.publishing((publishing) -> publishing.onlyIf((context) -> context.isAuthenticated()));
this.develocity.getServer().set("https://ge.spring.io");
}

private void tagBuildScan(BuildScanExtension buildScan) {
private void tagBuildScan(BuildScanConfiguration buildScan) {
tagCiOrLocal(buildScan);
tagJdk(buildScan);
tagOperatingSystem(buildScan);
}

private void tagCiOrLocal(BuildScanExtension buildScan) {
private void tagCiOrLocal(BuildScanConfiguration buildScan) {
buildScan.tag(isCi() ? "CI" : "Local");
}

Expand Down Expand Up @@ -127,23 +119,24 @@ private boolean isGitHubActions() {
return this.env.containsKey("GITHUB_ACTIONS");
}

private void tagJdk(BuildScanExtension buildScan) {
private void tagJdk(BuildScanConfiguration buildScan) {
buildScan.tag("JDK-" + getJdkVersion());
}

protected String getJdkVersion() {
return System.getProperty("java.specification.version");
}

private void tagOperatingSystem(BuildScanExtension buildScan) {
private void tagOperatingSystem(BuildScanConfiguration buildScan) {
buildScan.tag(System.getProperty("os.name"));
}

private void addGitMetadata(BuildScanExtension buildScan) {
private void addGitMetadata(BuildScanConfiguration buildScan) {
run("git", "rev-parse", "--short=8", "--verify", "HEAD").standardOut((gitCommitId) -> {
String commitIdLabel = "Git commit";
buildScan.value(commitIdLabel, gitCommitId);
String server = buildScan.getServer();

String server = this.develocity.getServer().getOrNull();
if (server != null) {
buildScan.link("Git commit build scans", server + createSearchUrl(commitIdLabel, gitCommitId));
}
Expand All @@ -158,7 +151,7 @@ private void addGitMetadata(BuildScanExtension buildScan) {
});
}

private void addCiMetadata(BuildScanExtension buildScan) {
private void addCiMetadata(BuildScanConfiguration buildScan) {
if (isBamboo()) {
buildScan.link("CI build", this.env.get(BAMBOO_RESULTS_ENV_VAR));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@

import javax.inject.Inject;

import com.gradle.enterprise.gradleplugin.GradleEnterpriseExtension;
import com.gradle.enterprise.gradleplugin.GradleEnterprisePlugin;
import com.gradle.scan.plugin.BuildScanExtension;
import com.gradle.develocity.agent.gradle.DevelocityConfiguration;
import com.gradle.develocity.agent.gradle.DevelocityPlugin;
import com.gradle.develocity.agent.gradle.scan.BuildScanConfiguration;
import org.gradle.StartParameter;
import org.gradle.api.Plugin;
import org.gradle.api.initialization.Settings;
Expand All @@ -45,9 +45,9 @@ public GradleEnterpriseConventionsPlugin(ProcessOperations processOperations) {

@Override
public void apply(Settings settings) {
settings.getPlugins().withType(GradleEnterprisePlugin.class, (plugin) -> {
GradleEnterpriseExtension extension = settings.getExtensions().getByType(GradleEnterpriseExtension.class);
configureBuildScanConventions(extension.getBuildScan(), settings.getStartParameter(),
settings.getPlugins().withType(DevelocityPlugin.class, (plugin) -> {
DevelocityConfiguration extension = settings.getExtensions().getByType(DevelocityConfiguration.class);
configureBuildScanConventions(extension, extension.getBuildScan(), settings.getStartParameter(),
settings.getRootDir());
});
if (settings.getStartParameter().isBuildCacheEnabled()) {
Expand All @@ -56,15 +56,15 @@ public void apply(Settings settings) {
}
}

private void configureBuildScanConventions(BuildScanExtension buildScan, StartParameter startParameter,
File rootDir) {
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()) {
new AnonymousPublicationBuildScanConventions(processRunner) {
new AnonymousPublicationBuildScanConventions(develocity, processRunner) {

@Override
protected String getJdkVersion() {
Expand All @@ -75,7 +75,7 @@ protected String getJdkVersion() {
}.execute(buildScan);
}
else {
new BuildScanConventions(processRunner) {
new BuildScanConventions(develocity, processRunner) {

@Override
protected String getJdkVersion() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020-2021 the original author or authors.
* Copyright 2020-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -29,19 +29,20 @@ class AnonymousPublicationBuildScanConventionsTests {

private final TestProcessRunner processRunner = new TestProcessRunner();

private final TestBuildScanExtension buildScan = new TestBuildScanExtension();
private final TestDevelocityConfiguration develocity = new TestDevelocityConfiguration();

private final TestBuildScanConfiguration buildScan = new TestBuildScanConfiguration();

@Test
void buildScansAreConfiguredToUseDefaultPublicationBehaviour() {
new AnonymousPublicationBuildScanConventions(this.processRunner).execute(this.buildScan);
assertThat(this.buildScan.publishAlways).isFalse();
assertThat(this.buildScan.publishIfAuthenticated).isFalse();
new AnonymousPublicationBuildScanConventions(this.develocity, this.processRunner).execute(this.buildScan);
assertThat(this.buildScan.publishing.predicate).isNull();
}

@Test
void buildScansAreConfiguredToPublishToDefaultServer() {
new AnonymousPublicationBuildScanConventions(this.processRunner).execute(this.buildScan);
assertThat(this.buildScan.server).isNull();
new AnonymousPublicationBuildScanConventions(this.develocity, this.processRunner).execute(this.buildScan);
assertThat(this.develocity.getServer().getOrNull()).isNull();
}

}
Loading

0 comments on commit e830ee8

Please sign in to comment.