Skip to content
Latchkey

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-java with distribution: temurin and cache: '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 verify

Gotchas

  • -B (batch mode) silences interactive prompts and trims log noise in CI.
  • cache: 'maven' keys on pom.xml; a multi-module build caches the whole ~/.m2.

Related guides

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