How to Create Multi-Module Maven Project in IntelliJ IDEA
- Details
- Written by Nam Ha Minh
- Last Updated on 02 August 2021   |   Print Email
In this article, I’d like to share with you guys on how to create and build a Java Maven project that consists of multiple modules using IntelliJ IDEA.
Given a scenario in which we need to develop a Java project that has two types of apps: Console app and Desktop app. Each app has its own code, and they also share some common code which is in a Shared Library, as depicted in the following picture:
So there would be 3 different projects: Shared Library, Console App and Desktop App. The Console App and Desktop App projects have to use the common code in the Shared Library project.
Using Maven, we will need to create a project acts as the root project for the modules. Each module is a separate project right inside the root project, as shown below:
Note that the packaging type of the root project must be pom. And the packaging type of modules can be jar or war.
Now, let’s create this multi-module project in IntelliJ IDEA.
1. Create the root Maven project
In IntelliJ IDEA’s welcome dialog, click New Project. Choose Maven and click Next:
Enter project name and specify artifact coordinates information as below:
Click Finish. It will create a simple Maven project.
2. Create the SharedLibrary project
Next, we create the first Maven module for the shared library project. Right-click on the root project, and select New > Module:
Then enter the module name as SharedLibrary. Note that the parent module is CompanyProject:
Click Finish. IntelliJ IDEA will create the SharedLibrary project under CompanyProject. You can notice the pom.xml file of the root project was updated:
<project ...> [...] <groupId>com.mycompany</groupId> <artifactId>CompanyProject</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <modules> <module>SharedLibrary</module> </modules> [...] </project>
You see, it specifies the packaging type is pom, and adds a module named SharedLibrary. And in this SharedLibrary project’s pom.xml file, it specifies the parent information as below:
<project ...> <parent> <artifactId>CompanyProject</artifactId> <groupId>com.mycompany</groupId> <version>1.0-SNAPSHOT</version> </parent> <artifactId>SharedLibrary</artifactId> [...] </project>
And code a simple class in this project as follows:
package com.mycompany; public class CommonUtility { public static String getAppName() { return "My Company App Beta version"; } }
The static method getAppName() will be used by both console app and desktop app.
3. Create the ConsoleApp project
Similarly, create the second Maven module for the ConsoleApp project. And in order to reference the SharedLibrary project, you should declare the dependency in the pom.xml file of the ConsoleApp project as follows:
<project ...> [..] <artifactId>ConsoleApp</artifactId> <dependencies> <dependency> <groupId>com.mycompany</groupId> <artifactId>SharedLibrary</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> [..] </project>
Note: You may need to Reload Maven project to update the dependency information.
Then code a simple console program like this:
package com.mycompany; public class ConsoleApp { public static void main(String[] args) { String appName = CommonUtility.getAppName(); System.out.println("Welcome to " + appName); } }
You see, this class makes use of the CommonUtility class from the SharedLibrary project.
4. Create the DesktopApp project
Similar to the ConsoleApp project, create the third Maven module for the DesktopApp project. We would end up having the modules declared in the root project like this:
<project ...> <groupId>com.mycompany</groupId> <artifactId>CompanyProject</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <modules> <module>SharedLibrary</module> <module>ConsoleApp</module> <module>DesktopApp</module> </modules> [...] </project>
For demo purpose, code a simple Swing program for the desktop app with the following code:
package com.mycompany; import javax.swing.*; import java.awt.*; public class DesktopApp extends JFrame { static String appName = CommonUtility.getAppName(); public DesktopApp() { super(appName); init(); } private void init() { setLayout(new FlowLayout()); add(new JLabel("Welcome to " + appName)); setDefaultCloseOperation(EXIT_ON_CLOSE); setSize(640, 480); setLocationRelativeTo(null); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new DesktopApp().setVisible(true); } }); } }
You see, this class also makes use of the CommonUtility class from the SharedLibrary project.
5. Build a Multi-Module Maven project in IntelliJ IDEA
To build the whole multi-module Maven project in IntelliJ IDEA, open Maven view. Select the root project, and click button Execute Maven Goal, and double click mvn install from the list, as shown below:
Then you should see the Maven’s build success output as follows:
Now you can check the target directory in each project. Maven should have generated jar file for each project there.
That’s how to create a multi-module Maven project in IntelliJ IDEA. To see the steps and coding in action, I recommend you to watch the following video:
Related Articles:
- Introduction to IntelliJ IDEA for beginner
- How to download and install IntelliJ IDEA
- Getting Familiar with IntelliJ IDEA - Tutorial for beginner
Comments