Last active
June 18, 2025 13:26
-
-
Save jprinet/b050474697aa2b8c458ea530ac3620af to your computer and use it in GitHub Desktop.
Conditioned Develocity injection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.myorg; | |
import com.gradle.develocity.agent.maven.api.DevelocityApi; | |
import com.gradle.develocity.agent.maven.api.DevelocityListener; | |
import com.gradle.develocity.agent.maven.api.cache.BuildCacheApi; | |
import com.gradle.develocity.agent.maven.api.scan.BuildScanApi; | |
import org.apache.maven.execution.MavenSession; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
public final class ConventionDevelocityListener implements DevelocityListener { | |
private static final Logger LOGGER = LoggerFactory.getLogger(ConventionDevelocityListener.class); | |
private static final String DEVELOCITY_CONFIGURATION_FILE = ".mvn/develocity.xml"; | |
private static final String ENV_KEY_CI = "CI"; | |
private static final String ENV_KEY_CI_PROJECT_NAME = "CI_PROJECT_NAME"; | |
private static final String ENV_KEY_CI_PROJECT_NAMESPACE = "CI_PROJECT_NAMESPACE"; | |
private static final String ENV_KEY_DEVELOCITY_INJECTION = "DEVELOCITY_INJECTION"; | |
private static final String CUSTOM_VALUE_KEY_PROJECT = "project"; | |
private static final String CUSTOM_VALUE_KEY_ORG = "org"; | |
@Override | |
public void configure(DevelocityApi develocity, MavenSession session) { | |
if(isDevelocityNotConfigured()) { | |
if (isDevelocityInjectionEnabled()) { | |
LOGGER.info("************************* Develocity injection enabled *************************"); | |
configureDevelocity(develocity); | |
configureBuildCache(develocity.getBuildCache()); | |
} else { | |
LOGGER.debug("************************* Develocity injection disabled *************************"); | |
disableDevelocity(develocity); | |
} | |
} | |
} | |
private boolean isDevelocityNotConfigured() { | |
boolean isDevelocityNotConfigured = true; | |
if(new File(DEVELOCITY_CONFIGURATION_FILE).exists()) { | |
LOGGER.debug("************************* Develocity already configured at project level *************************"); | |
isDevelocityNotConfigured = false; | |
} | |
return isDevelocityNotConfigured; | |
} | |
private boolean isDevelocityInjectionEnabled() { | |
return Boolean.parseBoolean(System.getenv(ENV_KEY_DEVELOCITY_INJECTION)); | |
} | |
private void configureDevelocity(DevelocityApi develocity) { | |
develocity.setServer("https://my-develocity-url.com"); | |
configureBuildScan(develocity.getBuildScan()); | |
} | |
private void configureBuildCache(BuildCacheApi buildCache) { | |
buildCache.getRemote().setEnabled(false); | |
buildCache.getRemote().setStoreEnabled(isCi()); | |
buildCache.getLocal().setEnabled(false); | |
buildCache.getLocal().setStoreEnabled(true); | |
} | |
private static boolean isCi() { | |
return System.getenv().containsKey(ENV_KEY_CI); | |
} | |
private void configureBuildScan(BuildScanApi buildScan) { | |
buildScan.setUploadInBackground(!isCi()); | |
captureCustomValues(buildScan); | |
} | |
private void captureCustomValues(BuildScanApi buildScan) { | |
addCustomValueIfPresent(buildScan, CUSTOM_VALUE_KEY_PROJECT, ENV_KEY_CI_PROJECT_NAME); | |
addCustomValueIfPresent(buildScan, CUSTOM_VALUE_KEY_ORG, ENV_KEY_CI_PROJECT_NAMESPACE); | |
} | |
private void addCustomValueIfPresent(BuildScanApi buildScan, String key, String ref) { | |
String value = System.getenv(ref); | |
if(value != null) { | |
buildScan.value(key, value); | |
} | |
} | |
private void disableDevelocity(DevelocityApi develocity) { | |
develocity.setEnabled(false); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment