|

How to override caching in Gradle

Gradle is smart enough to skip running tasks if there haven’t been any changes compared to the previous build. This post shows how to override the default optimisation to ensure the given task is executed every single time.

Let’s create a sample project:

mkdir hello-gradle && cd $_ && gradle init --type java-library

Build it:

gradle build
...
:test
:check
:build
BUILD SUCCESSFUL

Now let’s pick the test task and run just that one with a debug option on:

gradle test -d
...
0:46:52.653 [INFO] [org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter] Skipping task ':test' as it is up-to-date (took 0.073 secs).
10:46:52.654 [DEBUG] [org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter] Finished executing task ':test'
10:46:52.654 [LIFECYCLE] [class org.gradle.TaskExecutionLogger] :test UP-TO-DATE

The task was skipped as expected. Removing the build destroys the cache and makes the tests re-run:

gradle clean test -d
...
10:50:01.966 [INFO] [org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter] Executing task ':test'

The project cleanup however can be avoided by applying the rerun-tasks flag:

gradle test --rerun-tasks

The tests will run again, but without the need to clean the build first.

Similar Posts