Your Maven build takes 15 minutes in CI. How would you speed it up?
IntermediateScenarioJava builds (Maven, Gradle, JAR, WAR)
Answer
Short answer
Most of the time usually goes to downloading dependencies and running tests serially. Cache the ~/.m2/repository, use a nearby Nexus mirror, build in parallel, avoid rebuilding unchanged modules, and split slow integration tests into a separate stage.
Step-by-step
- Measure first — look at the log timestamps: is it downloads, compilation, tests or a single slow plugin?
- Cache dependencies between runs:
In Jenkins, use a persistent volume for- uses: actions/setup-java@v4 with: { distribution: temurin, java-version: '21', cache: maven }~/.m2on agents. - Use a Nexus/Artifactory proxy in the same region instead of downloading from Maven Central every time.
- Quieter, faster flags:
mvn -B -ntp(batch mode, no transfer progress logs). - Parallel builds:
mvn -T 1C clean verify(one thread per CPU core) for multi-module projects. - Parallel tests: configure Surefire
forkCount/ JUnit 5 parallel execution. - Build only what changed in monorepos:
mvn -pl <module> -am. - Split stages: fast unit tests on every PR; slow integration/e2e tests on merge to main or nightly.
- Docker layer caching: copy
pom.xmland runmvn dependency:go-offlinebefore copyingsrc/. - Bigger agents if CPU-bound.
Real-world result
A typical outcome: dependency cache + Nexus proxy cuts 6–8 minutes; parallel modules and tests cut another 3–4.
What to say in the interview
"I'd first find where time goes from the build log. Usually caching ~/.m2 and using a local Nexus proxy fixes most of it. Then parallel builds with -T, parallel tests, building only changed modules, and moving slow integration tests out of the PR pipeline."
All Build Tools (Java, Node.js, Python, .NET) questions Roadmap: Java apps: Maven, Gradle, JAR & WAR
More on Java builds (Maven, Gradle, JAR, WAR)
What is pom.xml and what is it used for?BeginnerHow do you build and run a Java project with Maven?BeginnerWhat is the difference between a JAR and a WAR file?BeginnerWhat is Gradle and how is it different from Maven?BeginnerHow do you configure a Spring Boot application for different environments?BeginnerA Maven build fails in the pipeline with "Could not resolve dependencies". How do you fix it?Intermediate