How to create Multi module Spring boot project using Dependency Management

We often need to create multi-modules for a project for several reasons. Today Let see How to create Spring boot Multi module project to use <dependencyManagement> using Intellij IDEA .

Why Dependency Management

Normally when we create spring boot project(using https://start.spring.io/) project generate parent as Spring boot. but if spring boot project is child module we can’t point out the main pom as because it already have parent pom added. This is a one reason we need to use Dependency Management in maven.

What is Dependency Management?

Dependency Management allows to consolidate and centralize the management of dependency versions without adding dependencies which are inherited by all children. This is especially useful when you have a set of projects (i.e. more than one) that inherits a common parent. So we Can add Spring boot as Centralized no need to add it as parent.

Let’s see some actions.

First you need to create main pom as maven project. Go to create New Project in Intellij and Select Maven from Left side. Then click next.

The Give any group id and artifact name in relevant fields like below.

On next window give a place to create the project and refactor name if necessary. Then click finish. Since we aren’t going to create anything in main project folder we don’t need src folder in there. So delete it. After that open pom.xml

Then add

<packaging>pom</packaging>

into the pom.xml like below

Now we need to add spring boot.

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
 
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

Now we need to child modules to the project.

Right click on main folder and New -> Module 

Then click next. As you can see below the module already inherited from parent pom. You only need give the child module name.

Next window you will ask place you want save the child module. Then click the Finish.

Now we have a child module. let’s enter Spring boot to the child module. Like below.

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
 
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

Now we need to add spring-boot-maven-plugin to the pom.xml

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

Now you are ready to use Spring boot on your child module. Create a spring boot application starter class

After that run below command in terminal (where child pom.xml is)

mvn spring-boot:run

If you type localhost:8080 on web browser and white label error in web browser that mean you child worked (of course errors because we didn’t configure the spring to return anything 😀 )

If you want full pom.xml files of modules check below.

  1. Parent Pom.xml
  2. Child Pom.xml

About Author:

Leave A Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.