Skip to content

Commit 2c511ae

Browse files
committed
Add support for evaluating one or more Groovy scripts in the Develocity storage directory
This change enables a user to place one or more Groovy scripts in the Develocity storage directory for evaluation. Previously, only a single script at a fixed location of `.mvn/develocity-custom-user-data.groovy` in the project root could be evaluated. This change supports users who need to configure Develocity for any Maven build running on a given machine, without the need to create a custom extension.
1 parent f02dbca commit 2c511ae

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,13 @@ captured and under which conditions.
5757

5858
## Capturing additional tags, links and values in your build scans
5959

60-
You can apply additional configuration beyond what is contributed by the Common Custom User Data Maven extension by default. The additional configuration happens in a specific
61-
Groovy script. This is a good intermediate step before creating your own extension.
60+
You can apply additional configuration beyond what is contributed by the Common Custom User Data Maven extension by default.
61+
The extension evaluates Groovy scripts from two locations:
6262

63-
The Common Custom User Data Maven extension checks for a `.mvn/develocity-custom-user-data.groovy` or `.mvn/gradle-enterprise-custom-user-data.groovy` Groovy script in your root project. If the file exists, it evaluates
64-
the script with the following bindings:
63+
1. A `.mvn/develocity-custom-user-data.groovy` or `.mvn/gradle-enterprise-custom-user-data.groovy` in your root project
64+
2. Any files matching `*.groovy` in the directory `~/.mvn/.develocity/custom-user-data/`
65+
66+
Any matching files are evaluated with the following bindings:
6567

6668
- `develocity/gradleEnterprise` (type: [DevelocityAdapter](https://github.com/gradle/develocity-agent-adapters/blob/main/develocity-maven-extension-adapters/src/compatibilityApi/java/com/gradle/develocity/agent/maven/adapters/DevelocityAdapter.java)): _configure Develocity_
6769
- `buildScan` (type: [BuildScanApiAdapter](https://github.com/gradle/develocity-agent-adapters/blob/main/develocity-maven-extension-adapters/src/compatibilityApi/java/com/gradle/develocity/agent/maven/adapters/BuildScanApiAdapter.java)): _configure build scan publishing and enhance build scans_
@@ -70,6 +72,8 @@ the script with the following bindings:
7072
- `project` (type: [`MavenProject`](https://maven.apache.org/ref/current/maven-core/apidocs/org/apache/maven/project/MavenProject.html)): _the top-level Maven project_
7173
- `session` (type: [`MavenSession`](https://maven.apache.org/ref/current/maven-core/apidocs/org/apache/maven/execution/MavenSession.html)): _the Maven session_
7274

75+
The Groovy scripts are evaluated in the exact order listed above, with the scripts in `~/.mvn/.develocity/custom-user-data/` being executed in alphabetical order.
76+
7377
See [here](.mvn/develocity-custom-user-data.groovy) for an example.
7478

7579
## Developing a customized version of the extension

src/main/java/com/gradle/GroovyScriptUserData.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,20 @@
88
import org.slf4j.Logger;
99

1010
import java.io.File;
11+
import java.util.Arrays;
12+
import java.util.Collections;
13+
import java.util.Comparator;
14+
import java.util.List;
15+
import java.util.stream.Collectors;
1116

1217
final class GroovyScriptUserData {
1318

1419
static void evaluate(MavenSession session, DevelocityAdapter develocity, Logger logger, CustomConfigurationSpec customConfigurationSpec) throws MavenExecutionException {
20+
evaluateGroovyScriptInRootProject(session, develocity, logger, customConfigurationSpec);
21+
evaluateGroovyScriptsInDevelocityStorageDirectory(session, develocity, logger, customConfigurationSpec);
22+
}
23+
24+
private static void evaluateGroovyScriptInRootProject(MavenSession session, DevelocityAdapter develocity, Logger logger, CustomConfigurationSpec customConfigurationSpec) throws MavenExecutionException {
1525
File script = getGroovyScript(session, customConfigurationSpec.groovyScriptName);
1626
if (script.exists()) {
1727
logger.debug("Evaluating custom user data Groovy script: {}", script);
@@ -30,11 +40,37 @@ static void evaluate(MavenSession session, DevelocityAdapter develocity, Logger
3040
}
3141
}
3242

43+
private static void evaluateGroovyScriptsInDevelocityStorageDirectory(MavenSession session, DevelocityAdapter develocity, Logger logger, CustomConfigurationSpec customConfigurationSpec) throws MavenExecutionException {
44+
File customUserDataDirectory = develocity.getStorageDirectory().resolve("custom-user-data").toFile();
45+
List<File> scripts = getGroovyScripts(customUserDataDirectory);
46+
for (File script : scripts) {
47+
logger.debug("Evaluating custom user data Groovy script: {}", script);
48+
evaluateGroovyScript(session, develocity, logger, script, customConfigurationSpec.apiVariableName);
49+
}
50+
}
51+
3352
private static File getGroovyScript(MavenSession session, String scriptName) {
3453
File rootDir = session.getRequest().getMultiModuleProjectDirectory();
3554
return new File(rootDir, ".mvn/" + scriptName + ".groovy");
3655
}
3756

57+
private static List<File> getGroovyScripts(File directory) {
58+
if (directory.exists() && directory.isDirectory()) {
59+
File[] files = directory.listFiles();
60+
if (files != null) {
61+
return Arrays.stream(files)
62+
.filter(GroovyScriptUserData::isGroovyScript)
63+
.sorted(Comparator.comparing(File::getName))
64+
.collect(Collectors.toList());
65+
}
66+
}
67+
return Collections.emptyList();
68+
}
69+
70+
private static boolean isGroovyScript(File file) {
71+
return file.isFile() && file.getName().endsWith(".groovy");
72+
}
73+
3874
private static void evaluateGroovyScript(MavenSession session, DevelocityAdapter develocity, Logger logger, File groovyScript, String apiVariableName) throws MavenExecutionException {
3975
try {
4076
Binding binding = prepareBinding(session, develocity, logger, apiVariableName);

0 commit comments

Comments
 (0)