Multi-Module Projects

Introduction

Once you get into Enterprise applications there will be many modules and Maven can handle this and this is what this section is all about. In Maen you have a parent POM which normally has a packaging of POM, this POM will be accessed (inherited) by all the other modules in the project and contain global properties that can be used by other POM's. As you can see in the below diagram sub-modules (children) can also inherit form other sub-modules, there is no limit to this but again don't make things to complex. Each module is in affect a project, each project (module or sub-module) will have its own POM file and can inherit up stream to the upper most POM file.


Maven uses the Reactor to perform the following

Maven uses a number of factors to determine the build order

Multi-Module Project (Intellij)

I will be using Intellij for building a multi-module project and thus the sceenshots will reflect this but other ide's are capable of building these types of projects.

First a top level project is created, this project will not have any source code and will have a packaging type of pom

Now we create a directory called jpa-entities and the normal src/main/java structure, create pom file (see below for screenshot), and a User class.


We then update the parent POM file (right screenshot), adding a modules section to see the new jpa-entities module. Also notice in the jpa-entities pom we have added a parent section pointing to the parent POM and some dependencies for that specific module, don't forget to add its own artifactId property. Remember that the sub-module will inherit all the properties in the parent POM.

Sub-Module POM Parent POM

Finally when you run the install lifecycle on the parent POM you can clear see the reactor coming into play, you can see the two modules.


Intellij you can actually create a new module (instead of manually creating it), it will automatically pickup the parment module.


When it gets created it will have the structure as per below, and the POM file will also be created and the lifecycles as well.


Intellij also updated the parent POM to include the new module and now when you run the install lifecycle the reactor picks it up, we can now see three modules (including the parent). Notice the reactor order is as per the modules order in the parent POM file.

Module Dependency

To add a dependency on another module in the project you simply add the dependency property, now even if I reverse the module order in the parent POM the reactor will work out the dependencies in the correct order.

The reactor still orders correctly


Revision Property

The revision property can be used to set the version for the whole project, simply subsitute the verion with ${revision} and now you have only one place to change it. This not only goes for this property only but you can the properties in the parent POM to global use property values.


There is a flatten Maven plugin which allows you to replace the ${revision} in the repository code with the correct version value, if you notice in the repository pom file the ${revision} has not been replaced, you can use the flatten plugin to do this. You use the dependencyManagement to declare the dependencies in the whole project (left screenshot), then you declare the specific dependencies for each module (right screenshot), however you can obmit the version as this will be picked by the dependencyManagement.


Bill of Material (BOM)

In manufactoring a bill of materials is a recipe of components to produce a widget, in Maven the BOM means the dependencies declared within the dependency managemnt section in the POM file, it is used to standized versions.