Skip to content
Latchkey

How to Cache the Maven and Gradle Caches in GitHub Actions

Cache ~/.m2/repository for Maven or ~/.gradle/caches for Gradle, keyed on your build files.

Maven downloads into ~/.m2/repository; Gradle caches into ~/.gradle/caches and ~/.gradle/wrapper. Key on the build definition so a dependency change refreshes the cache.

Maven

.github/workflows/ci.yml
- uses: actions/cache@v4
  with:
    path: ~/.m2/repository
    key: maven-${{ runner.os }}-${{ hashFiles('**/pom.xml') }}
    restore-keys: maven-${{ runner.os }}-
- run: mvn -B verify

Gradle

.github/workflows/ci.yml
- uses: actions/cache@v4
  with:
    path: |
      ~/.gradle/caches
      ~/.gradle/wrapper
    key: gradle-${{ runner.os }}-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
- run: ./gradlew build

Gotchas

  • The setup-java action can cache the Maven and Gradle homes via its cache input.
  • Avoid caching ~/.m2/settings.xml; it can hold credentials you do not want stored.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →