A GitHub Actions Workflow for a Maven Project: Cache, a Java Matrix and Test Reports That Survive a Red Run
One workflow file for a Spring Boot ticket desk. The first run downloads 924 files and the build step takes 38 s; with the Maven cache restored it downloads none and takes 28 s. A matrix runs Java 21 and 25 side by side, and when a one-line change breaks a test, both jobs go red but still upload the report that says why.
What you will learn
- Trigger a build on every pull request and on pushes to main
- Cache Maven's downloads with setup-java and explain where the cache key comes from
- Test on Java 21 and Java 25 at once with a matrix
- Keep the Surefire reports of a failed run with upload-artifact and if: always()
Before you start
- CI with GitHub Actions: Run the Tests and Build the Docker Image on Every Push
- Java: Maven and Projects in IntelliJ
A build on a pull request has one job: tell the author, quickly, whether the change is safe to merge, and if it isn't, why. This workflow does that for a small Spring Boot ticket desk with seven tests. It caches what Maven downloads, tests on two Java versions at once, and keeps the test reports even when the run fails.
All the runs below were made with act, a tool that runs GitHub Actions workflows locally in Docker, so no GitHub account was needed. On GitHub itself, the same file shows up as checks on the pull request.
The whole file
name: build
on:
pull_request:
push:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
java: [21, 25]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v4
with:
distribution: temurin
java-version: ${{ matrix.java }}
cache: maven
- name: Build and test
run: ./mvnw -B verify -Djava.version=${{ matrix.java }}
- name: Upload test reports
if: always()
uses: actions/upload-artifact@v4
with:
name: test-reports-java-${{ matrix.java }}
path: target/surefire-reports/
on runs it for every pull request and for pushes to main, so a merge is checked again on the branch everyone shares. act -l lists one job, test, for the events pull_request,push.
The build step is the same command you'd run on your own machine: ./mvnw -B verify. The Maven wrapper means the runner doesn't need Maven installed, and -B (batch mode) keeps the log free of progress bars. verify compiles, runs the tests and packages the jar.
The cache: 924 downloads, then none
A fresh runner has an empty ~/.m2. Every plugin and library has to come from Maven Central. On the first run the log had 924 "Downloaded from" lines, the cache step reported cache-hit=false, and the build step for the Java 25 job took 38 seconds.
cache: maven in setup-java saves ~/.m2/repository at the end of the job and restores it at the start of the next. The log showed where it went:
Cache saved with the key: setup-java-Linux-x64-maven-cff8a25be917...
The last part of the key is a hash of the project's pom.xml files. Same dependencies, same key, same cache. Add a dependency and the key changes, so you never build against a stale set of jars. On the second run the log said Cache restored successfully, there were 0 downloads, and the same build step took 28 seconds.
(The step still prints cache-hit=false on the second run. In this run that output stayed false even though the restore worked, so read the "Cache restored" line rather than that flag.)
A matrix: two Java versions, one file
strategy.matrix.java: [21, 25] turns one job into two copies that run side by side, one with java: 21 and one with java: 25. The value goes into setup-java and into Maven as -Djava.version, so each copy compiles and tests for its version. In act's log they're test-1 and test-2; on GitHub they appear as two checks, test (21) and test (25). Both ran 7 tests with 0 failures and ended with Job succeeded.
A matrix is how a library proves it still works on the older Java its users have, while the app moves to the newer one.
A red run that explains itself
Then a one-line change in TicketService:
- if (ticket.hoursOpen() >= 24) {
+ if (ticket.hoursOpen() >= 25) {
Both jobs failed with Tests run: 7, Failures: 1 and BUILD FAILURE. Normally a failed step skips every step after it. The upload step has if: always(), so it ran anyway, and the artifacts folder held test-reports-java-21 and test-reports-java-25. Inside the Java 25 one, TicketServiceTest.txt said:
Tests run: 4, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.191 s <<< FAILURE! -- in org.studyeasy.s01l02.TicketServiceTest
org.studyeasy.s01l02.TicketServiceTest.ticketOpenForADayBecomesMedium -- Time elapsed: 0.012 s <<< FAILURE!
org.opentest4j.AssertionFailedError: expected: <MEDIUM> but was: <LOW>
The test name and the message tell the reviewer exactly what broke: a ticket open for a day should be MEDIUM, and after the change it's LOW. Without if: always(), a red run would leave only a long log to scroll through. On GitHub, the artifacts are on the run's page for anyone to download.
Common mistakes
- Only running on push. The check then comes after the merge. Run on
pull_requesttoo. - Calling
mvninstead of./mvnw. The runner's Maven version may differ from yours. The wrapper pins it. - No cache. Every run downloads the internet again, and slow checks get ignored.
- Uploading reports without
if: always(). They're uploaded only when you don't need them. - A matrix of every version ever. Each one costs runner time. Test what you support, and no more.
Try it yourself
Add a test dependency to the pom and run the workflow twice. What happens to the cache key? Count the "Downloaded from" lines in both runs and explain the difference.
Related course on Udemy
FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE
Java 25, JSP, Hibernate, Spring Boot 4, REST APIs, React and Spring AI.View on Udemy ↗: FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE (paid course, opens in a new tab)Related posts
Why Spring in 2026: What the Container Does and What Spring Framework 7 Changed
Spring in one line: a container that creates your classes and hands each one what it needs. Plus the Spring Framework 7 baseline (JDK 17 to 25, Jakarta EE 11, Tomcat 11, JUnit 6), what got better than Spring 6, and a two-line version check.
A Plain Spring Project, No Boot: Your First Bean With spring-context
One dependency, one recipe, one getBean: a Maven project with spring-context 7.0.9, a Blog record, an AppConfig with @Bean, and a main that asks the container for the Blog. Then delete @Bean and meet NoSuchBeanDefinitionException.
Inversion of Control and Dependency Injection in Spring: One Program, Built Three Ways
A PostService needs a PostRepository. Build it three ways: the service calls new itself (tight coupling), main hands it in through the constructor (DI by hand), and the Spring container does the wiring from a @Configuration recipe.
Spring Component Scanning: @Component and @ComponentScan Instead of @Bean Methods
Instead of one @Bean method per class, put @Component on EmailNotifier and PostService and @ComponentScan on an empty BlogConfig. Spring walks the package, finds the stickers and builds the beans; main doesn't change.
