TExcelTAD
Home › Java Builds: Maven & Gradle › Field Notes: Reproducible JARs & Stable Tests

Field Notes: Reproducible JARs & Stable Tests

A build is reproducible when the same source, built twice (a minute apart or a year apart, on two machines), produces bit-for-bit identical artifacts. That lets anyone verify that a published JAR really came from the tagged source, makes caches more effective, and turns "did anything change?" into a checksum comparison. Build Fundamentals explains the general idea; on the JVM the main culprit is small and concrete: timestamps inside JAR files.

Maven: project.build.outputTimestamp

A JAR is a ZIP, and every ZIP entry carries a modification time. By default Maven's archiver stamps entries with the build time, so two builds differ. The property project.build.outputTimestamp fixes every entry's time (and the archiver also sorts entries):

bash · try it yourself
unset SOURCE_DATE_EPOCH   # this machine sets it, and Maven's archiver would use it as a default
mkdir -p r/src/main/java/demo && cd r
echo 'package demo; public class A { }' > src/main/java/demo/A.java
pom() {
  cat > pom.xml <<POM
<project xmlns="http://maven.apache.org/POM/4.0.0">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId><artifactId>r</artifactId><version>1.0</version>
  <properties>
    <maven.compiler.release>21</maven.compiler.release>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    $1
  </properties>
</project>
POM
}
twice() {
  mvn -q clean package && cp target/r-1.0.jar first.jar
  sleep 3   # ZIP times have 2-second resolution
  mvn -q clean package && cmp -s first.jar target/r-1.0.jar && echo "identical" || echo "different"
}
pom ""
echo "no outputTimestamp:   $(twice)"
pom "<project.build.outputTimestamp>2026-01-01T00:00:00Z</project.build.outputTimestamp>"
echo "with outputTimestamp: $(twice)"
unzip -l target/r-1.0.jar | sed -n '4,7p'
unzip -p target/r-1.0.jar META-INF/MANIFEST.MF
no outputTimestamp:   different
with outputTimestamp: identical
        0  2026-01-01 00:00   META-INF/
       99  2026-01-01 00:00   META-INF/MANIFEST.MF
        0  2026-01-01 00:00   demo/
        0  2026-01-01 00:00   META-INF/maven/
Manifest-Version: 1.0
Created-By: Maven JAR Plugin 3.5.0
Java-Version: 21
Build-Jdk-Spec: 21
  • Use a fixed date (or the date of the release commit; the release plugin updates the property automatically when it is in the POM).
  • The Build-Jdk-Spec manifest entry records the JDK feature version: building with a different major JDK still changes the JAR, which is why reproducibility also needs a pinned toolchain.
  • mvn artifact:compare (Maven Artifact Plugin) rebuilds and compares against a published release, and artifact:check-buildplan lists plugins that are known not to be reproducible.

Gradle: reproducible archives

Archive tasks (Jar, Zip, Tar) have two properties: isPreserveFileTimestamps and isReproducibleFileOrder. Since Gradle 9 they default to reproducible (fixed timestamps, sorted order), so a plain build is already stable; older builds and many existing scripts set them explicitly, and you should keep them that way if you support Gradle 8:

bash · try it yourself
mkdir -p g/src/main/java/demo && cd g
echo 'package demo; public class A { }' > src/main/java/demo/A.java
echo 'rootProject.name = "g"' > settings.gradle.kts
twice() {
  gradle -q clean jar && cp build/libs/g.jar first.jar
  sleep 3   # ZIP times have 2-second resolution
  gradle -q clean jar && cmp -s first.jar build/libs/g.jar && echo "identical" || echo "different"
}
echo 'plugins { java }' > build.gradle.kts
echo "Gradle 9 defaults:          $(twice)"
unzip -l build/libs/g.jar | sed -n '4,5p'
cat > build.gradle.kts <<'KTS'
plugins { java }
tasks.withType<AbstractArchiveTask>().configureEach {
    isPreserveFileTimestamps = true          // the old default: real file times
}
KTS
echo "preserveFileTimestamps=true: $(twice)"
cat > build.gradle.kts <<'KTS'
plugins { java }
tasks.withType<AbstractArchiveTask>().configureEach {
    isPreserveFileTimestamps = false
    isReproducibleFileOrder = true
}
KTS
echo "explicit settings (Gradle 8 style): $(twice)"
Gradle 9 defaults:          identical
        0  1980-02-01 00:00   META-INF/
       25  1980-02-01 00:00   META-INF/MANIFEST.MF
preserveFileTimestamps=true: different
explicit settings (Gradle 8 style): identical

Gradle's fixed timestamp is 1980-02-01, safely after the earliest date a ZIP entry can hold (1980-01-01), so no time zone can push it out of range.

Other sources of non-determinism

Source Fix
a different JDK version toolchains (JavaLanguageVersion.of(21)), requireJavaVersion, the wrapper; record the JDK in CI
build time or user name written into resources (${maven.build.timestamp}, Built-By) use the commit time or the version instead; current JAR plugins no longer write Built-By (see the manifest above)
generated code with unordered maps or random ids sort output; seed generators
absolute paths in outputs relative paths; Gradle's @PathSensitive(RELATIVE) for inputs
locale and time zone (String.format, dates in reports) -Duser.timezone=UTC -Duser.language=en for the build and test JVMs

Deterministic tests

Tests that pass or fail depending on order or timing destroy trust in CI faster than anything else.

  • Order of test classes. Surefire's default runOrder is filesystem, which can differ between machines; set <runOrder>alphabetical</runOrder> (or random with a recorded seed, to find order dependencies). Gradle runs classes in a stable order per fork; keep maxParallelForks deliberate.
  • Order of test methods. JUnit 5's default method order is deterministic but intentionally not obvious; tests must not depend on each other. If a sequence matters, say so with @TestMethodOrder(MethodOrderer.OrderAnnotation.class) and @Order.
  • Time, randomness, environment. Inject a java.time.Clock instead of calling now(); seed every Random; pin time zone and locale in the test JVM (<argLine> for Surefire, jvmArgs(...) or systemProperty(...) on Gradle's Test task).
  • Parallelism. org.gradle.workers.max and maxParallelForks (Gradle) or -T and Surefire's forkCount/parallel (Maven) change interleavings; tests that share files, ports or static state must be isolated before you turn them up. (This course's sandbox runs Gradle with one worker for exactly this reason: stable output.)
  • Flaky tests get quarantined and fixed, not retried forever. Retries (Surefire rerunFailingTestsCount, the Gradle test-retry plugin) are a stopgap that hides real bugs if left on.

Test yourself

Which Maven property makes the timestamps inside built JARs fixed?

  1. maven.build.timestamp
  2. project.build.outputTimestamp
  3. maven.compiler.release
  4. project.build.sourceEncoding

In Gradle 9, are JARs reproducible without any configuration?

  1. No, timestamps are always preserved
  2. Yes: archive tasks default to fixed timestamps and reproducible file order
  3. Only with --build-cache
  4. Only for Kotlin projects

A test passes on your laptop and fails in CI only when another test class runs first. What is the first fix?

  1. Retry it three times
  2. Make the tests independent (shared static state, files, clock) and run classes in a fixed order to reproduce
  3. Disable parallel builds forever
  4. Delete the test