Maven in a Google Style Monorepo
Is using Maven in a monorepo situation possible? Is it even desirable? This experiment looks at making Maven into a Monorepo-type setup for more modularity.
Join the DZone community and get the full member experience.Join For Free
let's consider some data. google’s gigantic monorepo has:
- 86tb of history
- 9 million unique files
- one branch (trunk based development)
- 25k developers all committing there
from my calcs (googlers please correct me), there’s one commit to the trunk every 30 seconds, and their proprietary ci infrastructure keeps up with that on a per-commit basis. that’s a different story.
a monorepo is where two or more teams with different deployable/shippable applications and/or services (and potentially different release cadences/schedules) exist in the same repo/branch. ‘the trunk’ of trunk based development specifically.
those checkouts could get big, right?
a level 2 monorepo
(martin fowler will give this a better name, hopefully).
the checkout to the developer’s workstation expands or contract to the smallest amount of buildable/linkable modules needed to perform test and package operations. not only that but in a 100% provable way from a deep understanding of the directed graph of buildable things and the things that could use them.
at some level, ex-googler ex-facebooker buck-committer, simon stewart points out, this expand/contract thing is just a view or projection of a monorepo. he says:
“the amount of boilerplate required to get the maven thing working is terrifying. both buck and blaze let you just create a new directory, shovel classes into it, and you’re done. the “src” nonsense required to make mvn work without hoop jumping seriously raises the bar to multiple modules.”
he is right of course, but maven is still the enterprise gorilla.
the maven challenge
maven is a recursive build technology that forward declares child modules to build. compare that to google’s blaze (partially open sourced as bazel) and facebook’s buck which are directed graph build systems where there are no forward declarations. it is no co-incidence that expanding/contracting checkouts are easy with them, because that was the design goal blaze (the one that came first).
maven’s forward module declarations look like this:
<modules> <!-- maven works out the build order - phew! --> <module>amodule</module> <module>another-module</module> <module>this_one_has_child_modules_too</module> </modules>
all those are directories within the current directory. maven is going to take some coercing .
ant and gradle are the same, but i’ll focus on the java enterprise gorilla — maven.
maven monorepo proof of concept — in git
i took google’s guava because it had a multi-module build that, although small, could be a surrogate for something with hundreds of modules, which could represent a company’s entire set of (java) deployable/shippable applications and/or services.
the default checkout of this repo doesn’t build as all the
files were renamed
no matter, run this:
now you have poms again.
runs as you’d expect now.
run this on the command line:
mvn com.github.ferstl:depgraph-maven-plugin:aggregate -dincludes=com.google.guava
this gives a dot graph (graphviz) that, via some colorization in omnigraffle, looks like:
i’ve colored in two items above. wouldn’t it be nice to modify the checkout (working copy) to have just those two modules, and maven not choke because of missing modules?
well, that’s possible now. do this:
git config core.sparsecheckout true echo '/mr' > .git/info/sparse-checkout echo '/readme.md' >> .git/info/sparse-checkout echo '/pom*' >> .git/info/sparse-checkout echo '/guava/' >> .git/info/sparse-checkout echo '/guava-testlib/' >> .git/info/sparse-checkout mr/checkout.sh
works on just two modules now, rather than the eight before. the dependency graph looks like this:
a word of warning
for the love of turing, have a lock-step version number for everything built in the monorepo. maybe maven’s classic
suffices, and in your cd-esque deployment technologies, you designate something more meaningful in jenkins (etc).
that’s easy, some more python fu, that works with
the first dot graph
to allow you to conveniently modify
. like so:
mr/checkout.sh guava-testlib # calculates that it needs guava too # or mr/checkout.sh guava-tests,guava-gwt # calculates that it needs another 5: guava-testlib/test, guava, # guava-testlib/test, guava-tests/test, and guava/test (and not # guava-testlib at all)
well, i haven’t convinced anyone bar googlers and xooglers. no matter, i’m looking forward to having git or mercurial push their size boundaries to get to the place perforce, subversion, and plasticscm can so that companies can bank on monorepo setups.
while i have your attention
can i direct your attention to a portal documenting ‘trunk based development’ (including monorepos)? -> https://trunkbaseddevelopment.com
no ads, no services being sold, and mobile friendly. well worth a look, in my opinion.
Published at DZone with permission of Paul Hammant, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.