Saturday 28 July 2018

Creating Multi-Module Pom Project

In one of my previous posts, I've written about creating maven project for simple purpose. Of course maven can be used for more complex projects as well and the structure defined by maven becomes really useful.
While working on a project you'd realize it has more complex needs, you would need a system where you can develop different independent components and that leads to the scenario we are going to cover in this post. We will be creating a project with maven which will serve as parent, or you can say binding element for the build of the other sub-projects or modules. Maven has a feature which allows you to create multi module project. We are going to see how.
To create a multi module project, we will have to start with a simple project about which you can learn from my previous post Creating a simple Maven Project.

# mvn archetype:generate -DgroupId=myProject.myCompany.com -DartifactId=myProject -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
Once done, you  would see that a directory with name "myProject" as per the artifactId defined earlier is created. Now move into this directory where you would see the pom.xml file created by maven the pom file may appear as below :



Before we move to creation of the modules, we need to do some changes in the parent pom file which was stated above. Locate and modify the lines as given below.

Modify the parameter packaging from "jar" to "pom". Now using the below command, you can create the modules.

# mvn archetype:generate -DgroupId=myProject.myCompany.com -DartifactId=myModule1 -DinteractiveMode=false
You will notice that a new directory is created in the parent directory, open the pom file inside which would appear as below


You will also notice that there are few extra lines added in the main project pom file as below Every new module created will have similar to above line added to the main pom.xml file. So that was quick update about creating a multi module pom project, hope you enjoyed and it was helpful.

Update
If you are running a latest version of maven, you would realize that the build is failing with rather strange error as below
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project myModule1: Compilation failure: Compilation failure:
[ERROR] Source option 5 is no longer supported. Use 6 or later.
[ERROR] Target option 1.5 is no longer supported. Use 1.6 or later.
If you have the above error while build, you might need to  add below lines in the main project's (parent) pom file under "project" head.


With above changes, you should be able to build your project successfully using the command
# mvn clean install