Create Spring multi-module project in Intellij on MacOS

KARAN VERMA
Mac O’Clock
Published in
5 min readMay 16, 2020

Creating a Spring-boot multi-module project in Intellij IDEA and maintaining such project can land lots of developers to great trouble.

This article is all about creating the spring multi-module project, maintain it, troubleshoot things and all configuration related matter.

So, here is what we are going to cover in this:

  1. What is sprint multi-module
  2. Why multi-module
  3. Create a spring-boot multi-module project
  4. Add one more module!!!

What is a multi-module project

A multi-module project is one in which there could be child-parent relationships between different components of your project. And you want all your code in a single project, separated apart in different components, having one or more common dependencies and can be executed as a single or multiple jars or deployments.

Why a multi-module project

As your project scope increases, you come with adding a lot of files and classes, which can sometimes make your code difficult to traverse and find files.

To solve this most developers divide your files and components in different packages, but in a single project. But that itself can become difficult to understand and maintain if there are a lot of packages.

Thus, comes the modules which you can have for different functionality or business use case and separated all together, but maintained as a single project and single git repository. Sounds interesting, isn’t it!!!

Create a spring-boot multi-module project in intellij

So lets create a multi-module project using intellij IDEA Ultimate edition. I will be creating a spring-boot project with four modules. So lets start creating it.

  1. Open the Intellij and go to File-> New -> Project. From the left tab, select Spring-initializr and select Project SDK in right pane and click next.

2. In the next window, enter the group and artifact and choose the type as Maven POM, which will basically specify that you want to create a multi-module project and click next. Selecting Maven pom also tell the intellij to not include the src directory in you parent project.

3. In the next window, select dependencies whichever you want. For simplicity, we will go with the Spring web dependency only and click next.

4. Next enter the project name and finish.

5. So now the intellij project will open with just the pom.xml and no other major directories, no src directory as well. And the pom is created as the parent pom.

6. Now let’s add modules, where will be writing code and executing our business logic. So we will be creating the four modules as follows:

Application module — which will have the spring boot main class Application.java

Utilities module — A common module which can be used by all other modules independently

auth-module — A module for catalog stuff

stream-module — A module for some streams which uses auth module and utilities module.

So right click on your project, Right click -> New -> Module

In the left pane, select Maven and in right pane select SDK and click next.

Select parent as your parent project and enter name for your module and click finish.

Now a new module will be added in you project directory with src directory and a separate pom.xml with the parent project pom as the parent.

Now you can create a package under src/main/java folder and create your sprint boot Application class.

Similarly, we can create more modules. So now we have all the modules created in our project. Now comes the major part of dependency management and project configuration.

The dependencies are as follows:

a. App modules is where the spring boot loads. And it will have dependency to all module so that all modules can get up at run time. One thing to note here is that whichever dependencies are there in pom of app-module, only those will be initialed as spring beans and only those classes or controller would be available at runtime.

b. Utilities is required by both auth and stream module.

c. Stream requires auth module.

Now you can add you controller and service in any of the services and simply start the spring boot application using following command:

mvn spring-boot:run

Add one more module!!!

So its simple to add a new module in your existing project as well.

But if there is any controller or spring beans to be created in the new module, make sure to add you new module dependency in the app-module.

If you do not add the new module dependency in app-module, your controllers will not be invoked and you will get 404 error for that particular api.

Actually the 404 error was the whole reason of writing out this article. If you want to save your precious time, take care of this step for sure.

Hope this all helps developers enjoy coding!!!

--

--