How can I make a dependent task not run if the task that depends on it is up-to-date?

I have integration tests that needs to start a web container before running the tests and shutting it down afterwards, leading to the following setup:

task integrationTest(type: Test) {
  dependsOn startContainer
    finalizedBy stopContainer

The problem is that ‘startContainer’ should not run if ‘integrationTest’ is up-to-date. Would be nice if I could ask a task if it is up-to-date so I could put that in an ‘onlyIf’ closure, for example.

The problem is, that the calculation if integrationTest is up-to-date happens AFTER the startContainer task was executed. You (or better say your gradle build) would need to validate the up-to-date state of integrationTest when evaluating up-to-date state of startContainer. I’m afraid there is no (public) API available to do that in your build script atm.

That was sort of like I expected. Would really love something like a Task.dependantsWillDoWork() method :slight_smile:

By executing the startContainer task manually from inside a ‘doFirst’ closure in ‘integrationTest’, I got it to only run when ‘integrationTest’ was not up-to-date.

By executing the startContainer task manually

Doing this really works by accident. We will close this off one day.