How to Set Up CI for Java (Maven) With GitHub Actions
setup-java with cache: maven persists the ~/.m2 repository so dependencies are not re-downloaded each run.
Run actions/setup-java with a distribution (Temurin) and cache: 'maven', then mvn -B verify, which compiles, runs checks, and executes the test phase. A JDK matrix proves compatibility.
Steps
- Check out the code.
- Run
actions/setup-javawithdistribution: temurinandcache: 'maven'. - Build and test with
mvn -B verify. - Fan out across JDK versions with a matrix.
Workflow
.github/workflows/ci.yml
name: CI
on:
push:
branches: [main]
pull_request:
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
java: ['17', '21']
steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: ${{ matrix.java }}
cache: 'maven'
- run: mvn -B verifyGotchas
-B(batch mode) silences interactive prompts and trims log noise in CI.cache: 'maven'keys onpom.xml; a multi-module build caches the whole~/.m2.
Related guides
How to Set Up CI for Java (Gradle) With GitHub ActionsSet up GitHub Actions CI for a Gradle project: setup-java provides the JDK, gradle/actions/setup-gradle cache…
How to Set Up CI for a Spring Boot App With GitHub ActionsSet up GitHub Actions CI for a Spring Boot app: setup-java provides the JDK with Maven caching, a Postgres se…