Register

[DevOps] Multi-module Maven Projects

A maven best practice is to make sure that intra-module dependencies are always unidirectional, in order to avoid cyclic references. 

Below’s a very good example we’ve experienced from one of our projects. Assuming that we have a multi-module maven project that looks like:
pom.xml  (parent pom in the project root folder)
| module1
pom.xml 
| module2
pom.xml 

The pom.xml  in module2 has a dependency on module1, and the pom.xml  in module1 has a dependency on module2. (In other words, module1 is a dependency of module2 and module2 is a dependency of module1).

When you run mvn clean package, the build fails fairly quickly and you will get the following error if your project is configured to be a multi-module one:
“The projects in the reactor contain a cyclic reference.”

In order to avoid these cyclic references (which multi-module maven projects strictly do not allow), the best practice is to always extract your common code into a separate module in order to resolve the cyclicity. Modularizing code also helps you figure out how to refactor your code so that it is easily re-usable.