Skip to content

Commit b9010d0

Browse files
authored
Merge pull request #329 from gradle/erichaagdev/groovy-scripts-m2-directory
Add support for evaluating one or more Groovy scripts in the Develocity storage directory
2 parents f02dbca + 82281ec commit b9010d0

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. Any `*.groovy` files in the `custom-user-data` directory, located within the [Develocity storage directory](https://docs.gradle.com/develocity/maven-extension/current/#anatomy_of_the_develocity_directory), `${user.home}/.m2/.develocity` by default
64+
2. A `.mvn/develocity-custom-user-data.groovy` or `.mvn/gradle-enterprise-custom-user-data.groovy` in your root project
65+
66+
All 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 the `custom-user-data` directory 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,29 @@
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+
evaluateGroovyScriptsInDevelocityStorageDirectory(session, develocity, logger, customConfigurationSpec);
21+
evaluateGroovyScriptInRootProject(session, develocity, logger, customConfigurationSpec);
22+
}
23+
24+
private static void evaluateGroovyScriptsInDevelocityStorageDirectory(MavenSession session, DevelocityAdapter develocity, Logger logger, CustomConfigurationSpec customConfigurationSpec) throws MavenExecutionException {
25+
File customUserDataDirectory = develocity.getStorageDirectory().resolve("custom-user-data").toFile();
26+
List<File> scripts = getGroovyScripts(customUserDataDirectory);
27+
for (File script : scripts) {
28+
logger.debug("Evaluating custom user data Groovy script: {}", script);
29+
evaluateGroovyScript(session, develocity, logger, script, customConfigurationSpec.apiVariableName);
30+
}
31+
}
32+
33+
private static void evaluateGroovyScriptInRootProject(MavenSession session, DevelocityAdapter develocity, Logger logger, CustomConfigurationSpec customConfigurationSpec) throws MavenExecutionException {
1534
File script = getGroovyScript(session, customConfigurationSpec.groovyScriptName);
1635
if (script.exists()) {
1736
logger.debug("Evaluating custom user data Groovy script: {}", script);
@@ -35,6 +54,23 @@ private static File getGroovyScript(MavenSession session, String scriptName) {
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)