Skip to content

Commit ba9913c

Browse files
committed
Use new adapter for configuring Build Cache
The new `BuildCacheConfigurationAdapter` works with more DV plugin and Gradle versions, allowing CCUD plugin to be compatible with `com.gradle.enterprise` plugins back to v3.2.
1 parent 216fead commit ba9913c

3 files changed

Lines changed: 35 additions & 51 deletions

File tree

‎src/main/java/com/gradle/CommonCustomUserDataGradlePlugin.java‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.gradle;
22

3+
import com.gradle.develocity.agent.gradle.adapters.BuildCacheConfigurationAdapter;
34
import com.gradle.develocity.agent.gradle.adapters.BuildScanAdapter;
45
import com.gradle.develocity.agent.gradle.adapters.DevelocityAdapter;
6+
import com.gradle.develocity.agent.gradle.adapters.develocity.GradleBuildCacheConfigurationAdapter;
57
import com.gradle.develocity.agent.gradle.adapters.develocity.DevelocityConfigurationAdapter;
68
import com.gradle.develocity.agent.gradle.adapters.enterprise.BuildScanExtension_1_X_Adapter;
79
import com.gradle.develocity.agent.gradle.adapters.enterprise.GradleEnterpriseExtensionAdapter;
@@ -71,14 +73,15 @@ private static void applySettingsPlugin(DevelocityAdapter develocity, ProviderFa
7173
buildScanEnhancements.apply();
7274

7375
BuildCacheConfiguration buildCache = settings.getBuildCache();
74-
customDevelocityConfig.configureBuildCache(buildCache);
76+
BuildCacheConfigurationAdapter buildCacheAdapter = new GradleBuildCacheConfigurationAdapter(buildCache);
77+
customDevelocityConfig.configureBuildCache(buildCacheAdapter);
7578

7679
// configuration changes applied in this block will override earlier configuration settings,
7780
// including those set in the settings.gradle(.kts)
7881
Action<Settings> settingsAction = __ -> {
7982
Overrides overrides = new Overrides(providers);
8083
overrides.configureDevelocity(develocity);
81-
overrides.configureBuildCache(buildCache, develocity.getBuildCache());
84+
overrides.configureBuildCache(buildCacheAdapter);
8285
};
8386

8487
// it is possible that the settings have already been evaluated by now, in which case

‎src/main/java/com/gradle/CustomDevelocityConfig.java‎

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package com.gradle;
22

3+
import com.gradle.develocity.agent.gradle.adapters.BuildCacheConfigurationAdapter;
34
import com.gradle.develocity.agent.gradle.adapters.BuildScanAdapter;
45
import com.gradle.develocity.agent.gradle.adapters.DevelocityAdapter;
5-
import org.gradle.caching.configuration.BuildCacheConfiguration;
66

77
/**
88
* Provide standardized Develocity configuration.
@@ -31,23 +31,19 @@ void configureBuildScanPublishing(BuildScanAdapter buildScan) {
3131
*/
3232
}
3333

34-
void configureBuildCache(BuildCacheConfiguration buildCache) {
35-
/* Example of build cache configuration
34+
void configureBuildCache(BuildCacheConfigurationAdapter buildCache) {
3635

36+
/* Example of build cache configuration
3737
boolean isCiServer = System.getenv().containsKey("CI");
3838
3939
// Enable the local build cache for all local and CI builds
4040
// For short-lived CI agents, it makes sense to disable the local build cache
41-
buildCache.local(local -> {
42-
local.setEnabled(true);
43-
});
41+
buildCache.getLocal().setEnabled(true);
4442
4543
// Only permit store operations to the remote build cache for CI builds
4644
// Local builds will only read from the remote build cache
47-
buildCache.remote(com.gradle.ccud.adapters.enterprise.proxies.GradleEnterpriseBuildCacheProxy.gradleEnterpriseBuildCacheClass() ,remote -> {
48-
remote.setEnabled(true);
49-
remote.setPush(isCiServer);
50-
});
45+
buildCache.getRemote().setEnabled(true);
46+
buildCache.getRemote().setStoreEnabled(isCiServer);
5147
5248
*/
5349
}

‎src/main/java/com/gradle/Overrides.java‎

Lines changed: 24 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
package com.gradle;
22

3-
import com.gradle.develocity.agent.gradle.adapters.BuildCacheAdapter;
3+
import com.gradle.develocity.agent.gradle.adapters.BuildCacheConfigurationAdapter;
4+
import com.gradle.develocity.agent.gradle.adapters.BuildCacheConfigurationAdapter.LocalBuildCacheAdapter;
5+
import com.gradle.develocity.agent.gradle.adapters.BuildCacheConfigurationAdapter.RemoteBuildCacheAdapter;
46
import com.gradle.develocity.agent.gradle.adapters.DevelocityAdapter;
5-
import com.gradle.develocity.agent.gradle.adapters.develocity.DevelocityBuildCacheAdapter;
6-
import com.gradle.develocity.agent.gradle.adapters.enterprise.GradleEnterpriseBuildCacheAdapter;
7+
78
import org.gradle.api.provider.ProviderFactory;
8-
import org.gradle.caching.configuration.AbstractBuildCache;
9-
import org.gradle.caching.configuration.BuildCacheConfiguration;
10-
import org.gradle.caching.http.HttpBuildCache;
119

1210
import java.time.Duration;
1311
import java.util.Optional;
@@ -51,43 +49,30 @@ void configureDevelocity(DevelocityAdapter develocity) {
5149
firstAvailableBooleanSysPropertyOrEnvVariable(providers, DEVELOCITY_ALLOW_UNTRUSTED_SERVER, GRADLE_ENTERPRISE_ALLOW_UNTRUSTED_SERVER).ifPresent(develocity::setAllowUntrustedServer);
5250
}
5351

54-
void configureBuildCache(BuildCacheConfiguration buildCache, Class<? extends AbstractBuildCache> develocityCacheClass) {
55-
buildCache.local(local -> {
56-
sysPropertyOrEnvVariable(LOCAL_CACHE_DIRECTORY, providers).ifPresent(local::setDirectory);
57-
durationSysPropertyOrEnvVariable(LOCAL_CACHE_REMOVE_UNUSED_ENTRIES_AFTER_DAYS, providers).ifPresent(v -> local.setRemoveUnusedEntriesAfterDays((int) v.toDays()));
58-
booleanSysPropertyOrEnvVariable(LOCAL_CACHE_ENABLED, providers).ifPresent(local::setEnabled);
59-
booleanSysPropertyOrEnvVariable(LOCAL_CACHE_PUSH, providers).ifPresent(local::setPush);
60-
});
61-
62-
// Only touch remote build cache configuration if it is already present and of type HttpBuildCache or DevelocityBuildCache
63-
// Do nothing in case of another build cache type like AWS S3 being used
64-
if (buildCache.getRemote() instanceof HttpBuildCache) {
65-
buildCache.remote(HttpBuildCache.class, remote -> {
66-
sysPropertyOrEnvVariable(REMOTE_CACHE_URL, providers).ifPresent(remote::setUrl);
67-
booleanSysPropertyOrEnvVariable(REMOTE_CACHE_ALLOW_UNTRUSTED_SERVER, providers).ifPresent(remote::setAllowUntrustedServer);
68-
booleanSysPropertyOrEnvVariable(REMOTE_CACHE_ALLOW_INSECURE_PROTOCOL, providers).ifPresent(remote::setAllowInsecureProtocol);
69-
booleanSysPropertyOrEnvVariable(REMOTE_CACHE_ENABLED, providers).ifPresent(remote::setEnabled);
70-
booleanSysPropertyOrEnvVariable(REMOTE_CACHE_PUSH, providers).ifPresent(remote::setPush);
71-
});
72-
} else if (develocityCacheClass.isInstance(buildCache.getRemote())) {
73-
buildCache.remote(develocityCacheClass, remote -> {
74-
BuildCacheAdapter adapter = createBuildCacheAdapter(remote, develocityCacheClass);
75-
sysPropertyOrEnvVariable(REMOTE_CACHE_SERVER, providers).ifPresent(adapter::setServer);
76-
sysPropertyOrEnvVariable(REMOTE_CACHE_PATH, providers).ifPresent(adapter::setPath);
77-
booleanSysPropertyOrEnvVariable(REMOTE_CACHE_ALLOW_UNTRUSTED_SERVER, providers).ifPresent(adapter::setAllowUntrustedServer);
78-
booleanSysPropertyOrEnvVariable(REMOTE_CACHE_ALLOW_INSECURE_PROTOCOL, providers).ifPresent(adapter::setAllowInsecureProtocol);
79-
booleanSysPropertyOrEnvVariable(REMOTE_CACHE_ENABLED, providers).ifPresent(adapter::setEnabled);
80-
booleanSysPropertyOrEnvVariable(REMOTE_CACHE_PUSH, providers).ifPresent(adapter::setPush);
81-
});
52+
void configureBuildCache(BuildCacheConfigurationAdapter buildCache) {
53+
configureLocalBuildCache(buildCache.getLocal());
54+
55+
RemoteBuildCacheAdapter remote = buildCache.getRemote();
56+
if (remote != null) {
57+
configureRemoteBuildCache(remote);
8258
}
8359
}
8460

85-
private static BuildCacheAdapter createBuildCacheAdapter(AbstractBuildCache cache, Class<? extends AbstractBuildCache> reportedCacheClass) {
86-
if (reportedCacheClass.getName().toLowerCase().contains("develocity")) {
87-
return new DevelocityBuildCacheAdapter(cache);
88-
}
61+
private void configureLocalBuildCache(LocalBuildCacheAdapter local) {
62+
sysPropertyOrEnvVariable(LOCAL_CACHE_DIRECTORY, providers).ifPresent(local::setDirectory);
63+
durationSysPropertyOrEnvVariable(LOCAL_CACHE_REMOVE_UNUSED_ENTRIES_AFTER_DAYS, providers).ifPresent(v -> local.setRemoveUnusedEntriesAfterDays((int) v.toDays()));
64+
booleanSysPropertyOrEnvVariable(LOCAL_CACHE_ENABLED, providers).ifPresent(local::setEnabled);
65+
booleanSysPropertyOrEnvVariable(LOCAL_CACHE_PUSH, providers).ifPresent(local::setPush);
66+
}
8967

90-
return new GradleEnterpriseBuildCacheAdapter(cache);
68+
private void configureRemoteBuildCache(RemoteBuildCacheAdapter remote) {
69+
sysPropertyOrEnvVariable(REMOTE_CACHE_URL, providers).ifPresent(remote::setUrl);
70+
sysPropertyOrEnvVariable(REMOTE_CACHE_SERVER, providers).ifPresent(remote::setServer);
71+
sysPropertyOrEnvVariable(REMOTE_CACHE_PATH, providers).ifPresent(remote::setPath);
72+
booleanSysPropertyOrEnvVariable(REMOTE_CACHE_ALLOW_UNTRUSTED_SERVER, providers).ifPresent(remote::setAllowUntrustedServer);
73+
booleanSysPropertyOrEnvVariable(REMOTE_CACHE_ALLOW_INSECURE_PROTOCOL, providers).ifPresent(remote::setAllowInsecureProtocol);
74+
booleanSysPropertyOrEnvVariable(REMOTE_CACHE_ENABLED, providers).ifPresent(remote::setEnabled);
75+
booleanSysPropertyOrEnvVariable(REMOTE_CACHE_PUSH, providers).ifPresent(remote::setPush);
9176
}
9277

9378
static Optional<String> firstAvailableSysPropertyOrEnvVariable(ProviderFactory providers, String... sysPropertyNames) {

0 commit comments

Comments
 (0)