Ignoring task outputs

I have a task that only needs to run when dependencies change. It alters an external file, but it doesn’t need to run again until the dependencies change, no matter what other external program also modifies that external file.

First, I added the following:

def dependenciesAsString = (project.configurations.compile.dependencies.collect { ${it.group}:${it.name}:${it.version}" }).join(’,’)

project.tasks.myTask.inputs.property “dependenciesAsString”, dependenciesAsString

This part works OK - to a point. If the output file hasn’t changed and the dependencies haven’t changed, nothing happens. If the dependencies change, then the file is regenerated. But, if the output file is changed externally, Gradle still marks it as out of date.

So, now I’m trying to tell Gradle that the output file doesn’t matter: ‘task.outputs.files.from.clear()’. This “works” a little too well: now Gradle is complaining that no output files are specified, so it’s going to execute the task no matter what.

How do I tell Gradle to ignore the output file and base its up-to-date decision completely on the input properties?

You can provide a closure to the task for customizing the up-to-date logic. This should mark the output as always up to date, no matter what, but similarly you could just do a File.exists() test to gate execution.

task.outputs.upToDateWhen { true }

Providing a ‘{ true }’ closure to upToDateWhen just disables the task. Checking to see if the file exists means it’ll only execute once. Neither one of those is what I need.

The up-to-date logic I need goes something like:

task.outputs.upToDateWhen { dependenciesAsString == task.STUFF_FROM_LAST_TIME.properties.dependenciesAsString }

I just have no idea how to access STUFF_FROM_LAST_TIME.

I suppose I could implement my own cache, but that would be duplicating what Gradle already does.

You want ‘task.outputs.upToDateWhen { false }’. It will then fall back to checking whether the inputs have changed.

I just tried ‘task.outputs.upToDateWhen { false }’. Now it runs every time.

I am trying to use this, but it always thinks it’s UP-TO-DATE. Is there a way to FORCE it to be out-of-date? If false doesn’t do it, then what does?