Gradle inputs and outputs
Asked Answered
F

1

12

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?

Federalize answered 9/6, 2015 at 15:43 Comment(0)
K
12

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?

Kenner answered 28/7, 2015 at 19:23 Comment(4)
I tested again with several files: status is always up-to-date when only output is specified. When both input and output are specified, status changes as expected (even when input and output are the same file). I have accepted this answer based on the statement, "Gradle needs timestamps for inputs and outputs." My testing shows that both are required.Federalize
It sounds like a bug. But I don't have a clue who should change the output files and when to do this, for a task with only outputs to be considered out of date. Should you touch/change the outputs during the configuration phase of that task? The semantics are not clear in my opinion. It is best to always specify inputs and outputs for reliable behavior.Kenner
if the output is specified but the file doesn't exist, it's also up-to-date?Hyperostosis
No, a task is not up-to-date if one of its outputs is missing. You explicitly state that the task will produce a file/directory, so when it is not there, the task cannot be up-to-date.Kenner

© 2022 - 2024 — McMap. All rights reserved.