GitHub Action
Maven Cache
This Github Action adds improved support for caching Maven dependencies between builds compared to Github's built-in Maven cache.
Features:
- restores dependency cache with the help of the git history
- clears unused dependencies before saving caches
- caches the Maven wrapper if present
- relies on Github Action's built-in cache infrastructure (just like Github's Cache Action)
Benefits:
- faster and more predictable builds times
- considerable load reduction on artifact repositories
Audience:
- primarily intended for use with (private) third party repositories, i.e. 'one repo to rule them all'
- repos which act as artifact caches for other remote repositories and/or
- repos with limited transfer capacity, and/or
- repos where data transfer is limited or increases cost
- also works well with Maven Central
Note that Github seems to have an excellent network connection to Maven Central, so reducing the reliance of the (private) third party repositories by lettings projects additionally (i.e. first) connect directly to Maven Central might be a good alternative.
The skjolber/maven-cache-github-action
action must be present twice in your build job, with step: restore
and step: save
parameters:
jobs:
hello_world_job:
runs-on: ubuntu-latest
name: Maven build with caching
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up JDK 21
uses: actions/setup-java@v4
with:
java-version: 21
distribution: liberica
- name: Restore Maven cache
uses: skjolber/maven-cache-github-action@v3.1.1
with:
step: restore
- name: Build hello-world application with Maven
run: mvn --batch-mode --update-snapshots verify
- name: Save Maven cache
uses: skjolber/maven-cache-github-action@v3.1.1
with:
step: save
The second steps saves your cache even if the dependencies were only partially resolved:
- artifact transfers fails
- partial build
Required:
step
- Build step, i.e.restore
orsave
Optional:
key-path
: A list of files and wildcard patterns used to detect files which affects dependency cache content (default: **/pom.xml)cache-key-prefix
: Prefix for cache keys (default: maven-cache-github-action)wrapper
: Enable Maven wrapper cache (default: true, but skipped if.mvn/wrapper/maven-wrapper.properties
is not present)depth
- Maximum git history depth to search for changes to build files (default: 100 commits).upload-chunk-size
- The chunk size used to split up large files during upload, in bytes.enableCrossOsArchive
- An optional boolean when enabled, allows windows runners to save or restore caches that can be restored or saved respectively on other platforms (defaults: false).
cache-restore
- A value to indicate result of cache restore:none
- no cache was restoredpartial
- a cache was restored, but from a previous version of the build files (or a failed build for the current).full
- cache was restored and is up to date.
Combine cache-key-prefix
with key-path
to have seperate caches within the same repo.
While unused dependencies are contiously removed from the (incremental) cache, it is sometimes necessary to clean the cache completely.
Add [cache clear]
to a commit-message build with a new, empty cache entry.
This action only saves/loads dependency data to/from the Github Action cache infrastructure.
On initial setup, it additionally transfers a cache cleaning utility from Maven Central using an HTTP call.
The scripts and documentation in this project are released under the MIT License
- Tidy Cache - clear cache (for successful builds only)
- Entur CircleCI Maven Orb
- Github Cache Action