Quick revision
The short answer
First we measure where the 25 minutes go, per module and per plugin, instead of guessing. Then we fix the biggest costs in order: build modules in parallel with -T, build only the modules affected by a pull request with -pl and -amd, cache the local repository between CI runs, move slow integration tests into a separate stage split across agents, and look at whether the module layout itself adds overhead. Locally, mvnd and the Maven build cache extension help too.
What to remember
In this answer, we saw that a slow build is fixed by measuring first, per module and per plugin, and then working on the biggest costs. Parallel builds with -T and selective builds with -pl, -am and -amd cut the compile and test time, caching the local repository removes repeated downloads, mvnd and the build cache extension speed up local work, and a separate, split integration stage removes the largest cost from the fast feedback loop. Finally, the module layout itself is worth a look, because very fine modules add overhead.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
First we measure where the 25 minutes go, per module and per plugin, instead of guessing. Then we fix the biggest costs in order: build modules in parallel with -T, build only the modules affected by a pull request with -pl and -amd, cache the local repository between CI runs, move slow integration tests into a separate stage split across agents, and look at whether the module layout itself adds overhead. Locally, mvnd and the Maven build cache extension help too.
Why this question is asked
A slow build is not just an annoyance. If every pull request waits 25 minutes for feedback, developers batch changes, switch context and stop running the build locally, and the codebase gets harder to change. Interviewers ask this scenario because it tests a method more than a trick: measure, find the dominant cost, fix it, measure again. In this answer, we will see how to measure a Maven build, then go through the changes in the order they usually pay off, with rough numbers for a 60-module project.
1Start by Measuring, Not Guessing
Before changing anything we need to know where the time goes. A 25-minute build might be 20 minutes of integration tests, or 10 minutes of downloading dependencies, or 60 modules each spending 15 seconds on plugin overhead. Each of these has a different fix.
Maven's own output already gives per-module times in the reactor summary at the end. For per-plugin, per-goal detail we add a profiling tool. Options include a build profiler extension, such as the open source maven-profiler extension, which writes a report of time per module and per mojo (a mojo is a single plugin goal, such as surefire:test), or a build scan service such as Develocity, which records every build and lets us compare them over time. CI timestamps per stage complete the picture: how long was spent before Maven even started, for example checking out code and pulling images.
| Where the time goes | Minutes | Fix |
|---|---|---|
| Integration tests in 5 modules | 13 | Separate stage, split across agents |
| Compile and unit tests, 60 modules in sequence | 8 | Parallel build with -T |
| Downloading dependencies | 3 | Cache ~/.m2 or a nearby repository manager |
| Packaging, plugins, overhead | 1 | Check for unneeded plugins per module |
In a typical case like this one, a few modules with integration tests take most of the time, the rest is a long tail of small modules, and a couple of minutes go to downloads. We note the baseline numbers so we can show the effect of each change.
2Build Independent Modules in Parallel
By default Maven builds one module at a time. With -T, the reactor builds modules that do not depend on each other at the same time. On a CI agent with 8 cores, -T 1C often cuts the compile and unit-test part by half or more, depending on how wide the dependency graph is.
But there is a catch: every plugin in the build must be safe to run in parallel. Maven prints a warning for plugins that are not marked thread-safe, and we should take it seriously, especially for custom or old plugins and for tests that use fixed ports, shared temporary directories or a shared database. We fix those tests to use random ports and their own data, or we keep them in the separate integration stage.
3Build Only What the Pull Request Touched
For a one-line change in orders-service, there is no reason to build and test payments-service. On pull requests we compute which modules contain changed files, then ask Maven to build those modules and everything that depends on them.
Notice the two options together: -amd adds the modules that depend on the changed one, so we still catch breakages downstream, and -am builds the changed module's own dependencies in the same reactor instead of trusting the local repository. Changes to the parent POM or to shared build files should fall back to a full build, because they affect every module. The main branch still runs the full build, so nothing escapes for long.
4Stop Downloading the Internet on Every Run
A fresh CI agent starts with an empty local repository and downloads hundreds of megabytes of dependencies and plugins. We cache ~/.m2/repository between runs, keyed on a hash of all pom.xml files so the cache is refreshed when dependencies change, or we rely on a repository manager such as Nexus or Artifactory placed close to the runners so downloads are fast. Running Maven with -B and --no-transfer-progress also keeps the logs readable.
For developers, two more tools help. mvnd, the Maven daemon, keeps a warm JVM and cached plugins between builds, so repeated local builds start much faster. The Apache Maven build cache extension can skip a module entirely when its inputs (sources, dependencies, configuration) have not changed, reusing the previous output, locally or from a shared remote cache. We introduce the build cache carefully, because a plugin with undeclared inputs can lead to a wrong cache hit.
5Move Slow Integration Tests Into Their Own Stage
Integration tests that start Spring contexts, containers or databases usually dominate. We move them from the Surefire unit test phase to Failsafe, which runs tests named *IT in the integration-test phase, and run them in a separate CI stage. The fast stage (compile and unit tests) gives feedback in a few minutes, and the integration stage runs in parallel across several agents, each taking a share of the modules or test classes.
Inside each module, we also look at why the tests are slow. Spring's test context cache reuses one application context across test classes only if they share the same configuration, so a few tests with unusual @MockitoBean or property settings can force dozens of extra context startups. Fixing that can turn a five-minute module into a one-minute module.
- Pull request openedcompute changed modules
- Fast stage-T 1C, -pl changed -am -amd, unit tests only
- Fast stage green?YesStart the integration stageNoReport failure in about 3 minutes
- Integration stageFailsafe *IT tests split across 4 agents
- Main branchfull build with the same stages
5 steps in order. Play them, or step through them one at a time.
6Check the Module Layout Itself
Finally, we look at the shape of the project. Every module has a fixed cost: Maven resolves its dependencies, runs its plugins, and often starts a test JVM. Sixty very small modules can spend more time on that overhead than on real work, so merging modules that always change together can help. The opposite also happens: one huge module that everything depends on means every change rebuilds the world, and splitting its stable API from its busy implementation lets -amd build far less.
After each change we compare against the baseline. A realistic result for this kind of project is a pull request build of 3 to 6 minutes for a typical change and a full main-branch build of around 10 minutes, but the right target is whatever our measurements show is achievable.
7Follow-up questions
What if a plugin is not thread-safe and we cannot replace it?
We can check whether the plugin is needed in every module and limit it to the modules that use it, so fewer parallel builds are affected. If it really must run, we look for a newer version that is marked thread-safe, or we run the affected part of the build without -T in a separate step. Ignoring the warning is risky because the failures it causes are intermittent and very hard to trace.
How do you keep affected-module detection safe?
We fall back to a full build whenever a file outside a module changes, such as the parent POM, CI configuration or shared scripts, and we always run the full build on the main branch. That way a mistake in detection delays finding a problem by one merge at most, rather than hiding it.
8Conclusion
In this answer, we saw that a slow build is fixed by measuring first, per module and per plugin, and then working on the biggest costs. Parallel builds with -T and selective builds with -pl, -am and -amd cut the compile and test time, caching the local repository removes repeated downloads, mvnd and the build cache extension speed up local work, and a separate, split integration stage removes the largest cost from the fast feedback loop. Finally, the module layout itself is worth a look, because very fine modules add overhead.
Open with
I would start by measuring, because a 25-minute build can be slow for very different reasons. Maven's reactor summary gives time per module, and a profiler extension or build scans give time per plugin goal, so I would find out whether it is integration tests, downloads, or overhead spread across 60 modules.
Then, if they want more
Usually a few modules with integration tests dominate. Then I would make changes in order of payoff. I would enable -T 1C so independent modules build in parallel, and check the thread-safety warnings and tests that share ports or databases.
On pull requests, I would compute which modules contain changed files and run mvn with -pl on those, plus -am and -amd, so only the changed modules, their dependencies and their dependents are built, with a full build when the parent POM changes and always on main.
I would cache the local repository between CI runs, or use a repository manager near the runners, and for developers suggest mvnd and possibly the Maven build cache extension. I would move integration tests to Failsafe in a separate stage split across several agents, and look at Spring test context reuse inside slow modules.
Finally, I would check whether the module layout itself adds overhead, merging tiny modules that always change together. After each step I compare against the baseline.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Apache Maven guides
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A 60-module Maven build takes 25 minutes in CI even for one-line changes. What would you...": https://scrivio-interview-library.netlify.app/java/maven/4871b0de02/