Cache Gradle GitHub Actions builds, and Maven, with numbers
To cache Gradle GitHub Actions runs, use gradle/actions/setup-gradle , which keeps the whole Gradle User Home rather than the dependency folder alone; for Maven, use actions/setup-java with cache: maven , which is a managed actions/cache over ~/.m2/repository . On one small Spring project measured on a Latchkey runner, the Gradle cache was worth 22.0 seconds a run and the Maven cache 10.3…
To optimize Gradle and Maven builds using GitHub Actions, two specific actions can be utilized: gradle/actions/setup-gradle and actions/setup-java. The gradle/actions/setup-gradle action caches the entire Gradle User Home, while actions/setup-java caches Maven's ~/.m2/repository directory.
For Maven, caching is straightforward as the directory only contains downloaded artifacts. However, Gradle's cache includes the dependency cache, wrapper distributions, generated jars, transformed artifacts, and the local build cache. The official action manages which of these to keep.
In a test project, the Gradle cache saved 22.0 seconds per run, and the Maven cache saved 10.3 seconds. When emptying the dependency directory for both tools, a cold run took 29.3 seconds for Gradle and 14.1 seconds for Maven. With the cache restored, these times dropped to 7.3 seconds for Gradle and 3.8 seconds for Maven.
To manage caches effectively, it is recommended to use actions/setup-java for Maven and gradle/actions/setup-gradle for Gradle. Avoid using actions/cache on the Gradle User Home or pairing actions/setup-java with cache: gradle, as this can cause conflicts. Additionally, note that setup-gradle only writes cache entries from jobs on the default branch, meaning long-lived feature branches may still download missing dependencies on each run.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.