Skip to content

Commit

Permalink
Allow configuring build cache through the DevelocityConfigurable
Browse files Browse the repository at this point in the history
  • Loading branch information
erichaagdev committed Aug 12, 2024
1 parent 29ea9cb commit f8468ff
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ void configureDevelocity(DevelocityConfigurable develocity) {
// CHANGE ME: Apply your Develocity configuration here
develocity.setServer("https://develocity-samples.gradle.com");
configureBuildScan(develocity.getBuildScan());
configureBuildCache(develocity.getBuildCache());
}

private void configureBuildScan(BuildScanConfigurable buildScan) {
// CHANGE ME: Apply your Build Scan configuration here
buildScan.setUploadInBackground(!isCi());
}

void configureBuildCache(BuildCacheConfigurable buildCache) {
private void configureBuildCache(BuildCacheConfigurable buildCache) {
// CHANGE ME: Apply your Build Cache configuration here
buildCache.getLocal().setEnabled(true);
buildCache.getLocal().setStoreEnabled(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ public interface DevelocityConfigurable {

BuildScanConfigurable getBuildScan();

BuildCacheConfigurable getBuildCache();

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.gradle.CommonCustomUserDataGradlePlugin;
import com.gradle.develocity.agent.gradle.DevelocityConfiguration;
import com.gradle.develocity.agent.gradle.DevelocityPlugin;
import com.myorg.configurable.GradleBuildCacheConfigurable;
import com.myorg.configurable.GradleDevelocityConfigurable;
import org.gradle.api.GradleException;
import org.gradle.api.Plugin;
Expand Down Expand Up @@ -41,20 +40,15 @@ public void apply(Object target) {
private void configureGradle6OrNewer(Settings settings) {
settings.getPluginManager().apply(DevelocityPlugin.class);
settings.getPluginManager().apply(CommonCustomUserDataGradlePlugin.class);

DevelocityConfiguration develocity = settings.getExtensions().getByType(DevelocityConfiguration.class);
DevelocityConventions develocityConventions = new DevelocityConventions();
develocityConventions.configureDevelocity(new GradleDevelocityConfigurable(develocity));
develocityConventions.configureBuildCache(new GradleBuildCacheConfigurable(develocity.getBuildCache(), settings.getBuildCache()));
new DevelocityConventions().configureDevelocity(new GradleDevelocityConfigurable(develocity, settings.getBuildCache()));
}

private void configureGradle5(Project project) {
project.getPluginManager().apply(DevelocityPlugin.class);
project.getPluginManager().apply(CommonCustomUserDataGradlePlugin.class);

DevelocityConventions develocityConventions = new DevelocityConventions();
develocityConventions.configureDevelocity(new GradleDevelocityConfigurable(project.getExtensions().getByType(DevelocityConfiguration.class)));
// develocityConventions.configureBuildCache is not called because the build cache cannot be configured via a plugin prior to Gradle 6.0
DevelocityConfiguration develocity = project.getExtensions().getByType(DevelocityConfiguration.class);
new DevelocityConventions().configureDevelocity(new GradleDevelocityConfigurable(develocity));
}

private static boolean isGradle6OrNewer() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.gradle.develocity.agent.gradle.buildcache.DevelocityBuildCache;
import org.gradle.caching.configuration.BuildCacheConfiguration;

public final class GradleBuildCacheConfigurable implements BuildCacheConfigurable {
final class GradleBuildCacheConfigurable implements BuildCacheConfigurable {

private final LocalBuildCacheConfigurable localBuildCache;
private final RemoteBuildCacheConfigurable remoteBuildCache;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
package com.myorg.configurable;

import com.gradle.develocity.agent.gradle.DevelocityConfiguration;
import org.gradle.caching.configuration.BuildCacheConfiguration;

public final class GradleDevelocityConfigurable implements DevelocityConfigurable {

private final DevelocityConfiguration develocity;
private final BuildScanConfigurable buildScan;
private final BuildCacheConfigurable buildCache;

public GradleDevelocityConfigurable(DevelocityConfiguration develocity) {
this.develocity = develocity;
this.buildScan = new GradleBuildScanConfigurable(develocity.getBuildScan());
this.buildCache = new NoopBuildCacheConfigurable();
}

public GradleDevelocityConfigurable(DevelocityConfiguration develocity, BuildCacheConfiguration buildCache) {
this.develocity = develocity;
this.buildScan = new GradleBuildScanConfigurable(develocity.getBuildScan());
this.buildCache = new GradleBuildCacheConfigurable(develocity.getBuildCache(), buildCache);
}

@Override
Expand Down Expand Up @@ -37,4 +46,9 @@ public BuildScanConfigurable getBuildScan() {
return buildScan;
}

@Override
public BuildCacheConfigurable getBuildCache() {
return buildCache;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.myorg.configurable;

/*
* Build cache cannot be configured via a plugin prior to Gradle 6.0, so build
* cache operations must be a no-op.
*/
final class NoopBuildCacheConfigurable implements BuildCacheConfigurable {

@Override
public LocalBuildCacheConfigurable getLocal() {
return new NoopLocalBuildCacheConfigurable();
}

@Override
public RemoteBuildCacheConfigurable getRemote() {
return new NoopRemoteBuildCacheConfigurable();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.myorg.configurable;

import java.io.File;

/*
* Build cache cannot be configured via a plugin prior to Gradle 6.0, so build
* cache operations must be a no-op.
*/
final class NoopLocalBuildCacheConfigurable implements LocalBuildCacheConfigurable {

@Override
public void setEnabled(boolean enabled) {

}

@Override
public void setStoreEnabled(boolean storeEnabled) {

}

@Override
public void setDirectory(File directory) {

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.myorg.configurable;

/*
* Build cache cannot be configured via a plugin prior to Gradle 6.0, so build
* cache operations must be a no-op.
*/
final class NoopRemoteBuildCacheConfigurable implements RemoteBuildCacheConfigurable {

@Override
public void setEnabled(boolean enabled) {

}

@Override
public void setStoreEnabled(boolean storeEnabled) {

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.gradle.develocity.agent.maven.api.DevelocityApi;
import com.gradle.develocity.agent.maven.api.DevelocityListener;
import com.myorg.configurable.MavenBuildCacheConfigurable;
import com.myorg.configurable.MavenDevelocityConfigurable;
import org.apache.maven.execution.MavenSession;
import org.codehaus.plexus.component.annotations.Component;
Expand All @@ -19,9 +18,7 @@ final class ConventionDevelocityMavenExtensionListener implements DevelocityList

@Override
public void configure(DevelocityApi develocity, MavenSession session) {
DevelocityConventions develocityConventions = new DevelocityConventions();
develocityConventions.configureDevelocity(new MavenDevelocityConfigurable(develocity));
develocityConventions.configureBuildCache(new MavenBuildCacheConfigurable(develocity.getBuildCache()));
new DevelocityConventions().configureDevelocity(new MavenDevelocityConfigurable(develocity));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.gradle.develocity.agent.maven.api.cache.BuildCacheApi;

public final class MavenBuildCacheConfigurable implements BuildCacheConfigurable {
final class MavenBuildCacheConfigurable implements BuildCacheConfigurable {

private final LocalBuildCacheConfigurable localBuildCache;
private final RemoteBuildCacheConfigurable remoteBuildCache;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ public final class MavenDevelocityConfigurable implements DevelocityConfigurable

private final DevelocityApi develocity;
private final BuildScanConfigurable buildScan;
private final BuildCacheConfigurable buildCache;

public MavenDevelocityConfigurable(DevelocityApi develocity) {
this.develocity = develocity;
this.buildScan = new MavenBuildScanConfigurable(develocity.getBuildScan());
this.buildCache = new MavenBuildCacheConfigurable(develocity.getBuildCache());
}

@Override
Expand Down Expand Up @@ -37,4 +39,9 @@ public BuildScanConfigurable getBuildScan() {
return buildScan;
}

@Override
public BuildCacheConfigurable getBuildCache() {
return buildCache;
}

}

0 comments on commit f8468ff

Please sign in to comment.