How to check if a future task is up-to-date

Continuing the discussion from How to override upToDate behaviour cargoStartLocal:

Is there a way to configure one task in such a way that it is skipped when another, future task is up-to-date.

When I run an integrationTest task, I first start a local application server (cargoStartLocal). However, I don’t want to start the application server when the integrationTest task is considered up-to-date. It makes no sense to start the application server, simply to find out that there’s nothing to do.

I thought about wiring the outputs and inputs of the integrationTask to those of cargoStartLocal, but that doesn’t help because the plugin that provides the cargoStartLocal task explicitly sets up-to-date to false (upToDateWhen{false}) and there’s no way in gradle to replace and override that behaviour.

Next thought was to play around with an onlyIf Spec and inside that onlyIf spec I check if the integrationTest task is up-to-date like the following:

cargoStartLocal.onlyIf {
    def result = integrationTest.outputs.????
    println "integration is up to date: $result"
    return !result
}

However, I can’t seem to find a way to manually check if integrationTest will be up-to-date.

Is there some way?
Greetings
Jan

You could just wire it into gradle’s DAG

integrationTask.dependsOn 'cargoStartLocal' 
integrationTask.finalizedBy 'cargoStopLocal' 

More info here

Hi, thanks for the reply.

I’m already doing the dependsOn and finalizedBy and that part works just fine.

The problem I’m trying to solve is to skip execution of cargoStartLocal and cargoStopLocal whenever there is no work to be done in integrationTest.

Greetings
Jan

I thought that was gradle’s default behaviour for task dependencies… Perhaps I’m wrong

Hi, you might be right.

However, the plugin that provides the cargoStartLocal task explicitly sets up-to-date to false (upToDateWhen{false}) - for some reason that I don’t understand - and there’s no way in gradle to replace and override that behaviour.

So I’m looking for other ways to do the same thing, and the only thing I could come up with was to specify an onlyIf spec in cargoStartLocal's task where I first check if integrationTest will be up-to-date. However, I cannot find a way to do that because the integrationTest is yet to execute.

Greetings
Jan

Perhaps another task could help

cargoStartLocal.enabled = false
cargoStopLocal.enabled = false 
task cargoSwitch {
   inputs.files integrationTask.inputs.files
   outputs.files integrationTask.outputs.files
   doLast {
      cargoStartLocal.enabled = true 
      cargoStopLocal.enabled = true 
   } 
} 
cargoStartLocal.dependsOn 'cargoSwitch' 
cargoStopLocal.dependsOn 'cargoSwitch' 

Oh yes. That works. Nice trick!

This is the final configuration I used:

cargoStartLocal.enabled = false
cargoStopLocal.enabled = false

integrationTest.dependsOn 'cargoSwitch'
integrationTest.dependsOn cargoStartLocal
integrationTest.finalizedBy cargoStopLocal
cargoStartLocal.dependsOn 'cargoSwitch'
cargoStopLocal.dependsOn 'cargoSwitch'


task cargoSwitch {
    inputs.properties integrationTest.inputs.properties
    inputs.files integrationTest.inputs.files
    outputs.files integrationTest.outputs.files
    doLast {
        cargoStartLocal.enabled = true
        cargoStopLocal.enabled = true
    }
}

Thanks for the help.

Greetings
Jan

integrationTest.dependsOn 'cargoSwitch'

I’d hope this dependency wasn’t required as it can be derived. But perhaps I’m wrong because of the enabled flag