---
title: "How to speed up Java CI with Gradle and Maven"
description: "Speed up Java CI with Gradle build and configuration caches, parallel Maven builds, forked tests, JVM startup tuning, and leaner Testcontainers setups."
url: https://9apes.com/blog/speed-up-java-ci/
---

# How to speed up Java CI with Gradle and Maven

To speed up Java CI, turn on the caches your build tool already has, build modules in parallel, fork tests across cores, and stop paying for JVM startup and dependency downloads more often than you need to. Gradle's build cache, configuration cache, and parallel execution are all off by default. Maven builds one module at a time unless you ask for more threads. Those defaults are where most Java pipelines lose their time.

This post is part of a series on [speeding up CI](/blog/speed-up-ci-builds/). The hub covers techniques that apply to every language; this one covers the JVM.

## Gradle: turn on the three switches

Add these to `gradle.properties`:

```properties
org.gradle.caching=true
org.gradle.configuration-cache=true
org.gradle.parallel=true
```

- **Build cache** reuses task outputs, such as compiled classes and test results, when inputs match, including outputs from earlier CI runs if the cache persists. It is [off by default](https://docs.gradle.org/current/userguide/performance.html).
- **Configuration cache** skips the configuration phase when build scripts have not changed. It has been stable since Gradle 8.1, and Gradle 9 calls it the preferred mode, but it is [still not on by default](https://docs.gradle.org/current/userguide/configuration_cache_enabling.html). Some plugins are incompatible; Gradle reports them on the first run.
- **Parallel execution** builds independent subprojects at the same time. It helps multi-project builds and does nothing for a single module.

Gradle 9.8 is current. Gradle 9 requires JVM 17 or newer to run the build, though toolchains can still compile for older targets.

### Caching in GitHub Actions

The [`gradle/actions/setup-gradle`](https://github.com/gradle/actions/blob/main/docs/setup-gradle.md) action caches the Gradle user home: dependencies, wrapper distributions, toolchains, and the local build cache. By default only the default branch writes the cache; pull requests restore from it, which keeps it from filling with short-lived entries:

```yaml
- uses: actions/checkout@v7
- uses: actions/setup-java@v6
  with:
    distribution: temurin
    java-version: 25
- uses: gradle/actions/setup-gradle@v6
- run: ./gradlew build
```

Check the licensing before upgrading. Since v6, setup-gradle's default caching component is no longer open source and requires accepting terms of use; it is free for public repositories and in free preview for private ones. Setting `cache-provider: basic` uses an MIT-licensed provider without cleanup or deduplication. Do not combine setup-gradle with `setup-java`'s own `cache: gradle`.

For teams with many pipelines, a remote build cache (self-hosted or through Develocity) lets every job reuse task outputs from every other job, not just from the previous run of the same workflow.

### Fork tests across cores

Gradle runs a project's tests in a single JVM by default. Forking spreads test classes across several:

```kotlin
// build.gradle.kts
tasks.withType<Test>().configureEach {
    maxParallelForks = (Runtime.getRuntime().availableProcessors() / 2).coerceAtLeast(1)
}
```

Each fork is a new JVM with its own startup and warm-up, so more is not always faster. Avoid a low `forkEvery`, which restarts JVMs repeatedly.

### Run fewer tests

[Develocity](https://docs.develocity.ai/predictive-test-selection/) (formerly Gradle Enterprise) offers predictive test selection, which uses historical build data to run only the tests likely to fail for a change. It is a commercial feature and works for both Gradle and Maven. Without it, Gradle's build cache already skips test tasks whose inputs did not change, which is often the larger win in a multi-module build.

## Maven: build modules in parallel

Maven's reactor builds modules one after another unless you give it threads:

```bash
mvn -T 1C --batch-mode verify
```

`-T 1C` uses one thread per core; `-T 4` uses exactly four. Modules still respect their dependency order, so the gain depends on how wide your module graph is. Plugins not marked thread-safe produce a warning; check them before relying on the result.

### Cache dependencies and build outputs

`setup-java` caches the local repository, keyed on your `pom.xml` files:

```yaml
- uses: actions/setup-java@v6
  with:
    distribution: temurin
    java-version: 25
    cache: maven
```

That avoids downloads but still rebuilds everything. The [Maven build cache extension](https://maven.apache.org/extensions/maven-build-cache-extension/) adds Gradle-style output reuse: unchanged modules are restored instead of rebuilt, from a local cache or a remote HTTP store. Declare it in `.mvn/extensions.xml`; it requires Maven 3.9 or newer.

### mvnd and Maven 4

The [Maven Daemon](https://github.com/apache/maven-mvnd) keeps a warm JVM between builds and builds modules in parallel by default. On a developer machine that saves a lot. On ephemeral CI runners, each job starts a new daemon, so most of the gain comes from its default parallelism rather than from warm JVMs. The stable 1.x line wraps Maven 3.9.

Maven 4 is at release candidate, and Maven 3.10 is the current release. Plan the upgrade, but do not block CI work on it.

### Parallel tests with Surefire

```xml
<plugin>
  <artifactId>maven-surefire-plugin</artifactId>
  <configuration>
    <forkCount>1C</forkCount>
    <reuseForks>true</reuseForks>
  </configuration>
</plugin>
```

`forkCount=1C` starts one test JVM per core. [Surefire 3.6](https://maven.apache.org/surefire/maven-surefire-plugin/examples/fork-options-and-parallel-execution.html) runs everything on the JUnit Platform. Its `parallel` and `threadCount` options apply only to JUnit 4 and TestNG; for JUnit 5 and later, use `junit.jupiter.execution.parallel.*` properties instead.

## Reduce JVM startup cost

A Java build starts many short-lived JVMs: the build tool, compiler workers, and every test fork. Each spends time loading classes and warming up the JIT compiler before doing useful work.

**Use a current JDK.** JDK 25, the current LTS release, ships a default class data sharing archive that speeds up loading of JDK classes, and adds the [Project Leyden ahead-of-time cache](https://openjdk.org/jeps/514) improvements. AOT caches made Spring PetClinic start [42% faster](https://openjdk.org/jeps/483) in the JEP's measurements. They currently require a classpath of JAR files only, and test classpaths usually include class directories, so they suit application startup in integration tests more than build tool or unit test JVMs today.

**Consider limiting JIT tiers for short test JVMs.** `-XX:TieredStopAtLevel=1` skips the optimizing compiler, which helps JVMs that exit before optimization pays off. Apache Lucene adopted it for its tests after [large speedups](https://github.com/apache/lucene/issues/10200), but the same change made Solr's tests slower. Measure it on your suite:

```kotlin
tasks.withType<Test>().configureEach {
    jvmArgs("-XX:TieredStopAtLevel=1")
}
```

## Make Testcontainers start once

Integration tests with Testcontainers often spend more time starting containers than running tests. Two patterns help:

- **Start shared containers once per test JVM** with a static container in a base class (the [singleton pattern](https://java.testcontainers.org/test_framework_integration/manual_lifecycle_control/)) instead of one per test class.
- **Start independent containers in parallel** with `Startables.deepStart(postgres, kafka).join()`.

The reuse feature that keeps containers alive between runs is experimental, and the Testcontainers docs say it is [not suited for CI](https://java.testcontainers.org/features/reuse/). On ephemeral runners there is nothing to reuse anyway.

## How 9apes speeds up Java CI

Java builds spend a large share of their time on serial work: Gradle configuration, JVM startup and class loading, and the longest module chain. That work runs on one core at a time, so single-core speed matters as much as core count.

9apes runs each GitHub Actions job in a fresh microVM on hardware chosen for high single-core performance with NVMe storage. Fast cores shorten JVM startup, configuration, and serial compilation; NVMe speeds up the many small files in a Gradle user home or Maven repository. Parallel Gradle builds, `-T 1C`, and forked tests can use more cores, with sizes up to 8 vCPU and 32 GB of memory on Linux x86_64. See [available runners](/docs/available-runners/).

We have not published a Java-specific benchmark; the [9apes benchmarks](/benchmarks/) cover C++, Rust, and Go builds. To measure your own build, follow the [quick start](/docs/quick-start/), change the runner label on one job, and compare complete job time and [cost](/pricing/) with the same caches enabled:

```diff
- runs-on: ubuntu-latest
+ runs-on: 9apes-8vcpu-ubuntu-2404
```

## Should I use the Gradle daemon in CI?

Gradle recommends the daemon for CI as well. On ephemeral runners each job starts a new one, so the benefit is limited to reuse within a single job that runs several Gradle invocations.

## Is the Gradle configuration cache safe to enable?

It has been stable since Gradle 8.1. Some plugins are incompatible; enable it, read the report from the first run, and update or replace the plugins it flags.

## Does Maven -T 1C always speed up builds?

Only when modules can build side by side. A long chain of modules that each depend on the previous one stays serial whatever the thread count.

## Should I cache ~/.m2 in GitHub Actions?

Yes, with `setup-java`'s `cache: maven`. It removes downloads but not compilation; add the Maven build cache extension to reuse build outputs.
