You use maven in Eclipse, and need to work with multi-module (hierarchical) projects?
This page shows how to create and work with maven multi-module projects in Eclipse.
For this article, I am using eclipse-jee-2019-06, which already supports maven. If you use a different version of eclipse, make sure the m2e plugin is installed (+m2e-wtp for java-web)
maven multi-module
A Maven multi-module project consists of a parent module that manages a group of submodules. Most of professional java maven applications are built with multi modules.
In this example, we’re going to build a typical multi-module project, structured as follows:
- project1: parent project packaged as pom. An empty project with a pom.xml used to manage the build of all modules.
- lib, app, and test: submodules packaged as a jar. Interdependent java project whose build results in the production of jars.
Parent module
Open the new wizard with File > New > Other (or Ctrl+N/Cmd+N). Then select General > Project, and name the project com.asciiware.maven.project1
Convert the project to maven nature: Select it, right-click and choose Configure > Convert to > Maven Project.
When converting, provide Group Id and Artifact Id, and then set Packaging to pom
type.
After completing the wizard, our maven parent project is ready.
Child projects (modules)
Now we add three maven child modules: lib, app and test. Note that creating a maven module will create a new project inside the selected parent project.
Start creating the app module: Select the parent module and open the “new..” wizard (Ctrl+N/Cmd+N). Then, select Maven Module and press Next.
In the next page, select to Create a simple project .. , set the Module Name to com.asciiware.maven.project1.lib
as you see below, and press Next.
The child module will be in the same group as the parent; so in the next page, set Group Id to com.asciiware.maven.project1
, make sure the Packaging is jar
and press Finish.
Repeat this procedure for the other two: com.asciiware.maven.project1.app
and com.asciiware.maven.project1.test
.
When done, you will have your projects organized in a hierarchical structure.
Note the difference between the Project Explorer (left) and Package Explorer (right). In the Project Explorer you will see the projects in a hierarchy, while in the Package Explorer you will see the projects arranged in a flat fashion.
Project dependencies
The project app
depends on lib
and test
depends on both lib
and app
. In this section we set the project dependencies.
First, we set the dependency from app to lib. Open the pom.xml for the app project, switch to the dependencies tab, and then add the dependency.
Now, add also the dependency from test to app, and from test to lib
Project code
Here we add some sample code
The app is a command line application. It takes the command line parameters, and execute methods that are provided in the library,
The library contains a class
public class Parameters {
String [] cliArguments;
public Parameters( String [] arguments ){
cli
}
public String getInfo(){
}
}
In the app, we add an Application class, that we run from command line
public class Application {
public static void main(String [] args){
Parameters params = new Parameters(args);
}
}
In the test, we write a test class to test both the lib and the app
Build the projects
Right click on the parent pom.xml, and then select Run As > Maven build …
In the next dialog, type clean install
as goals for the maven build, and press the run button. When done, you will see a maven build success message like you see below
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for com.asciiware.maven.project1 0.0.1-SNAPSHOT:
[INFO]
[INFO] com.asciiware.maven.project1 ....................... SUCCESS [ 0.388 s]
[INFO] com.asciiware.maven.project1.lib ................... SUCCESS [ 0.933 s]
[INFO] com.asciiware.maven.project1.app ................... SUCCESS [ 0.051 s]
[INFO] com.asciiware.maven.project1.test .................. SUCCESS [ 0.063 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
ss
0 Comments