Latest point at which projectCacheDir can be set

I am looking to override gradle.startParameter.projectCacheDir programmatically. I was looking to do it somewhere in the initscript, but I was wondering what is the latest point at which the cacheDir can be changed to still have effect.

The three potential solutions I had in mind in the initscript was either as simple as

  gradle.startParameter.projectCacheDir=new File('/foo')

or alternatively via a plugin that gets applied within the initscript:

  allprojects {
    apply plugin : 'my.custom.plugin'  // implements  Plugin<Project>

I am not to sure about the last one. It would be cleaner, but I am unsure about the timing as to when it will get applied. Also should it rather be done in rootProject.

Or should I rather have a plugin that implements Plugin<Gradle> and then just apply it straight it in the initscript?

What’s the specific use case you are trying to implement? Wouldn’t it be easier to just set gradle.user.home in your

It would be easier if it was just me.

I want to make as easy as possible for the developers to use the tool. The environment in which it is to be used is pretty complex and restrictive. One of the requirements is that source tree is read-only, so that is a reason for moving the cacheDir.

Two options I can see right now:

  • Check in the file with your source code. Gradle will pick that up.
  • Using an init script should be fine to set the start param. If you want to apply this logic across all projects in your organization, you could build a custom Gradle distribution that automatically applies the init script logic.

I don’t think this works at all.

If I just have an init script containing

startParameter.projectCacheDir = new File('/my/cache')

and I run

gradle --init-script=/my/init.gradle tasks

it will still create a ‘.gradle’ folder.

It is only when I run it with --project-cache-dir that it will set a new cache directory.

gradle --init-script=/my/init.gradle --project-cache-dir=/my/cache tasks

I am now starting to think that this is not possible via the init script at all.

@bmuschko Just to be clear, I am am trying to set the project cache dir (--project-cache-dir), not the gradle user home (--gradle-user-home).

Anyway it looks like it is not possible. Looking at the Gradle codebase, org.gradle.cache.internal.DefaultCacheScopeMapping is created very early from the start parameters, which sets the base directory for project cache. I cannot see any means of touching it afterwards.


I’m also chasing after a way to build without altering the source tree at all, ideally using configurations from properties and no extra command-line arguments.

Did you end up figuring out a way to change the Project Cache Dir like you were intended or did you just give up?

@adamantivm, It is not possible AFAIK from the build script itself. The only means is to provide your own modified gradlew or somehow always have GRADLE_OPTS set in the environment.

Hi adamantivm,

You can try this inside an init script:

//  CACHING_PATH = "${rootProject.rootDir}/cache";
    CACHING_PATH = "somePathName/cache";    
    StartParameter  startParameter = project.gradle.startParameter;
    File            cacheDir = startParameter.getProjectCacheDir();
    String          cacheDir = cacheDir.getName();

    println  "  ** cache directory = '${cachePath}'";

    File            cacheFile      = new File( CACHING_PATH );

    startParameter.setProjectCacheDir( cacheFile );

    cacheDir  = startParameter.getProjectCacheDir();
    cachePath = cacheDir.getName();
    println  "     cache directory = '${cachePath}'";

I think it pretty much works correctly. I’m not using it for production because I wanted to use the project directory so I have to do some thinking about how to represent that. It seems one can not have init scripts within the project folder used by default – At least they only worked for me when I used: “--init-script init.gradle

I would prefer the daemon or gradle to pick-up this kind of initialisation so it becomes automatic. Maybe I don’t know all the options yet. I reckon that approach will work even if you need to use --init-script.

good luck … p

Btw, setting gradle.user.home in your doesn’t actually work, even though the manual seems to say that it does: