Through this tutorial, you will learn how to create Java Maven project that consists of multiple modules in Apache NetBeans IDE – follow this project development scenario:

Multi module project scenario

This scenario supposes that you have to develop a project that consists of 3 modules: Shared library, Console app and Desktop app. Each app has its own code, and they also share some common code in the Shared Library.

With Maven, you will need to create the root project as the parent of the modules. Each module is a separate project right inside the root project, as shown below:

Multi Module Maven Project structure

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 Apache NetBeans IDE.

 

1. Create the root Maven project

In NetBeans, click File > New Project. Then choose Java with Maven in Categories, and select POM Project in Projects:



New Maven Pom project

Then enter project name and specify information for the Maven project like this:

New POM Project info

Click Finish. NetBeans will create a simple Maven POM project that acts as the root project for sub modules.


2. Create the first Maven module

Next, let’s create the first Maven module for the shared library project. Right-click on the Modules node of the root project, and click Create New Module from the context menu:

Create New Module Context Menu

Then in the New Project Dialog, choose Java Application in Java with Maven:

New Maven Java Application Project

Click Next. And specify information for the SharedLibrary project as follows:

Create Shared Library Module Project

Click Finish. NetBeans will create the SharedLibrary project under CompanyProject. You can notice a new module is added to the root project’s pom.xml file as below:

<project ...>
    
    [...]
    
    <groupId>com.mycompany</groupId>
    <artifactId>CompanyProject</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    
    <modules>
        <module>SharedLibrary</module>
    </modules>

    [...]    

</project>
As you can notice, the packaging type of the root project is pom. And in the SharedLibrary project, its parent information is specified as below:

<project ...>

    <parent>
        <artifactId>CompanyProject</artifactId>
        <groupId>com.mycompany</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>SharedLibrary</artifactId>

    [...]
    
</project>
Then code a simple class in the SharedLibrary project as follows:

package com.mycompany;

public class CommonUtility {

    public static String getAppName() {
        return "My Company App standard version";
    }
}
The static method getAppName() will be used by both console app and desktop app.


3. Create the second Maven module

Next, let’s create the second Maven module for the ConsoleApp project similarly to the first module. And in the ConsoleApp project, it should refer to the SharedLibrary project in the pom.xml file follows:

<project ...>
    [..]

    <artifactId>ConsoleApp</artifactId>

    <dependencies>
        <dependency>
            <groupId>com.mycompany</groupId>
            <artifactId>SharedLibrary</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

    [..]

</project>
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);
    }
}
Here, this class makes use of the CommonUtility class from the SharedLibrary project.


4. Create the third Maven module

Similar to the ConsoleApp project, create the third Maven module for the DesktopApp project. The root project’s pom.xml would end up being updated for 3 modules as follows:

<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 program 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 Apache NetBeans

To build the whole multi-module Maven project in NetBeans, right click on the root project, and click Build. Maven will build this multi-module project including sub modules as follows:

Build multi module project with maven

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 Apache NetBeans IDE. To see the steps and coding in action, I recommend you to watch the following video:

 

Other NetBeans Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.



Add comment

   


Comments 

#1Aniket Poptani2023-06-14 00:45
This is deprecated and removed from the new version, after creating the POM project, you are unable to add modules into it. SO, go on with Netbeans Modules only
Quote