Configure build cache using Gradle API

(Mykhailo Oleksiuk) #1


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

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>:

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:

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:

(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: