What you will need.
What exactly is Maven’s multi-module?
This multi-module project is built off of a POM aggregator that handles multiple submodules. Often the aggregator is located in the project’s root directory and must have packaging of type pom. The submodules are Maven projects as well and the cool thing is that they can be either built separately or through the same aggregator POM.
Why would we use Multi-modules?
Reducing duplication for one. If we have a project with multiple modules and make changes to them, we don’t have to build all modules separately. Instead, we can just run a Maven command that will do that for us.
What is a parent?
The parent in a maven project is of packaging type pom and it makes the project act as an aggregator which means it won’t be producing further artifacts.
What are modules?
Think of modules as the child projects that effectively inherit properties from the parent project (the aggregator). And again, all modules can be built with a single command, helping us save a whole lot of time. Different children can have different packaging type, which is awesome! One child project may be a jar project and it can be packaged into a war project and etc.
Let’s start building our app!
To create the pom.xml file and the whole Maven in project in Eclipse, follow the steps below:
- Click on New->Other
- After that select Maven Project
- Click on Next
-
Select quickstart, like so: - Give it a Group Id and artifact Id like so:
- Click on Finish.
- Okay great! Now we got our Maven project built.
To create the pom.xml file with the Terminal, type the following:
mvn archetype:generate -DgroupId=com.submoduledemo -DartifactId= submoduledemo -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
Now change the pom.xml’s file packaging type to ‘pom’. This will make in the parent.
<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>submoduledemo</groupId> <artifactId>submoduledemo</artifactId> <version>0.0.1-SNAPSHOT</version> < packaging>pom</packaging> <name>submoduledemo</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>
Now it’s time to add some submodules (child projects). Head on to the parent’s directory and run these commands:
mvn archetype:generate -DgroupId= submoduledemo -DartifactId=example1 mvn archetype:generate -DgroupId= submoduledemo -DartifactId=example2 mvn archetype:generate -DgroupId= submoduledemo -DartifactId=example3
After each of these commands’ execution, you should see something like the following:
Automatically, after we have run these 3 commands (which generate three child projects), Maven will generate them and modify the pom.xml file for us by adding:
<modules>
<module>example1</module>
<module>example2</module>
<module>example3</module>
</modules>
Now, the coolest thing about building sub-modules is that when we run mvn package command in the parent project directory, Maven will test all three modules we created as children and will build them as a consequence. If one submodule is dependent on another for example example1 is dependent on example2 Maven will build example2 before building example1.
One final thing before building the whole project, if we want to share the configuration between our submodules we should declare the parent in their pom.xml files as such:
<parent>
<groupId>submoduledemo</groupId>
<artifactId>submoduledemo</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
Finally, building the project
In Eclipse, right click on project, Run As->Maven build, as such:
And then type package:
You should see something similar to this:
The build resulted in three .jar files for all our child modules.
To do the same in the command line, simply type mvn package.