r/java • u/Level_Yak_87 • Oct 04 '26
I wrote own efficient Maven scheduler to build modules with max parallelism
While inspecting Maven multi-module builds performance I've figured out the fundamental limitation: for any two modules depending on each other (moduleB depends on moduleA, in any scope), Maven will first execute all phases and goals of moduleA, and only then schedule the building of moduleB. Independent dependency branches can be executed in parallel, but directly/transitively dependent - only sequential.
In real projects running tests is usually the most time-consuming part, so this significantly limits possible parallelism.
To address this problem I made an extension providing a custom scheduler (in Maven it's called "builder") https://github.com/maven-turbo-reactor/maven-turbo-builder . When it is enabled, Maven schedules downstream dependencies right after the our module JAR is packaged, and then runs tests of our module and downstream module build in parallel (this way utilizing the multi-core CPUs much more):

The extension also does two things:
- reorder
packageandtestphases (run tests afterpackage, not before as default), so we can schedule downstream dependencies earlier - prioritize building modules with the highest number of downstream dependencies - this way reducing the chance of unloaded Maven worker threads
It's easy to set up - add to .mvn/extensions
<extensions>
<extension>
<groupId>com.github.seregamorph</groupId>
<artifactId>maven-turbo-builder</artifactId>
<version>1.3</version>
</extension>
</extensions>And then define on the CLI or .mvn/maven.config (to make it default):
-bturbo
-T1CIn real projects the boost is approximately 35% in clock time, also this extension is complementary with build caching.
I used this extension for more than a year in various mission-critical projects and received positive feedback from other organizations, so I believe it's quite stable.
12
u/davidalayachew Oct 04 '26
Interesting. I only skimmed it, but the gist looks pretty cool.
Doesn't this break a lot of Maven Plugins though? A lot of plugins actively depend on the order of the Maven lifecycle to be consistent.
In fact, now that I think about it, aren't you kind of uprooting a load-bearing wall here? There are a lot of things in the entire Maven ecosystem that depend on this lifecycle being in the order that it is.
Another question, and maybe this is answered by me doing more than skimming -- what is the order change for dependent builds that have to wait for many others?
You said that you prioritize the modules with the most dependents. But what if there are free threads in the pool?
The way maven works is that, if you have Module A, which Modules AA, AB, and AC depend on, Modules AAA, AAB, and AAC depend on AA, then your parallel build in normal maven will go like this.
Each stage must finish before the next stage can start. As in, I can't build AA before A is built. Which makes sense.
But what doesn't make sense is that I cannot build AAA before AC is built. That is because Maven forces all modules in each stage (stage 2 in this case) to be built before progressing to the next stage.
Some other plugins have solved this problem. For example, here is the Takari Smart Builder.
I guess my question is, since you are tackling the question of how to efficiently schedule maven builds, are you also tackling this problem too?
For me personally, this is actually where most of my time is spent, and thus, would probably have the greatest return on investment for me. I build over 100 modules, and the dependency hierarchy is definitely tree-like, so this would have immediate speed-ups for me.
I don't use Takari because it does a whole bunch of other "clever" things under the hood -- stuff that might cause problems for my builds.
But something like yours, which targets a very specific problem might be more within my realm of possibility. Of course, uprooting load-bearing walls aside.
7
u/jonenst Oct 04 '26
I think this was planned for maven 4
"the concurrent builder will allow a dependant project to be built as soon as the dependencies are at the ready phase"
https://maven.apache.org/whatsnewinmaven4.html#lifecycle-changes
4
u/RebbitUzer Oct 04 '26
Does it have any advantages over Maven Deamon (mvnd)?
2
u/bluecheetah001 Oct 05 '26
How does this handle standard out? My main problem with
mvm -T1Cis that the logs from testing are interleaved between modules and are basically unusable.2
u/agentoutlier Oct 05 '26
The first rule of Maven Club is to not put unit tests of any slowness in core modules. The second rule is to basically do that for all modules. The third rule is to skip javadoc creation.
5
u/sweating_teflon Oct 04 '26
I LOVE this. I've been thinking about a build system that could split out parallelism between phases of of a module. I never thought Maven would have the flexibility to do it! Can't wait to try it on a big project.
This is yet another extension that should become part of the standard Maven distribution. It's transparent to the user and brings free performance gains.
1
u/Zardoz84 Oct 05 '26
How compares against mvnd ? mvnd uses his own scheduler to maximize parallelism.
1
u/stefanos-ak Oct 05 '26
hey, I tried this on our somewhat large project (200k LoC), and it moved the build+test time from 10min to 9min. 10% for free is pretty cool! (our project is very optimized already).
That being said, it uncovered that the last 30% of the build is waiting on a single module. If it was possible to make that module faster to a point where that extra 30% didn't exist, then the turbo builder would not provide us any benefits at all (according to the timeline graphs). very interesting...
1
u/cachely-admin Oct 11 '26
Nice work, especially the point about this being complementary to build caching.
We're building Cachely, a managed remote build cache, and exploring Maven support (not available yet).
From what you've seen, are teams already using the Maven Build Cache Extension in practice, or is setting up and maintaining a shared cache still a barrier?
We're trying to understand the actual pain before deciding what to build.
-3
u/vips7L Oct 04 '26
Looks like Claude wrote it and not you.
-6
u/_somethingvague_ Oct 04 '26
Or just move to Bazel and get parallelism and cache hitting for free.
0
u/[deleted] Oct 04 '26
[deleted] — view deleted comment