Last Updated on 03 August 2021   |   Print Email
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: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: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:
Then enter project name and specify information for the Maven project like this: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:Then in the New Project Dialog, choose Java Application in Java with Maven:Click Next. And specify information for the SharedLibrary project as follows: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:
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:
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:
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: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:
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.
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
Comments