Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Up To Date" Gradle task status when it has no output

Tags:

gradle

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?

like image 289
brunobowden Avatar asked Apr 13 '15 06:04

brunobowden


People also ask

How does Gradle determine up to date?

Gradle will determine if a task is up to date by checking the inputs and outputs. For example, your compile task input is the source code. If the source code hasn't changed since the last compile, then it will check the output to make sure you haven't blown away your class files generated by the compiler.

How do I enable task in Gradle?

From the main menu, select Run | Edit Configurations to open the run/debug configuration for your project. icon. In the list that opens, select Run Gradle task. In the Select Gradle Task dialog, specify the project and the task that you want to execute before launching the project.

Is not up to date because task upToDateWhen is false?

upToDateWhen { false } means “outputs are not up to date”. If any upToDateWhen spec returns false, the task is considered out of date. If they return true, Gradle does the normal behavior of checking input/output files.

What is Gradle check task?

Check is a task you run from the command line using ./gradlew check in Linux and Mac or gradlew check in Windows. To understand check, know that Gradle has 2 types of tasks: actionable tasks have some action(s) attached to do work in your build. lifecycle tasks are workflow tasks with no actions attached.


2 Answers

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.

like image 51
bigguy Avatar answered Oct 17 '22 00:10

bigguy


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.

like image 27
Nullstress Avatar answered Oct 17 '22 02:10

Nullstress