Search code examples
gradlekotlingradle-kotlin-dslbuild-definitiontooling

Does Gradle automatically infer dependency between tasks? If so, when?


In my build script, when I configure the downloadAndUnzipFile task, I am explicitly querying output of downloadZipFile task. I expected this is sufficient for Gradle to infer a dependency between the tasks, but it apparently is not, because I get an error when invoking downloadAndUnzipFile`.

Execution failed for task ':downloadAndUnzipFile'.
> Cannot expand ZIP '/home/jdanek/repos/testing/gradle-infer-deps/build/1.0.zip' as it does not exist.

My build script build.gradle.kts is

import de.undercouch.gradle.tasks.download.Download

group = "org.example"
version = "1.0-SNAPSHOT"

plugins {
    id("de.undercouch.download").version("4.0.4")
}

tasks {
    val downloadZipFile by registering(Download::class) {
        src("https://github.com/michel-kraemer/gradle-download-task/archive/1.0.zip")
        dest(File(buildDir, "1.0.zip"))
    }

    val downloadAndUnzipFile by registering(Copy::class) {
        from(zipTree(downloadZipFile.get().outputFiles.first()))
        into(buildDir)
    }
}

I also tried

from(zipTree(downloadZipFile.get().outputFiles.first()))

and that does not define a dependency either.

My Gradle is the latest 6.2.2.


Solution

  • In order for Gradle to discover task dependencies, they have to use specific types for their inputs and outputs so that Gradle can track the dependencies for you. See this documentation page on the topic.

    In your use case, the de.undercouch.download plugin seems to expose a simple List<File> which is not a rich type, so Gradle cannot figure out the link. In that case you have be explicit about the task dependency, using dependsOn(downloadZipFile)