Multi Module spring boot application
What is multi module?
A Spring Boot project that contains nested maven projects is called the multi-module project. In the multi-module project. The parent project works as a container for base maven configurations.
Github link
Here is the Github link for the complete code which we will create in this article.
Benefit of multi module
Splitting the project into multiple modules is useful and easy to maintain. We can also easily edit or remove modules in the project without affecting the other modules. It is useful when we required to deploy modules individually.
We only need to specify all the dependencies in the parent pom. All the other modules share the same pom, so we need not to specify the same dependency in each module separately. It makes the code easier to keep in order with a big project.
Maven child module
- The child modules are independent maven projects that share properties from the parent project.
- All child projects can be built together because it is inside a parent project. (We will see it at the end of this article)
- It is easier to define the relationship between the projects.
Final Project structure will looks a like as per below screenshot

Let’s create a multi module project using eclipse
File -> new -> Project -> Maven project
It will open a new dialog box

In above image we can see I have selected Create a simple project, it will only create a pom file and skip all other unnecessary folders for multi module project. And I have already created a folder named as user-service where we will create our multi module project which I have selected as workspace location.
Now click on Next button

In above image, you can add data as per your requirement, but please make sure to select pom from packaging drop down. And as we are creating parent project currently so skip the details of parent project.
Click on Finish
After that we can see our project in Project Explorer as per below image

Open pom.xml file which will look a like as per below

Now let’s add some important dependency to our parent pom.xml file
Once done, now as we have added new dependency, we need to update the maven project. For that right click on the project -> maven -> update project -> ok. Wait till the process done.
We are done with our parent, now let’s add child module, I will show only one as rest will be created the same way, except the name and details will be different.
Let’s create a child module for data-model where we will create models for Data Base.
Right click on the project user -> new -> project -> Maven Module

Make sure to select Create a simple project and add the name of the module as per above screenshot and click Next.

Add details as per your requirement and click on Finish.
Now our project will looks a like as per below screenshot

As we have added a new module our parent pom.xml file will as per below screenshot.
At the end of the pom.xml file we can see modules where our Data-Model is added and when we will add new module it will be added automatically here.
Add dependency for Data-Model, so modify the new module pom.xml as per below
Here lombock is used to reduce the code for setter, getter, constructor, logger etc.
Now let’s do some code for models. Let’s add the package com.prajapati.hetaram.model under src/main/java folder by right clicking on the java folder -> new -> package.
Now create a class named as User.java under the new model as per below.
As we are using lombock, so no need to write code for Getter and Setter.
For this article we are adding only one model as User, so that’s it for this module. Now let’s create new modules for Data-Repository, DTO-service, Services for all API and constructor. Below is screenshot of all modules.

Below is the pom.xml file for Data-Repository
DTO-Service module pom.xml file
pom.xml file for Services module.
Now, let’s create a DTO in DTO-Service named as SaveUserRequest under the package com.prajapati.hetaram.dto
Now we will add our main application file in Services module.
Let’s create userServiceApplication.java file under com.prajapati.hetarama package as per below
Now we will create an interface for User under com.prajapati.hetaram.service and class UserImpl under com.prajapati.hetaram.service.impl
Let’s create our controller in Services module under com.prajapati.hetaram.controller
Now we need to add DB configuration in application.yaml under resources folder as per below
That’s it, we are done with our code, we have implemented multi module spring boot application.
Now let’s see how to start it.
As we have added many dependencies so let’s update project first, as described earlier.
Now right click on project -> Run As-> Run Configurations. It will open a new dialog box.

Right click on Maven Build and click on New Configuration.

Add details as per above make sure to select the parent folder in Base directory using Workspace.
Click on Apply and run, it will start maven install, we will get the result as per below

Now let’s configure for run the service.
Open run configuration and right click on Maven build as we have did for build.

Configure it as per above, for base directory make sure to select the module where we have created the application class which have the main function from Workspace. And in Goals write spring-boot:run after that apply and run it.
After clicking on run, if all goes well the server will start.
Now if we want to run the server again we don’t need to repeat these activities, we can click on the arrow beside the run icon and select the action i.e. build or run.

Let’s try our add user API using postman.

As we have used String as return type so we got User added as simple text.
Conclusion
So with this we have created a spring boot application with multi module and seen how we can run it. If you want to have multiple micro-services then you can create a common service with model and repository and use it in all other services to avoid duplicate model and repositories and many more things i.e. logger, Swagger etc.
Please feel free if you have any suggestion, doubts to add it in the comment sections.
References