Maven Multimodule Project: A Detailed View
By: Anish Antony
Tags: Coding, Java, Maven, Programming, Learning

Maven is a greater tool which makes build process easily. In Organizational projects, you can see there are different sub-modules. Each submodule has a separate pom.xml, and there is an aggregator pom.xml which will consolidate all the submodules. Here over this article lets explore the advantages of Multimodule Project.

Photo by Volodymyr Hryshchenko on Unsplash

This article lets discuss what a maven multi-module project is and how it makes your life better.

The large scale projects are not constant all the time. Every time there is a change appeared on the project. Sometimes it will be a new feature addition, and sometimes it is revoking the already added feature from the project.

If you are handling these in your single pom project, then when the project grows up, your pom also becomes very large also the pom becomes very sensitive. But by converting the project into a multi-module project, then it is so handy.

It is very easy to accommodate shared modules, external projects into to project. Also remember, the multi-module project is so helpful for the DevOps team to configure.

Why Multi-Module Project?

The major benefits of making multi-module projects are

One single command is required to build all projects, including the submodules. Maven always cares about the build order for you. You don't need to worry about that. In most cases, the sub-modules depend on other modules. So the build order matters while building the project. Multimodule project configuration on your CI server is so easy. One single job can handle everything. Can build and work with a single module itself. Suppose in your multi-module project there are different teams are working on different modules. You don't need to worry about others' work, and you can work by importing your own submodule.

How to make a Maven Multi-Module Project?

For a multimodule Maven project, In the root directory, there is a pom.xml there which is the parent pom and its packaging is pom . From the parent module, it is referencing the submodule.

Let's discuss in detail about the structure of the parent pom.

parent pom

The main difference between multi-module parent pom and normal pom is its packaging type. For a multi-module parent pom, its packaging type becomes ‘pom’.

<packaging>pom</packaging>

Also in parent pom, it is referring to the submodules with <submodule> tag.

<modules>

<module>email-service</module>

<module>frontend</module>

<module>data-service</module>

</modules>

Submodule pom

In the submodule pom.xml , the main change is it referring the parent pom using parent tag.

<parent>

<groupId>com.lici.maven</groupId>

<artifactId>maven-module</artifactId>

<version>0.0.1-SNAPSHOT</version>

</parent>

What are the additional advantages of parent pom?

The main use of parent pom is sharing the details to the submodule. especially property information such as versions, artefact details…etc. This information is the same for all submodules so no need to type on the submodules repeatedly. Just declare the properties on the parent and can refer those properties using ${} symbol.

Another advantage of parent pom is, you can define the general dependencies on dependencyManagement tag and can refer those dependencies on the submodules without mentioning the version. This helps to get away the version conflicting issues.

How to create a sample Maven Multi-module project?

Let's move on to create a sample multi-module project here. This is actually just consolidating the steps which we discussed above.

First, need to create the parent project including parent pom which includes the submodules information and its packaging type should be pom.

parent pom file

Next, we can create the sub-modules inside of the parent project and its related poms. So in our project contains 3 submodules, each modules pom.xml looks like follows.

data-service child pom

email-service child pom

frontend child pom

Here in the frontend child pom, it is referring to the other child modules email-service and data-service.

Finally, let's execute the build from parent pom. You can see here all submodule builds are executed as part of parent pom execution.

Here while building the project maven itself understands it is referring the data-service and email-service from frontend module. So he will handle the build order himself. We don't need to pass any additional information regarding the build order.

This is the basics of the multi-module project using maven. Hope you enjoyed this article.