When are gradle task inputs evaluated?

When does Gradle evaluate inputs of tasks? - I couldn’t find it anywhere.

In my particular case, I would like to have a guarantee that the inputs of my task are evaluated after all of it’s task dependencies have finished. Is that guaranteed? Basically my task depends on output of another (which generates files on the filesystem) in a non-trivial way. If that’s not the case caching can be broken.

From experimentation it seems like the evaluation happens at least twice, once during project’s configuration and once right before the task actually executes. In this case, I would have to do something ugly like check if the task I depend on already executed - if yes, I can compute my real input, if not return an empty value.

See Project.files(…) where you can use a Task as the source for a FileCollection

A Task . Converted to the task’s output files. The task is executed if the file collection is used as an input to another task

So a FileCollection has two pieces of information that Gradle uses

  • Task dependencies
  • The actual files

Gradle first uses task dependencies to determine the DAG which defines the task order. It only iterates the actual files under the hood during each task’s “UP-TO-DATE” check prior to task execution.

Many of Gradle’s file based configuration methods accept Object which you can pass a task instance.

So you can do the following and Gradle will create the task dependencies based on TaskInputs

task task1 {
   outputs.dir "path/to/output" 
   doLast {
      // create files in output dir 
   } 
}
task task2(type:Copy) {
   from task1
   into "another/path" 
} 
task task3 {
   inputs.files task2
   doLast {
      task2.files.each { println it } 
   } 
}