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 verifyGradle
.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 buildGotchas
- The setup-java action can cache the Maven and Gradle homes via its
cacheinput. - Avoid caching
~/.m2/settings.xml; it can hold credentials you do not want stored.
Related guides
How to Cache the Cargo Registry and target Directory in GitHub ActionsCache the Cargo registry, git dependency cache, and the target directory in GitHub Actions, keyed on Cargo.lo…
How to Enable the Gradle Build Cache in GitHub ActionsTurn on the Gradle build cache in CI so unchanged task outputs are reused, and combine it with actions/cache…