Skip to content

Commit 1ced0d6

Browse files
jthurneerichaagdevbdemers
authored
Add support for the renamed Develocity Maven Extension (#624)
The Gradle Enterprise Maven Extension was renamed to the Develocity Maven Extension in version 1.21. The API was also updated for the product name change. Unfortunately, this broke compatibility with the Build Validation Scripts. This commit fixes the incompatibility. The fix works by updating the configure-gradle-enterprise-maven-extension to define and register two listeners: a GradleEnterpriseListener and a DevelocityListener. If an older version of the Gradle Enterprise Maven Extension is used, it will load the GradleEnterpriseListener. If a newer version of the Develocity Maven Extension is used, then it will load the DevelocityListener. The Develocity Maven Extension Adapters are used to eliminate duplication between the two different listeners. This commit also creates a "fat jar" so that the develocity-maven-extention-adapters jar does not have to be manually added to the classpath. The project compiles agianst an older version of Maven to ensure compatibility with projects using an older version of Maven. **Remove unused variables and suppress deprecation warnings** Unused warnings were previously suppressed because the main classes aren't used directly. However, this suppressed other unused warnings and hid a few things that are not actually used. So this commit un-supresses unused warnings and removes the unused variables and parameters. The attempt to suppress deprecation warnings on the ConfigureGradleEnterprise class wasn't working because the deprecated classes are referenced in import statements. The class was updated to use the fully qualified name of the deprecated classes. --------- Signed-off-by: Jim Hurne <[email protected]> Co-authored-by: Eric Haag <[email protected]> Co-authored-by: Brian Demers <[email protected]>
1 parent 196dc5c commit 1ced0d6

File tree

8 files changed

+172
-118
lines changed

8 files changed

+172
-118
lines changed

Diff for: .github/dependabot.yml

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ updates:
1414
# Update only patch version of GE Maven extension
1515
- dependency-name: "com.gradle:gradle-enterprise-maven-extension"
1616
update-types: ["version-update:semver-major", "version-update:semver-minor"]
17+
# intentionally compiling against an older version to preserve compatibility with older versions of Maven
18+
- dependency-name: "org.apache.maven:maven-core"
1719
schedule:
1820
interval: "daily"
1921
time: "02:00"

Diff for: build.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ val mavenComponents by configurations.creating
4848
dependencies {
4949
argbash("argbash:argbash:2.10.0@zip")
5050
commonComponents(project(path = ":fetch-build-scan-data-cmdline-tool", configuration = "shadow"))
51-
mavenComponents(project(":configure-gradle-enterprise-maven-extension"))
51+
mavenComponents(project(path = ":configure-gradle-enterprise-maven-extension", configuration = "shadow"))
5252
mavenComponents("com.gradle:gradle-enterprise-maven-extension:1.18.4")
5353
mavenComponents("com.gradle:common-custom-user-data-maven-extension:1.13")
5454
}

Diff for: components/configure-gradle-enterprise-maven-extension/build.gradle.kts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
plugins {
22
id("java")
3+
id("com.github.johnrengelman.shadow") version "8.1.1"
34
}
45

56
repositories {
67
mavenCentral()
78
}
89

910
dependencies {
10-
compileOnly("org.apache.maven:maven-core:3.9.7")
11+
compileOnly("org.apache.maven:maven-core:3.6.3") // intentionally compiling against an older version to preserve compatibility with older versions of Maven
1112
compileOnly("org.codehaus.plexus:plexus-component-annotations:2.2.0")
12-
compileOnly("com.gradle:gradle-enterprise-maven-extension:1.18.4")
13+
compileOnly("com.gradle:develocity-maven-extension:1.21.4")
14+
implementation("com.gradle:develocity-maven-extension-adapters:1.0")
1315
}
1416

1517
description = "Maven extension to capture the build scan URL"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.gradle;
2+
3+
import com.gradle.develocity.agent.maven.adapters.develocity.DevelocityApiAdapter;
4+
import com.gradle.develocity.agent.maven.api.DevelocityApi;
5+
import com.gradle.develocity.agent.maven.api.DevelocityListener;
6+
import org.apache.maven.execution.MavenSession;
7+
8+
import javax.inject.Inject;
9+
10+
public class ConfigureDevelocity implements DevelocityListener {
11+
12+
private final ConfigureDevelocityAdaptor configureDevelocityAdaptor;
13+
14+
@Inject
15+
public ConfigureDevelocity(ConfigureDevelocityAdaptor configureDevelocityAdaptor, RootProjectExtractor rootProjectExtractor) {
16+
this.configureDevelocityAdaptor = configureDevelocityAdaptor;
17+
}
18+
19+
@Override
20+
public void configure(DevelocityApi api, MavenSession session) {
21+
configureDevelocityAdaptor.configure(new DevelocityApiAdapter(api), session);
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package com.gradle;
2+
3+
import com.gradle.develocity.agent.maven.adapters.BuildScanApiAdapter;
4+
import com.gradle.develocity.agent.maven.adapters.DevelocityAdapter;
5+
import org.apache.maven.execution.MavenSession;
6+
import org.codehaus.plexus.logging.Logger;
7+
8+
import javax.inject.Inject;
9+
import java.io.File;
10+
import java.io.IOException;
11+
import java.io.UnsupportedEncodingException;
12+
import java.net.URI;
13+
import java.net.URLEncoder;
14+
import java.nio.charset.StandardCharsets;
15+
import java.nio.file.Files;
16+
17+
import static java.lang.Boolean.parseBoolean;
18+
import static java.nio.file.StandardOpenOption.*;
19+
20+
public class ConfigureDevelocityAdaptor {
21+
22+
private static final String EXPERIMENT_DIR = System.getProperty("com.gradle.enterprise.build-validation.expDir");
23+
24+
private final RootProjectExtractor rootProjectExtractor;
25+
private final Logger logger;
26+
27+
@Inject
28+
public ConfigureDevelocityAdaptor(RootProjectExtractor rootProjectExtractor, Logger logger) {
29+
this.rootProjectExtractor = rootProjectExtractor;
30+
this.logger = logger;
31+
}
32+
33+
public void configure(DevelocityAdapter api, MavenSession session) {
34+
logger.debug("Configuring build scan published event...");
35+
36+
BuildScanApiAdapter buildScan = api.getBuildScan();
37+
38+
String geUrl = System.getProperty("gradle.enterprise.url");
39+
String geAllowUntrustedServer = System.getProperty("gradle.enterprise.allowUntrustedServer");
40+
41+
if (geUrl != null && !geUrl.isEmpty()) {
42+
buildScan.setServer(geUrl);
43+
}
44+
if (geAllowUntrustedServer != null && !geAllowUntrustedServer.isEmpty()) {
45+
buildScan.setAllowUntrustedServer(Boolean.parseBoolean(geAllowUntrustedServer));
46+
}
47+
48+
String rootProjectName = rootProjectExtractor.extractRootProject(session).getName();
49+
50+
registerBuildScanActions(buildScan, rootProjectName);
51+
configureBuildScanPublishing(buildScan);
52+
}
53+
54+
private static void registerBuildScanActions(BuildScanApiAdapter buildScan, String rootProjectName) {
55+
buildScan.buildFinished(buildResult -> {
56+
// communicate via error file that no GE server is set
57+
boolean omitServerUrlValidation = parseBoolean(System.getProperty("com.gradle.enterprise.build-validation.omitServerUrlValidation"));
58+
if (buildScan.getServer() == null && !omitServerUrlValidation) {
59+
buildScan.publishAlwaysIf(false); // disable publishing, otherwise scans.gradle.com will be used
60+
File errorFile = new File(EXPERIMENT_DIR, "errors.txt");
61+
append(errorFile, "The Gradle Enterprise server URL has not been configured in the project or on the command line.");
62+
}
63+
});
64+
65+
buildScan.buildFinished(buildResult -> {
66+
String expId = System.getProperty("com.gradle.enterprise.build-validation.expId");
67+
addCustomValueAndSearchLink(buildScan, "Experiment id", expId);
68+
buildScan.tag(expId);
69+
70+
String runId = System.getProperty("com.gradle.enterprise.build-validation.runId");
71+
addCustomValueAndSearchLink(buildScan, "Experiment run id", runId);
72+
73+
String scriptsVersion = System.getProperty("com.gradle.enterprise.build-validation.scriptsVersion");
74+
buildScan.value("Build validation scripts", scriptsVersion);
75+
});
76+
77+
buildScan.buildScanPublished(scan -> {
78+
String runNum = System.getProperty("com.gradle.enterprise.build-validation.runNum");
79+
URI buildScanUri = scan.getBuildScanUri();
80+
String buildScanId = scan.getBuildScanId();
81+
String port = buildScanUri.getPort() != -1 ? ":" + buildScanUri.getPort() : "";
82+
String baseUrl = String.format("%s://%s%s", buildScanUri.getScheme(), buildScanUri.getHost(), port);
83+
84+
File scanFile = new File(EXPERIMENT_DIR, "build-scans.csv");
85+
append(scanFile, String.format("%s,%s,%s,%s,%s\n", runNum, rootProjectName, baseUrl, buildScanUri, buildScanId));
86+
});
87+
}
88+
89+
private static void configureBuildScanPublishing(BuildScanApiAdapter buildScan) {
90+
buildScan.publishAlways();
91+
buildScan.capture(t -> t.setGoalInputFiles(true)); // also set via sys prop
92+
buildScan.setUploadInBackground(false);
93+
}
94+
95+
private static void addCustomValueAndSearchLink(BuildScanApiAdapter buildScan, String label, String value) {
96+
buildScan.value(label, value);
97+
if (buildScan.getServer() != null) {
98+
String server = buildScan.getServer();
99+
String searchParams = "search.names=" + urlEncode(label) + "&search.values=" + urlEncode(value);
100+
String url = appendIfMissing(server, "/") + "scans?" + searchParams + "#selection.buildScanB=" + urlEncode("{SCAN_ID}");
101+
buildScan.link(label + " build scans", url);
102+
}
103+
}
104+
105+
private static String appendIfMissing(String str, String suffix) {
106+
return str.endsWith(suffix) ? str : str + suffix;
107+
}
108+
109+
private static String urlEncode(String str) {
110+
try {
111+
return URLEncoder.encode(str, StandardCharsets.UTF_8.name());
112+
} catch (UnsupportedEncodingException e) {
113+
throw new RuntimeException(e);
114+
}
115+
}
116+
117+
private static void append(File file, String text) {
118+
try {
119+
Files.write(file.toPath(), text.getBytes(), CREATE, WRITE, APPEND);
120+
} catch (IOException e) {
121+
throw new RuntimeException(String.format("Unable to write to file %s: %s", file.getName(), e.getMessage()), e);
122+
}
123+
}
124+
125+
}
Original file line numberDiff line numberDiff line change
@@ -1,128 +1,23 @@
11
package com.gradle;
22

3-
import com.gradle.maven.extension.api.GradleEnterpriseApi;
4-
import com.gradle.maven.extension.api.GradleEnterpriseListener;
5-
import com.gradle.maven.extension.api.scan.BuildScanApi;
3+
import com.gradle.develocity.agent.maven.adapters.enterprise.GradleEnterpriseApiAdapter;
64
import org.apache.maven.execution.MavenSession;
7-
import org.codehaus.plexus.logging.Logger;
85

96
import javax.inject.Inject;
10-
import java.io.File;
11-
import java.io.IOException;
12-
import java.io.UnsupportedEncodingException;
13-
import java.net.URI;
14-
import java.net.URLEncoder;
15-
import java.nio.charset.StandardCharsets;
16-
import java.nio.file.Files;
177

18-
import static java.lang.Boolean.parseBoolean;
19-
import static java.nio.file.StandardOpenOption.*;
8+
// Using fully qualified class names to avoid deprecation warnings on import statements
9+
@SuppressWarnings({"deprecation"})
10+
public class ConfigureGradleEnterprise implements com.gradle.maven.extension.api.GradleEnterpriseListener {
2011

21-
@SuppressWarnings("unused")
22-
public class ConfigureGradleEnterprise implements GradleEnterpriseListener {
23-
24-
private static final String EXPERIMENT_DIR = System.getProperty("com.gradle.enterprise.build-validation.expDir");
25-
26-
private final RootProjectExtractor rootProjectExtractor;
27-
private final Logger logger;
12+
private final ConfigureDevelocityAdaptor configureDevelocityAdaptor;
2813

2914
@Inject
30-
public ConfigureGradleEnterprise(RootProjectExtractor rootProjectExtractor, Logger logger) {
31-
this.rootProjectExtractor = rootProjectExtractor;
32-
this.logger = logger;
15+
public ConfigureGradleEnterprise(ConfigureDevelocityAdaptor configureDevelocityAdaptor) {
16+
this.configureDevelocityAdaptor = configureDevelocityAdaptor;
3317
}
3418

3519
@Override
36-
public void configure(GradleEnterpriseApi api, MavenSession session) {
37-
logger.debug("Configuring build scan published event...");
38-
39-
BuildScanApi buildScan = api.getBuildScan();
40-
41-
String geUrl = System.getProperty("gradle.enterprise.url");
42-
String geAllowUntrustedServer = System.getProperty("gradle.enterprise.allowUntrustedServer");
43-
44-
if (geUrl != null && !geUrl.isEmpty()) {
45-
buildScan.setServer(geUrl);
46-
}
47-
if (geAllowUntrustedServer != null && !geAllowUntrustedServer.isEmpty()) {
48-
buildScan.setAllowUntrustedServer(Boolean.parseBoolean(geAllowUntrustedServer));
49-
}
50-
51-
String rootProjectName = rootProjectExtractor.extractRootProject(session).getName();
52-
53-
registerBuildScanActions(buildScan, rootProjectName);
54-
configureBuildScanPublishing(buildScan);
20+
public void configure(com.gradle.maven.extension.api.GradleEnterpriseApi api, MavenSession session) {
21+
configureDevelocityAdaptor.configure(new GradleEnterpriseApiAdapter(api), session);
5522
}
56-
57-
private static void registerBuildScanActions(BuildScanApi buildScan, String rootProjectName) {
58-
buildScan.buildFinished(buildResult -> {
59-
// communicate via error file that no GE server is set
60-
boolean omitServerUrlValidation = parseBoolean(System.getProperty("com.gradle.enterprise.build-validation.omitServerUrlValidation"));
61-
if (buildScan.getServer() == null && !omitServerUrlValidation) {
62-
buildScan.publishAlwaysIf(false); // disable publishing, otherwise scans.gradle.com will be used
63-
File errorFile = new File(EXPERIMENT_DIR, "errors.txt");
64-
append(errorFile, "The Gradle Enterprise server URL has not been configured in the project or on the command line.");
65-
}
66-
});
67-
68-
buildScan.buildFinished(buildResult -> {
69-
String expId = System.getProperty("com.gradle.enterprise.build-validation.expId");
70-
addCustomValueAndSearchLink(buildScan, "Experiment id", expId);
71-
buildScan.tag(expId);
72-
73-
String runId = System.getProperty("com.gradle.enterprise.build-validation.runId");
74-
addCustomValueAndSearchLink(buildScan, "Experiment run id", runId);
75-
76-
String scriptsVersion = System.getProperty("com.gradle.enterprise.build-validation.scriptsVersion");
77-
buildScan.value("Build validation scripts", scriptsVersion);
78-
});
79-
80-
buildScan.buildScanPublished(scan -> {
81-
String runNum = System.getProperty("com.gradle.enterprise.build-validation.runNum");
82-
URI buildScanUri = scan.getBuildScanUri();
83-
String buildScanId = scan.getBuildScanId();
84-
String port = buildScanUri.getPort() != -1 ? ":" + buildScanUri.getPort() : "";
85-
String baseUrl = String.format("%s://%s%s", buildScanUri.getScheme(), buildScanUri.getHost(), port);
86-
87-
File scanFile = new File(EXPERIMENT_DIR, "build-scans.csv");
88-
append(scanFile, String.format("%s,%s,%s,%s,%s\n", runNum, rootProjectName, baseUrl, buildScanUri, buildScanId));
89-
});
90-
}
91-
92-
private static void configureBuildScanPublishing(BuildScanApi buildScan) {
93-
buildScan.publishAlways();
94-
buildScan.capture(t -> t.setGoalInputFiles(true)); // also set via sys prop
95-
buildScan.setUploadInBackground(false);
96-
}
97-
98-
private static void addCustomValueAndSearchLink(BuildScanApi buildScan, String label, String value) {
99-
buildScan.value(label, value);
100-
if (buildScan.getServer() != null) {
101-
String server = buildScan.getServer();
102-
String searchParams = "search.names=" + urlEncode(label) + "&search.values=" + urlEncode(value);
103-
String url = appendIfMissing(server, "/") + "scans?" + searchParams + "#selection.buildScanB=" + urlEncode("{SCAN_ID}");
104-
buildScan.link(label + " build scans", url);
105-
}
106-
}
107-
108-
private static String appendIfMissing(String str, String suffix) {
109-
return str.endsWith(suffix) ? str : str + suffix;
110-
}
111-
112-
private static String urlEncode(String str) {
113-
try {
114-
return URLEncoder.encode(str, StandardCharsets.UTF_8.name());
115-
} catch (UnsupportedEncodingException e) {
116-
throw new RuntimeException(e);
117-
}
118-
}
119-
120-
private static void append(File file, String text) {
121-
try {
122-
Files.write(file.toPath(), text.getBytes(), CREATE, WRITE, APPEND);
123-
} catch (IOException e) {
124-
throw new RuntimeException(String.format("Unable to write to file %s: %s", file.getName(), e.getMessage()), e);
125-
}
126-
}
127-
12823
}

Diff for: components/configure-gradle-enterprise-maven-extension/src/main/resources/META-INF/plexus/components.xml

+7
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,12 @@
77
<description>Configures Gradle Enterprise</description>
88
<isolated-realm>false</isolated-realm>
99
</component>
10+
<component>
11+
<role>com.gradle.develocity.agent.maven.api.DevelocityListener</role>
12+
<role-hint>configure-develocity</role-hint>
13+
<implementation>com.gradle.ConfigureDevelocity</implementation>
14+
<description>Configures Develocity</description>
15+
<isolated-realm>false</isolated-realm>
16+
</component>
1017
</components>
1118
</component-set>

Diff for: components/scripts/lib/maven.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env bash
22

3-
readonly CONFIGURE_GRADLE_ENTERPRISE_JAR="${LIB_DIR}/maven-libs/configure-gradle-enterprise-maven-extension-${SCRIPT_VERSION}.jar"
3+
readonly CONFIGURE_GRADLE_ENTERPRISE_JAR="${LIB_DIR}/maven-libs/configure-gradle-enterprise-maven-extension-${SCRIPT_VERSION}-all.jar"
44

55
find_maven_executable() {
66
if [ -f "./mvnw" ]; then

0 commit comments

Comments
 (0)