How to specify project cache dir from a gradle config

The --project-cache-dir command line option can be used to specify custom folder for the project specific cache directory.

How can this override be specified from a gradle configuration file such as build.gradle?

Afaik there is no way to do so.
What you could do, is to change the Gradle wrapper scripts to contain that option.

What you could do, is to change the Gradle wrapper scripts to contain that option.

Imagine a project build containing many folders, each having their own build.gradle and these folders getting built in an ad-hoc manner, as needed. Given the adhoc nature of these folder builds, it would be easier to build them using non wraper gradle invocation.

It would be nice to separate gradle build artifacts inside build and .gradle folders to a separate folder located in a hierarchy which is different from the source. The build folder can be relocated by setting a custom value for buildDir attribute inside build.gradle. In a similar way, it would be good to have a attribute such as projectCacheDir which could be used to relocate .gradle folder.

Imagine a project build containing many folders, each having their own build.gradle and these folders getting built in an ad-hoc manner, as needed. Given the adhoc nature of these folder builds, it would be easier to build them using non wraper gradle invocation.

I have no idea what you mean with “ad hoc” in this context.
But I have also no idea why a non wrapper gradle invocation would be easier, or I don’t know what you mean by that.
You always start a build through the Gradle wrapper, or an installed Gradle launcher (which is bad as you then might run with the wrong version), unless you launch it through the tooling API like from an IDE or similar. There the wrapper file modification would of course not be effective. But as far as I know there you don’t have a chance then.

It would be nice to separate gradle build artifacts inside build and .gradle folders to a separate folder located in a hierarchy which is different from the source.

Why?
What is so problematic?
Especially as it doesn’t matter how many projects you have in your multi-project build, the “project cache dir” is anyway always within the root project’s .gradle directory.
The build directories are indeed in each project’s directory and that is good like that imho, but that part you can configure from the build script as you found out and also was not what you asked about.

In a similar way, it would be good to have a attribute such as projectCacheDir which could be used to relocate .gradle folder.

Well, there isn’t afaik. If you really think it makes sense, you probably should open a feature request or even pull request for its addition and then post the link here for others to easily follow. :wink: