Gradle inputs and outputs

jaco0646 picture jaco0646 · Jun 9, 2015 · Viewed 11k times · Source

I'm learning Gradle and trying to understand how input and output files determine whether a task is up to date.

This task is never up to date, even when the build file doesn't change.

    task printFoo() {
        inputs.file(getBuildFile())

        doLast {
            println 'foo'
        }
    }

This task is always up to date, even when the build file changes.

    task printFoo() {
        outputs.file(getBuildFile())

        doLast {
            println 'foo'
        }
    }

I had expected both examples to consider the task out of date only when the build file changes, and up to date otherwise. What am I missing?

Answer

Johan Stuyts picture Johan Stuyts · Jul 28, 2015

Gradle needs timestamps for inputs and outputs to be able to determine whether the task results are out of date.

In the first case you don't have any output timestamps because you don't have any outputs. Gradle cannot determine whether your outputs are up to date, because it does not know them. So it considers your outputs to always be out of date. From the documentation: "A task with no defined outputs will never be considered up-to-date." (https://docs.gradle.org/current/userguide/more_about_tasks.html#sec:up_to_date_checks)

In the second case Gradle should do what you expect: consider the task outputs out of date when the build file changes. From the documentation: "A task with only outputs defined will be considered up-to-date if those outputs are unchanged since the previous build.". This could be a bug, but I think it is due to you using the build file as an output. Have you tried it with another file?