Interview Prep/Build Tools (Java, Node.js, Python, .NET)/Java builds (Maven, Gradle, JAR, WAR)

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

  1. Measure first — look at the log timestamps: is it downloads, compilation, tests or a single slow plugin?
  2. Cache dependencies between runs:
    - uses: actions/setup-java@v4
      with: { distribution: temurin, java-version: '21', cache: maven }
    
    In Jenkins, use a persistent volume for ~/.m2 on agents.
  3. Use a Nexus/Artifactory proxy in the same region instead of downloading from Maven Central every time.
  4. Quieter, faster flags: mvn -B -ntp (batch mode, no transfer progress logs).
  5. Parallel builds: mvn -T 1C clean verify (one thread per CPU core) for multi-module projects.
  6. Parallel tests: configure Surefire forkCount / JUnit 5 parallel execution.
  7. Build only what changed in monorepos: mvn -pl <module> -am.
  8. Split stages: fast unit tests on every PR; slow integration/e2e tests on merge to main or nightly.
  9. Docker layer caching: copy pom.xml and run mvn dependency:go-offline before copying src/.
  10. 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)