I recently explored a fairly new concept in code repository organization that some large companies have adopted, called the monorepo. To me it’s a subtle shift in how you manage your projects in code management systems such as Git. But from what I’ve seen, some people have strong feelings about it one way or the other. As a Java developer, there are some tangible benefits. Let’s explore.
Some Opinions on Code Repositories
I enjoy how modern IDEs make it easy to build and test a multi-component application. Whether it’s a series of microservices, a set of libraries, or an application with distributed components, it’s straightforward to use Eclipse or NetBeans to import them, build dependencies, then deploy and run the result. As for external dependencies, tools such as Maven and Gradle handle them well.
This comes naturally to me, and doesn’t impede my productivity in any way. It’s straightforward and common to have a single script to build a project and all of its dependent projects, pull down external dependencies, and then deploy and even run it. Managing Git repositories, on the other hand, is a tedious process to me. Why can’t we have an experience similar to an IDE across source code repositories? We can, and it’s the reason for the monorepo movement.
The Advantages of a Monorepo
Overall, I feel a monorepo helps to overcome some of the nagging polyrepo issues that bother me. The act of cloning multiple repos, configuring permissions, dealing with “pushes” across separate Git repos and directories, forgetting to push to one repo when I’ve updated code across more than one… phew. Tedious and exhausting.
With the monorepo, you ideally place all of your code–every application, every microservice, every library, and so on–in a single repository. Developers then pull down the entire bundle and operate on that one repo going forward. Even as developers work on different applications, they’re working from the same Git repository, which means all pull requests, all branches and merges, tags, and so on, take place against that one repo.
This has the advantage that you clone one repository for your entire organization’s code base and that’s it. For me, that means no more tedium related to multiple repos, as described above. It also has other benefits, such as:
- Avoiding silos: since you pull down the source for all internal applications and libraries, every developer has the means to make code changes, and indeed they should be expected to. This removes silos, where only certain developers would otherwise be expected to maintain them.
- Reduced Pull Requests: if you make changes to a library, its interface, one or more applications that use that library, and related documentation, you only need one pull request and merge with the monorepo compared to multiple, if each were in their own repo. This is sometimes referred to as an atomic commit.
- Transparency: I worked for a company whose main application consisted of dozens and dozens of individual Java projects. Depending on what you were trying to do, you needed to choose combinations of these projects. In a way, these were a simple form of microservices. Occasional I would need to discover which additional Git repo I needed to clone to make things work. With a monorepo, all of the code is there in your local tree.
- Code Awareness: Due to transparency, there’s reduced risk of code duplication.
- Improved Structure: I’ve seen different approaches to structuring a monorepo, but a common one is to create subdirectories for the different types of codebases, such as apps, libs, docs, and so on. Each application resides within their own subdirectory under apps, each library under libs, and so on. It also helps that documentation is kept with the code in the repo.
- CI/CD Integration: GitHub actions helps resolve many tooling issues with monorepos. Specifically, GitHub supports codeowners with scoped workflows for project management and permissions. Atlassian also provides tips for monorepos, and GitLab does as well. Other tools that specifically support monorepos include Bazel, Buck, Nx, and Lerna. But you can get by very well with just Maven or Gradle.
Monorepo and Java Projects
An advantage specific to Java projects with the monorepo is improved dependency management. Since you’re likely to have all of your organization’s applications and libraries locally, any changes to a dependency in an application other than the one you’re focused on will be built and tests run. This will highlight potential conflicts and need for regression testing earlier during development, before binaries are rolled out to production. Let’s see how the monorepo affects Maven projects.
Maven and Monorepos
Of course, Maven isn’t aware of your repo structure, but the monorepo does affect how you organize your Java projects, therefore Maven is involved. For instance, it’s common to structure you monorepo (and development directory structure) like so:
<monorepo-name>
|–apps
| |–app1
| |–app2
|–libs
| |–librarybase
| |–library1
| |–library2
|–docs
However, you can structure your monorepo as you wish, with directories such as frontend, backend, mobile, web, microservices, devops, and so on.
Using Maven Modules
To support the monorepo hierarchy, I use maven modules. For instance, at the root of the project, I define a pom.xml with modules for apps and libs, as shown in Listing 1.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http\://maven.apache.org/POM/4.0.0" xmlns:xsi="http\://www\.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http\://maven.apache.org/POM/4.0.0 http\://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ericbruno</groupId>
<artifactId>root</artifactId>
<version>\${revision}</version>
<packaging>pom</packaging>
<properties>
<revision>1.0-SNAPSHOT</revision>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>15</maven.compiler.source>
<maven.compiler.target>15</maven.compiler.target>
</properties>
<modules>
<module>libs</module>
<module>apps</module>
</modules>
…
Listing 1 - Partial pom.xml showing root level modules.
Within each of the subdirectories, libs and apps, there are pom.xml files that define the set of library and application modules, respectively. As shown in Listing 2, the pom.xml file for the set of library modules is straightforward, and goes within the libs subdirectory.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http\://maven.apache.org/POM/4.0.0" xmlns:xsi="http\://www\.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http\://maven.apache.org/POM/4.0.0 http\://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.ericbruno</groupId>
<artifactId>root</artifactId>
<version>\${revision}</version>
</parent>
<groupId>com.ericbruno.libs</groupId>
<artifactId>libs</artifactId>
<packaging>pom</packaging>
<modules>
<module>LibraryBase</module>
<module>Library1</module>
<module>Library2</module>
</modules>
</project>
Listing 2 - The pom.xml file that defines the monorepo library modules
The pom.xml file for applications is more involved. To avoid needlessly consuming local disk space, and to avoid long compile times, you may choose to only keep a subset of your organization’s applications locally. This is advised against, as you lose some of the benefits of a monorepo, but due to constraints, you may have no choice. In this case, you can omit application subdirectories as you see fit. However, to avoid build errors in your maven scripts, you can use Maven build profiles, which contain
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http\://maven.apache.org/POM/4.0.0" xmlns:xsi="http\://www\.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http\://maven.apache.org/POM/4.0.0 http\://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.ericbruno</groupId>
<artifactId>root</artifactId>
<version>\${revision}</version>
</parent>
<groupId>com.ericbruno.apps</groupId>
<artifactId>apps</artifactId>
<packaging>pom</packaging>
<profiles>
<profile>
<id>App1</id>
<activation>
<file>
<exists>App1/pom.xml</exists>
</file>
</activation>
<modules>
<module>App1</module>
</modules>
</profile>
<profile>
<id>App2</id>
<activation>
<file>
<exists>App2/pom.xml</exists>
</file>
</activation>
<modules>
<module>App2</module>
</modules>
</profile>
</profiles>
</project>
Listing 3 - Maven Profiles with activation based on file existence
The sample monorepo contains only two applications for brevity: App1 and App2. There are Maven build profiles defined for each, where the existence of each application’s separate pom.xml file is checked before the profile is activated. In summary, only the applications that exist on your local file system will be built, no Maven errors will occur, and you don’t need to change the Maven scripts. This works well with Git’s concept of sparse checkouts, as explained by GitHub.
Note: alternatively, you can drive Maven profile activation by checking for the lack of a file, using the
In the sample monorepo, available in my GitHub here, I’ve created two libraries, both of which extend a base library using Java interfaces and Maven modules and profiles. For example, whereas LibraryBase is a standalone Maven Java project, Library1 depends on it. As such, LibraryBase is denoted as a Maven dependency in the pom.xml for Library1:
<dependency>
<groupId>com.ericbruno</groupId>
<artifactId>LibraryBase</artifactId>
<version>1.0-SNAPSHOT</version>
<type>jar</type>
</dependency>
You can open the root monorepo pom.xml file as a Maven Java Project within your IDE of choice, such as NetBeans, and all of the modules will be listed in a hierarchy (see Figure1). You can build the entire set of libraries and applications from this root project. You can also double-click a module–such as App1 in this example–and that project will load separately so you can edit its code.

Figure 1 - A monorepo root Maven project, and an individual module loaded in NetBeans.
Some Challenges to Overcome
There are two sides to every story, and with the monorepo, there are some challenges. As discussed earlier, there could potentially be a lot of code to pull down and keep in sync. This can be burdensome, wasteful, and time consuming. Fortunately there’s an easy way to handle this, as I’ve shown, using build profiles. Other challenges include potential effects on application deployment, the potential of tight coupling between components, concerns over scalability of Git tools, inability to easily search through a large local code base, and some others. In my experience, these perceived challenges can be overcome, tools can and are being modified to handle growing code bases, and the benefits of the monorepo outweigh the drawbacks.
Conclusion
If you want to give monorepo a try, my advice is to start slowly. A monorepo can begin as a team choice, and doesn’t need to be an organization-wide commitment at first. As with anything in today’s agile world of software development, start with small experiments, pivot as you learn, and optimize for what suits your group or organization best.