Build Cache - Limit to a single task

I would like to use the gradle build cache for one of my custom tasks. I cannot just turn it on globally and would like to only turn it on for this one task. Is there a way to do this? I do not think there is but would be really nice if there was.

You should be able to add something like this to your settings.gradle:

gradle.rootProject {
    allprojects {
        tasks.whenTaskAdded { task ->
            task.outputs.cacheIf { task.name == 'yourCustomTask' }
        }
    }
}

Seems like it is not working. At least I am not seeing the build cache dir get created. Below is what is in my settings file. Thanks for your help. Just wanted to see if anyone has any ideas. I am running my normal “gradlew build” command

buildCache {

    local(DirectoryBuildCache) {
    	enabled = true
    	println "---------------${rootDir}"
    	directory = new File(rootDir, 'build-cache')
	}  
}

gradle.rootProject {
    allprojects {
        tasks.whenTaskAdded { task ->
            task.outputs.cacheIf { task.name == "myTaskname"}
        }
    }
}

Did you remember to run with --build-cache on the command-line or org.gradle.caching=true in your gradle.properties?

I was not adding “–build-cache” to my gradle command. I was worried that this would turn the build cache on for all tasks.

I did try with this and the build cache dir did get created. The issue now is that it does not look like the build cache dir is getting populated correctly. The only file in the build cache dir after I run the “gradlew build --build-cache” is gc.properites and it is empty.

Thanks in advanced for working with me on this.

I see this behavior when task.outputs.cacheIf { task.name == 'yourCustomTask' } doesn’t evaluate to true for any task (i.e. yourCustomTask is a string that doesn’t match any task name). You may want to log some additional data to see if you’re getting the expected true for your correct task.

Yeap it is matching. I did the below to verify

gradle.rootProject {
    allprojects {
        tasks.whenTaskAdded { task ->
            task.outputs.cacheIf { 
            	def shouldStoreInBuildCache = false;
            	if(task.name == "myTaskname") {
            		shouldStoreInBuildCache = true;
            		println "--------------here----------------"
            	}

            	return shouldStoreInBuildCache;
            }
        }
    }
}

I am able to see caching of a single task with what has been discussed, but it is not clear what may be different with your project / task from what has been provided. I would recommend running with --info as an argument. There should be logging like Caching disabled for task ':myCustomTask': with a reason why the task is not being cached. That should provide the best hint to resolve.

I see what was happening… I think. If my task already passes the UP-TO-DATE check that task does NOT get published to the build cache. Pretty sure this was the issue. It looks like it is working now. Thank you very much for your help.