In this tutorial I will explain you how to create multi-modules maven project in Eclipse. This kind of project is a good design when you want to group similar projects and reduce the duplication in code.
A multi-module project is built from a parent pom that manages a group of submodules. In most cases, the parent pom is located in the project’s root directory and must have packaging of type
The submodules are regular maven projects and have packaging type different than pom, such as,
ear and maven build can be run on separate module’s pom file or on the parent’s pom file. By running the maven build on parent’s pom file will build all submodules.
Creating Multi-modules Project in Eclipse
Please follow below steps to create the multi-modules maven project.
First we have to create parent module then we will create submodule.
Creating Parent Module
- Create a New maven project in Eclipse.
- Select Archetype as
- In next window input Group Id, Artifact Id etc. For example, Group Id –
com. roytuts, Artifact Id –
maven-multi-module-project, Version – by default auto populated or you can change.
- Once the module or project created successfully in the Eclipse, open the
pom.xmlfile and change the packaging type as
- Update the project: simply do
Maven -> Update Project.
So we saw how to create a parent module.
Now we will create submodule. The packaging type for submodule depends on what type of project or module you need for your application.
Maven 3.6.1, Java 8 or 12, Eclipse 4.12
Now we will create two submodules in this application – email-service and user-service.
Packaging type for each submodule is given as
- Do right click on the parent module –
New -> Other.
- In the Wizard window select
Maven -> Maven Module.
- In the next window input Module Name as user-service. Make sure that Parent Project selected as
- On next window select Archetype as
- In next window accept all defaults and click Finish. Now you will see in the parent project’s
(maven-multi-module-project)pom has been updated with
- Now open the user-service project’s pom.xml file and remove
<version/>tags. Because these two values will be inherited from parent project or module.
- Now create another module email-service in the similar way as you have created user-service.
- Remove the
<version/>tags from the pom.xml file.
- Now again you will see the parent’s pom file has been updated to include the
The project structure will look like similar to the below image.
Managing Dependencies and Plugins
Ideally you want to add dependencies and plugins into parent module or project from which each child module or project will automatically inherit those dependencies and plugins.
If you want to override any inherited dependency in child pom then you need to declare the same dependency in child’s pom file.
Building the Project
You can build the entire project with all modules from the parent pom file or you can also build the individual module from its own pom file.
Didn’t get the exact structure as shown in the above image?
Solution is highlighted in the below image:
You can download source code.
Thanks for reading.