Skip to content

Build cache

The unit tests on a feature branch don't rerun a module that nothing changed in. The Maven build cache extension hashes each module's inputs, and when a build with the same hash has run before, it restores that build's compiled classes and test reports instead of compiling and testing again.

What counts as a change

A module's hash covers:

  • every file under its src/ (Java, templates, properties, JSON, CSS, JS: any type);
  • the files at its root that its build reads (package.json, biome.json, the lint and type baselines, scripts/, types/ in web-mvc; docs/api-spec/ in rest), listed in .mvn/maven-build-cache-config.xml;
  • its effective pom (dependencies, plugin versions and their configuration);
  • the hashes of the modules it depends on.

The last one is what makes it safe: a change in commerce-core changes the hash of every module that depends on it (web-mvc, web-admin, web, rest, worker, auth, ai, channels), so they all run. A change in rest alone runs only rest's tests; common's and commerce-core's ~1,100 test classes are restored.

Where it's on

Run Cache
unit-tests on a feature branch Looks up and saves. The GitLab cache is per branch (build-cache-<branch>) and starts from development's.
unit-tests on development Saves only (-Dmaven.build.cache.skipCache=true): every module is built and tested, so a verified commit was fully tested.
Deploy build, integration tests, devEnv.sh, runTests.sh Off. The config sets enabled to false; only a build that passes -Dmaven.build.cache.enabled=true uses it.

On a hit the module's surefire-reports, jacoco.exec and site/jacoco are restored too, so the job's JUnit report and coverage figure still cover every module.

Measured in the build container (4 CPUs) on the full reactor:

Change Modules built and tested Time
Nothing cached all 11 8 min 10 s
Nothing changed none 12 s
A rest controller and a Nova template web-mvc, web-admin, web, rest 3 min 18 s

Run package, not test

The unit-test job runs mvn package (build-scripts/unitTests.sh), which runs the unit tests and makes each module's jar. A module restored from the cache joins the build as that jar, and the modules after it compile and test against it.

A module restored at test has no jar, so Maven resolves it from the local repository instead: whatever 0.0.0-SNAPSHOT some earlier install left there, possibly from another branch. Tests then run against the wrong code, and may pass. So only package and later builds are saved (-Dmaven.build.cache.cacheCompile=false, in .mvn/maven.config), and anything that turns the cache on must run at least package.

Tests that read another module's files

The one thing the cache can't see is a test that opens a file by path in another module. If that module isn't one the test's module depends on, a change there alone leaves the test's module unchanged, and the test is skipped when it should have run.

Two ways to keep such a test honest, in order of preference:

  1. Put it in a module that depends on everything it reads. The template scanners (TemplatesUseShopDatesTest, TemplatesUseShopMoneyTest, TemplateInlineExpressionsTest) read the templates of commerce-core, web-mvc, web-admin and web, so they live in web, which depends on the other three. PassColoursTest reads Nova's stylesheets and lives in web-mvc.
  2. Declare the files as a maven.build.cache.input.<name> property in the module's pom, when no module depends on everything the test reads:
    • commerce-core: BadgerBrandTokensTest reads web-mvc's admin-ui.css and auth's auth.css;
    • web-mvc: ProductFullReadRatchetTest scans ai's MCP tools and rest's API controllers.

A new test that reads ../<module>/... or walks up from Path.of("") must do one of these.

Turning it off

  • One run: -Dmaven.build.cache.enabled=false, or -Dmaven.build.cache.skipCache=true to build everything but still save the result.
  • CI: to force a full run on a branch, clear the runner caches in GitLab (Build > Pipelines > Clear runner caches), which clears every branch's build-cache-<branch>. development always runs everything anyway.

Trying it locally

BUILD_CMD="mvn -B package -Dmaven.build.cache.enabled=true -Dmaven.build.cache.location=/build-cache" \
  docker compose -p badger-dev -f dev-scripts/badger/docker-compose-dev.yml --profile build run --rm --no-deps \
  -v badger-build-cache:/build-cache -v badger-build-cache-jars:/build-output build

The second volume keeps the jars this package makes out of badger-jars, which the running dev stack starts from.

The build container copies the source to a fresh workspace each time, so a second run with the same volume shows the restore: modules print Found cached build, restoring ... from cache.