Monk CIMonk CI home

26 Nov, 2026Nitin Mandale

Speed Up Java CI: Maven and Gradle Caches, Parallelism and Memory


Your Java build is slow. Should you add more CPUs? First, find out where the time goes: downloading dependencies, configuring the build, compiling code, running tests, or waiting on garbage collection. Each bottleneck needs a different fix.

1. Stop downloading the same dependencies

Fresh runners often start without Maven or Gradle's local dependencies. On GitHub Actions, enable caching in setup-java:

- uses: actions/setup-java@v4
  with:
    distribution: temurin
    java-version: '21'
    cache: maven  # Use gradle for a Gradle project

Compare cache restore time and dependency downloads on cold and warm runs. If warm builds still download heavily, inspect cache misses, changing dependencies, and repository resolution. Dynamic and snapshot versions can trigger additional checks; Gradle's performance guide explains the trade-offs.

2. Avoid repeating compilation

Dependency caching saves downloads; it does not automatically reuse compiled outputs. Gradle's separate build cache can reuse eligible task outputs:

./gradlew build --build-cache

Look for FROM-CACHE in task output. On disposable runners, use persistent or remote build-cache storage to reuse outputs across jobs. For large multi-project builds, check whether changes force downstream recompilation and whether annotation processors are declared correctly.

3. Parallelize the work that can run together

For Maven multi-module builds, try ./mvnw -B -T 2 verify. For Gradle multi-project builds, try ./gradlew build --parallel --max-workers=2. Compare against the same build without those flags: dependencies between modules limit how much can run simultaneously.

Tests need separate attention. Maven Surefire forks can multiply concurrency across parallel modules; Gradle uses maxParallelForks for parallel test processes. Start small and isolate tests that share ports, files, or databases.

4. Leave enough memory for every JVM

The build process and test forks can each have their own heap. Too many workers can exhaust runner memory or spend more time on garbage collection. Track peak runner memory and test duration as you increase concurrency. If GC looks suspicious, use JVM GC logging to investigate. Gradle's org.gradle.jvmargs controls daemon settings; test JVM settings are separate.

Measure before changing runners

Record the commit, JDK and build-tool versions, runner size, cache state, and timings. Change one setting at a time and compare several runs. For Gradle, a Build Scan separates configuration, dependency resolution, and task execution.

Then try the same workflow on Monk CI. Its cache backend supports setup-java, and the Migration Wizard can open a pull request with the runner change. Compare your own warm and cold runs to see what improves.

Try Monk CI for free

Continue with GitHub