Configure build cache using Gradle API


(Mykhailo Oleksiuk) #1

Hi,

I implement a gradle plugin and in one of my task I would like to configure build cache using gradle api:

@TaskAction
configureBuildCache() {
	project.buildCache {
        remote(HttpBuildCache) {
                url = 'http://gradle-cache-url/cache/'
                push = true
        }
	}
}

But buildCache is a part of org.gradle.api.initialization.Settings and I don’t see how to get access to this object.

Is it possible to configure buildCache dynamically or just from setting.gradle only?


(Sterling Greene) #2

It can only be done from an init script, settings.gradle or a Plugin<Settings> because the build cache configuration needs to be finalized before task execution.

Here’s an example of a Plugin<Settings>: https://github.com/gradle/gradle-hazelcast-plugin/blob/master/src/main/java/org/gradle/caching/hazelcast/HazelcastPlugin.java

These are plugins like you would apply in your build.gradle, but these plugins need to be applied inside your settings.gradle. You can also apply other scripts (e.g., apply from: "somefile.gradle")–that’s what we do for Gradle: https://github.com/gradle/gradle/blob/master/gradle/remoteHttpCacheSettings.gradle

An init script is a useful way of configuring your build cache in a project-agnostic way because it can be placed in the GRADLE_USER_HOME and automatically apply to all projects: https://docs.gradle.org/4.0.2/userguide/init_scripts.html#sec:using_an_init_script


(Tim Preston) #3

@sterling, do you have an example of an init script implementation of this? Asking for a friend. :wink:


(Sterling Greene) #4

No problemo @prestontim

I thought we had already put this into the user guide for 4.0 – I opened to track it.

Here’s an example from our integration tests: