“Up To Date” Gradle task status when it has no out

2019-04-03 06:10发布

问题:

How can you correctly mark a Gradle task as being "up to date" when the task doesn't produce any output? The task should remain "up to date" provided the last run was successful and the inputs haven't changed since then. The Gradle guide states just before section 15.9.2, the following:

"A task with no defined outputs will never be considered up-to-date."

How is it possible to mark tasks as up to date in this case? It appears that Gradle needs to know the time of the last successful run and then compare that to the last modified time of the inputs. As a workaround the script could create / touch an empty file to mark the task as complete? Are there any other suggested workarounds?

回答1:

To just think through the different scenarios...

  • Tasks without any inputs or outputs. These run all the time. This might be just wrapping an existing "do something" executable.

  • Tasks with inputs and outputs. These run when either the inputs or outputs change. This might be a compiler.

  • Tasks with just outputs. These run only when the outputs have changed/don't exist. This might be something that downloads something. (I think these are pretty rare in reality, I'd count the URL to download as an input.)

  • Tasks with inputs and no outputs. I haven't run into these in practice.

Like you've said, you could cheat the up-to-date checks with an output file that is just empty. The built-in Gradle Test task is most similar to what you're describing and it has a "report" as its output. I think you would probably have something similar too. It could be as simple as capturing the stdout/stderr of the task and putting that into a file. That's not too useful for when everything passes, but it would be useful for when things fail.

Of course, any of these could be supplemented with a custom upToDateWhen bit of code. e.g., you have a task that starts a webserver and it's "up-to-date" when the webserver is already running. I don't think that's a good fit with what you're describing here.

To start out with, I'd try:

outputs.files file("${buildDir}/reports/${name}.out")

I think that'd work with or without actually putting something in the file.



回答2:

You can override the tasks upToDateWhen method.

task createDist(type: Zip) {
  outputs.upToDateWhen {
    return true
  }
}

So with true it is always up-of-date and false always out-of-date. You can define any kind of custom logic to determine if it is up to date.



标签: gradle