Search code examples
gradlegradle-plugingradle-task

Gradle: where are task dependencies defined for distZip task


I am using the gradle application plugin.

I noticed that:

  1. distZip task get's run before the assemble task.
  2. And I can see from the docs (link above), and confirmed during my gradle run, that the distZip tasks depends on: jar, startScripts tasks

So execution looks something like this:

> Task :my-app:processResources
> Task :my-app:compileJava
> Task :my-app:classes
> Task :my-app:jar
> Task :my-app:startScripts
> Task :my-app:distTar
> Task :my-app:distZip
> Task :my-app:assemble

Looking through the code in ApplicationPlugin.java and/or DistributionPlugin.java, I expected to see the task dependencies defined. e.g. something like this:

distZipTask.dependsOn(jarTask)
distZipTask.dependsOn(createScriptsTask)
assembleTask.dependsOn(distZipTask)

...but I couldn't find anything like this in the java code.

Would very much appreciate it if someone could point me in the direction of where to look to find out how these tasks are 'linked'.


Solution

  • Gradle can determine implicit task dependencies from task inputs. If (the output of) a task is used as input of another task, Gradle can derive that there must be a dependency on the task. This functionality is called incremental build support, as it also supports checking for changes in the inputs or outputs to skip tasks if the inputs did not change.

    Regarding your example, the task dependencies are defined implicitly in line 197 and line 208 of the file ApplicationPlugin.java:

    libChildSpec.from(jar);
    ...
    binChildSpec.from(startScripts);
    

    The arguments in these lines are the respective tasks (to be exact: providers of the tasks). The from method can often be used to define sources of a file operation (e.g. copying, zipping ...). So we define the results (outputs) of the tasks jar and startScripts as a source of some operation (CopySpec). The resulting CopySpec objects are then passed to both tasks distZip and distTar. When a task tries to resolve the defined sources it finds the tasks jar and startScripts and defines the dependencies on them on its own.

    Please note that the mentioned task assemble is a so-called lifecycle task. It does not run any action but can be used to organize tasks into build phases (just like build and check).