Gradle build, UP-TO-DATE check only - gradle

Is there a way to do an up-to-date check of the tasks only, without executing the tasks that are not up-to-date? The motivation behind this is debugging a script with a lot of lengthy tasks and a complicated task tree.

Checking gradle documentation and code there is no such option. Thinking about it this makes a lot of sense as gradle up-to-date logic is being executed during the execution phase as its result may be dependent on the actual outputs of the previous tasks. In other words, in order to know whether a task is up-to-date all its task dependencies has to be either executed or resolved as UP-TO-DATE, thus your ask is not possible.

Related

Continue where an interrupted task left off

I have an application for which the testing is quite extensive. Essentially, we must run the application a few hundred thousand time on different input. So I have built a custom Gradle task which manages forking processes and reaping finished processes. Each of the thousands of test runs generate a file that goes in a results directory. Full testing can take about a week when distributed across 10 cluster nodes.
The issue is, if I need to stop the testing for whatever reason, then there is currently no way for me to start back up where I left off. Gradle's incremental build and caching features (to my understanding) really only work when tasks finish, and it will just rerun the entire task from scratch if the previous invocation was interrupted (ctrl-c).
I could build in some detection of the results files and only rerun segments for which there is no results file. However, this will not work properly when the application is rebuilt, and then testing legitimately must start from scratch.
So how can I reliably detect which testing segments are up to date when the previous task invocation was interrupted?
Annotated tasks
For any Gradle task, if its output files exist and its inputs (including predecessor tasks) are all up-to-date, Gradle will treat that task as up-to-date and not run it again. You tell Gradle about inputs and outputs by annotating properties of the class you write to define the task.
You can make use of this by breaking your custom Gradle testing task into a number of smaller test tasks and have each of those task definitions declare annotated outputs. The test reports are probably the most suitable for those outputs. Then the test tasks which have a report will not have to be re-run if you stop the build halfway.
A whole application rebuild will always need all tests to be re-run
However, if your whole application is rebuilt then those test tasks will no longer be up-to-date as their predecessor build tasks will not be up-to-date. This makes sense of course: a new application build needs its tests to be run again to check it still works as intended.
Multimodule builds may mean only part of an application needs rebuilding
It may be that there are parts of the application that are not being rebuilt, and test tasks that depend solely on those intact parts of the application. If the chain of predecessor tasks for any previously completed test task are all up-to-date, then Gradle will not re-run those tests again either.
This would be more likely to be the case for more test tasks if your application, if appropriate, is separated into different Gradle subprojects in a multimodule build. Each then would have its own chain of tasks which may not have to be re-run if only part of the application's code or other inputs is changed.

Remove Implicit Dependency warning from Gradle output

I've got a generic task in my Gradle build that copies some configuration files to be included in the build, but aren't required for compiling or anything else (they're used at runtime). Basically:
val copyConfiguration by tasks.registering(Copy::class) {
from("${projectDir}/configuration")
into("${buildDir}/")
}
This however leads to an issue in every other task as I now get the Gradle warning about how the tasks use this output without declaring an explicit or implicit dependency
Execution optimizations have been disabled for task ':jacocoTestCoverageVerification' to ensure correctness due to the following reasons:
- Gradle detected a problem with the following location: '...'. Reason: Task ':jacocoTestCoverageVerification' uses this output of task ':copyConfiguration' without declaring an explicit or implicit dependency. This can lead to incorrect results being produced, depending on what order the tasks are executed. Please refer to https://docs.gradle.org/7.4.1/userguide/validation_problems.html#implicit_dependency for more details about this problem.
Now this is only a warning, and the build succeeds, and my service starts up and runs fine. But it does clog my output making it harder to find the line where something went wrong and is in general an eyesore. I'd like to somehow remove that warning. I saw (from the wiki) that the general solution for this is to write an explicit dependency in the task definition, but since this is happening for every task (from compile, to test, to ktlint, to jacoco, etc.) I don't really want to do that.
Is there an alternative, like an anti-dependency, wherein I can tell Gradle that it shouldn't care about the output of the :copyConfiguration task?
Given (emphasis mine to show what to look for)
Execution optimizations have been disabled for task 'spotlessJava' to ensure correctness due to the following reasons:
Gradle detected a problem with the following location: '...\build\generated\source\proto\main\grpc'. Reason: Task 'spotlessJava' uses this output of task 'generateProto' without declaring an explicit or implicit dependency. This can lead to incorrect results being produced, depending on what order the tasks are executed. Please refer to https://docs.gradle.org/7.5.1/userguide/validation_problems.html#implicit_dependency for more details about this problem.
Add the following to build.gradle
tasks.named("spotlessJava").configure { dependsOn("generateProto") }
I had a similar issue and funny that it started with a task related to Jacoco.
I documented a solution here https://discuss.gradle.org/t/task-a-uses-this-output-of-task-b-without-declaring-an-explicit-or-implicit-dependency/42896
In short, what worked for me was to get the location with the problem using the task properties, e.g. getOutputs.
Hope this helps.

How to execute Gradle task more than once?

I've got custom tasks in build.gradle file. I need to periodically recompile sources. The idea is to call tasks.compileJava.execute() in infinite looping custom task. The problem is that (as I understand) task will be executed only once (it doesn't depends on task type/inputs/outputs - even custom tasks will be executed only once).
How can I force gradle to execute task more than once (check inputs/outputs and mark it as UP-TO-DATE if it make sence)?
A task cannot be executed more than once, and Task.execute() should never be called from user code (not even once). Continuous task execution is a planned feature and will require changes to the Gradle codebase.

What does UP-TO-DATE in gradle indicate?

I have recently started using gradle in a project and I am running the standard
gradle clean init build
I noticed that in many of the tasks that are running, I get this UP-TO-DATE message in the console, next to the current task that's running. eg:-
:foo:bar:test UP-TO-DATE
I am curious as to what does this message mean. Couldn't find any documentation around the same.
Everything that Gradle does is a task. Most tasks have inputs and outputs declared. 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. If the input and output is unchanged, it considers the task "up to date" and doesn't execute that task. This can save a lot of time, especially on large builds.
BTW: In case you really want to bypass this build optimization, you can use the --rerun-tasks command-line option to enforce execution of every task.
see documentation of gradle command-line options
Gradle is an incremental build system. This means that it checks that a task must really be executed before actually executing it, in order to be faster.
So, for example, if you already compiled your source files in a previous build, and didn't modify any source file (nor any other input of the compile task), then Gradle won't recompile all the source files, because it know it will lead to exactly the same output as the one already present in the build folder. And the compilation is thus safely skipped, leading to a faster build.
More information in the documentation
I faced the similar problem tried all the options, but the following one worked for me:
gradle -Dorg.gradle.daemon=false <your tasks>

How can I ensure that the processResources task of a gradle build always runs?

We have a strange issue where randomly and infrequently, the compileJava task which deletes the META-INF folder and compiled classes to start, runs but the processResources task reports up-to-date, even though the META-INF directory clearly doesn't exist.
This bites us big time because it's possible that the artifacts make it all the way to production without an applicationContext.xml!
It costs very little for us to run that task, is it possible to force it to run, no matter what?
Maybe there's some kind of bug that fails to clear gradle cache. One possible solution would be to first force the task to clean its own output by running cleanProcessResources.
If that does not work then try overriding the upToDateWhen predicate of your task's outputs like this:
processResources.outputs.upToDateWhen{ false }
However I don't know if this API is permanent.

Resources