Infrastructure worth keeping — and kept patched.

Skip tests that already passed with the Maven build cache

When nothing in a module has changed, the Maven build cache extension restores its result and skips the tests that already passed. At FosterStack we prove everything, so here is how we proved it to ourselves: real builds, the exact log lines, and the cases where the tests rightly ran again.

The short answer

What we ran

A one-module Maven project with one class and two JUnit tests, run with the Surefire plugin (3.5.2). Every build used a fresh copy of the project on the same Mac and an empty local build cache. The builds shared the cache server, and one Maven repository folder so the test libraries were downloaded once. All runs used mvn verify, with extra options for the cache location and the shared repository. The cache setup is the one on Maven remote build cache setup. We counted the Tests run: line that Maven prints.

RunResultTests run
1. First build, as CI would do itBuilt, BUILD SUCCESS2
2. Fresh copy, same codeRestored from the cache, BUILD SUCCESSnone
3. Change the main codeBuilt, BUILD SUCCESS2
4. Add a testBuilt, BUILD SUCCESS3
5. A test that failsBuilt, BUILD FAILURE2, with 1 failure
6. The same failing test, fresh copyBuilt again, BUILD FAILURE2, with 1 failure

In run 2 Maven restored the module and then listed the steps it skipped, the tests included:

[INFO] Found cached build, restoring demo:mtest from cache by checksum ...
[INFO] Skipping plugin execution (cached): resources:resources
[INFO] Skipping plugin execution (cached): compiler:compile
[INFO] Skipping plugin execution (cached): resources:testResources
[INFO] Skipping plugin execution (cached): compiler:testCompile
[INFO] Skipping plugin execution (cached): surefire:test
[INFO] Skipping plugin execution (cached): jar:jar

What this means for your builds

We did not test a project with several modules here. For that, see Maven build cache extension in a multi-module project. We also did not test tests that read the clock, the network or files outside the project: if a test result depends on something Maven does not see, a restored pass could stand when it should not. That is a decision about your tests, not about the cache.

If you want the tests to run every time

Turn the cache off or skip the lookup for that build. The switches, and what each did in a real run, are on enable or disable the Maven build cache extension.

If nothing is restored

See Maven build cache not restoring. The setup is on Maven remote build cache setup.

Run on Maven 3.10.0 with maven-build-cache-extension 1.2.3, Surefire 3.5.2, JUnit 5.11.0 and FosterStack Cache 0.2.1, macOS (arm64), Java 27.