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 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 we have to create parent module then we will create submodule.

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 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.

Prerequisites

Maven 3.6.1, Java 8 or 12, Eclipse 4.12

Creating Submodule

Now we will create two submodules in this application – email-service and user-service.

Packaging type for each submodule is given as 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

You can download source code.

Thanks for reading.

Related posts

5 Thoughts to “Creating Multi-Modules Maven Project in Eclipse”

  1. Gautam

    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?

  2. Nabeel Ahmed

    Hi!
    Soumitra Roy Sarkar hope you are doing grate. i have a question related to this multi-modules spring boot project. see below detail
    I have 3 spring boot project.
    like A,B,C, each have rest-controller like below:-
    ——————–Project-A———————————-
    @RequestMapping(value = “/demoa”)
    public String demoa() {
    return “demoa”;
    }
    ——————–Project-B———————————-
    @RequestMapping(value = “/demob”)
    public String demob() {
    return “demob”;
    }
    ——————–Project-C———————————-
    @RequestMapping(value = “/democ”)
    public String democ() {
    return “democ”;
    }
    I add the both (B, C) project as a mavend dependency in Project(A) and compiled all
    com.b
    demob
    0.0.1-SNAPSHOT
    com.c
    democ
    0.0.1-SNAPSHOT
    after that i run my project like “mvn Spring-boot run” it run on 8080. When i call the “Rest /demoa” it give me the response while when i try to call other rest it show these error below:
    {
    “timestamp”: “2018-04-25T14:47:56.816+0000”,
    “status”: 404,
    “error”: “Not Found”,
    “message”: “No message available”,
    “path”: “/demob”
    }
    My Question is how i get the response of both 2 api while runing the proejct “A”

  3. karthik

    Good Example

Leave a Comment