Creating Multi-Modules Maven Project in Eclipse

Introduction

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 applications or 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 pom.

The submodules are regular maven projects and have packaging type different than pom, such as, jar, war, 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 you have to create parent module then you need to create submodules.

Creating Parent Module

  1. Create a New maven project in Eclipse.
  2. Select Archetype as maven-archetype-quickstart.
  3. In next window input Group Id, Artifact Id etc. For example, Group Idcom. roytuts, Artifact Idmaven-multi-module-project, Version – by default auto populated or you can change.
  4. Once the module or project created successfully in the Eclipse, open the pom.xml file and change the packaging type as pom.
  5. Update the project: simply do Maven -> Update Project.

So you saw how to create a parent module for this multi module maven project.

Now you need to create submodules. The packaging type for submodule depends on what type of project or module you need for your application.

Prerequisites

Maven 3.6.3, Java at least 8, Eclipse at least 4.12

Creating Submodule

Now I will create two submodules in this application – email-service and user-service. I am going to the packaging type for each submodule is jar.

Submodule: user-service

  1. Do right click on the parent module – maven-multi-module-project and click New -> Other.
  2. In the Wizard window select Maven -> Maven Module.
  3. In the next window input Module Name as user-service. Make sure that Parent Project selected as maven-multi-module-project.
  4. On next window select Archetype as maven-archetype-quickstart.
  5. 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 <modules/> tag.
  6. Now open the user-service project’s pom.xml file and remove <groupid/> and <version/> tags. Because these two values will be inherited from parent project or module.

Submodule: email-service

  1. Now create another module email-service in the similar way as you have created user-service.
  2. Remove the <groupid/> and <version/> tags from the pom.xml file.
  3. Now again you will see the parent’s pom file has been updated to include the email-service module under <modules/> tag.

The project structure will look like similar to the below image.

multi module maven project

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.

Related Posts:

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:

multi module maven project

Source Code

Download

3 thoughts on “Creating Multi-Modules Maven Project in Eclipse

  1. Hi Soumitra,
    I have created modules as you have mentioned, but the hierarchy of folders as you have shown in the image is not something which eclipse shows. It shows a separate project folder for each module at the same level as the parent. Is there any setting for showing it in a hierarchy as child modules of the parent as you have shown?

Leave a Reply

Your email address will not be published. Required fields are marked *