Learn how to create multi-module maven project from console IDE. In this maven tutorial, we will learn to create nested maven projects maven cli commands.
1. Maven projects
1.1. Maven parent project
The parent maven project is of packaging type ‘pom’. It makes the project as an aggregator – it won’t produce further artifacts.
1.2. Maven child projects/modules
- Maven child projects are independent maven projects but inherit properties from parent project.
- All child projects inside a parent project can be build with a single command.
- It’s easier to define the relationships between the projects. Such as a jar project can be packaged into a war project.
2. Create multi-module maven project from console
Let’s create a maven project having modules packaging ear, war and jar types. Notice how
archetypeArtifactId
is set to pom-root.1.1. Create parent project – packaging type ‘pom’
Create the parent project will below command.
$ mvn archetype:generate -DgroupId=com.howtodoinjava -DartifactId=HelloWorldApp -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode= false |
Open pom.xml file and change packaging type to ‘pom’.
<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" <modelVersion> 4.0 . 0 </modelVersion> <groupId>com.howtodoinjava</groupId> <artifactId>HelloWorldApp</artifactId> <!-- chaged fom jar to pom --> <packaging>pom</packaging> <version> 1.0 -SNAPSHOT</version> <name>HelloWorldApp</name> <url>http: //maven.apache.org</url> </project> |
1.2. Create ear, web and services modules
In console, navigate to parent project folder and create further modules for services (common code e.g. DAO), rws (rest services or web component) and ear.
$ cd HelloWorldApp $ mvn archetype:generate -DgroupId=com.howtodoinjava -DartifactId=HelloWorldApp-ear -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode= false $ mvn archetype:generate -DgroupId=com.howtodoinjava -DartifactId=HelloWorldApp-service -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode= false $ mvn archetype:generate -DgroupId=com.howtodoinjava -DartifactId=HelloWorldApp-rws -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode= false |
1.3. Update maven plugins and dependencies
It’s time to update add the dependencies (e.g. add services to war file, and war file inside ear file). Also all maven plugins to build ear and war files artifacts.
<? xml version = "1.0" encoding = "UTF-8" ?> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 < modelVersion >4.0.0</ modelVersion > < groupId >com.howtodoinjava</ groupId > < artifactId >HelloWorldApp</ artifactId > < packaging >pom</ packaging > < version >1.0-SNAPSHOT</ version > < name >HelloWorldApp</ name > < properties > < project.build.sourceEncoding >UTF-8</ project.build.sourceEncoding > < maven.compiler.source >1.8</ maven.compiler.source > < maven.compiler.target >1.8</ maven.compiler.target > </ properties > < modules > < module >HelloWorldApp-ear</ module > < module >HelloWorldApp-service</ module > < module >HelloWorldApp-rws</ module > </ modules > </ project > |
<? xml version = "1.0" ?> < project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 < modelVersion >4.0.0</ modelVersion > < parent > < groupId >com.howtodoinjava</ groupId > < artifactId >HelloWorldApp</ artifactId > < version >1.0-SNAPSHOT</ version > </ parent > < artifactId >HelloWorldApp-ear</ artifactId > < name >HelloWorldApp-ear</ name > < packaging >ear</ packaging > < dependencies > < dependency > < groupId >com.howtodoinjava</ groupId > < artifactId >HelloWorldApp-rws</ artifactId > < version >1.0-SNAPSHOT</ version > < type >war</ type > </ dependency > </ dependencies > < build > < pluginManagement > < plugins > < plugin > < groupId >org.apache.maven.plugins</ groupId > < artifactId >maven-ear-plugin</ artifactId > < version >3.0.1</ version > < configuration > < modules > < webModule > < groupId >com.howtodoinjava</ groupId > < artifactId >HelloWorldApp-rws</ artifactId > < uri >HelloWorldApp-rws-1.0-SNAPSHOT.war</ uri > <!-- Set custom context root --> < contextRoot >/application</ contextRoot > </ webModule > </ modules > </ configuration > </ plugin > </ plugins > </ pluginManagement > </ build > </ project > |
<? xml version = "1.0" ?> < project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 < modelVersion >4.0.0</ modelVersion > < parent > < groupId >com.howtodoinjava</ groupId > < artifactId >HelloWorldApp</ artifactId > < version >1.0-SNAPSHOT</ version > </ parent > < artifactId >HelloWorldApp-rws</ artifactId > < packaging >war</ packaging > < name >HelloWorldApp-rws Maven Webapp</ name > < dependencies > < dependency > < groupId >com.howtodoinjava</ groupId > < artifactId >HelloWorldApp-service</ artifactId > < version >1.0-SNAPSHOT</ version > </ dependency > </ dependencies > < build > < finalName >HelloWorldApp-rws</ finalName > </ build > </ project > |
<? xml version = "1.0" ?> < project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 < modelVersion >4.0.0</ modelVersion > < parent > < groupId >com.howtodoinjava</ groupId > < artifactId >HelloWorldApp</ artifactId > < version >1.0-SNAPSHOT</ version > </ parent > < artifactId >HelloWorldApp-service</ artifactId > < name >HelloWorldApp-service</ name > </ project > |
2. Project build process
To build the projects, run $ mvn clean install command from console.
E:\devsetup\workspacetemp\HelloWorldApp>mvn clean install [INFO] Scanning for projects... [INFO] ------------------------------------------------------------------------ [INFO] Reactor Build Order: [INFO] [INFO] HelloWorldApp [pom] [INFO] HelloWorldApp-service [jar] [INFO] HelloWorldApp-rws Maven Webapp [war] [INFO] HelloWorldApp-ear [jar] [INFO] [INFO] --- maven-jar-plugin: 2.4 :jar ( default -jar) @ HelloWorldApp-service --- [WARNING] JAR will be empty - no content was marked for inclusion! [INFO] Building jar: E:\devsetup\workspacetemp\HelloWorldApp\HelloWorldApp-service\target\HelloWorldApp-service- 1.0 -SNAPSHOT.jar [INFO] [INFO] Copying webapp resources [E:\devsetup\workspacetemp\HelloWorldApp\HelloWorldApp-rws\src\main\webapp] [INFO] Webapp assembled in [ 47 msecs] [INFO] Building war: E:\devsetup\workspacetemp\HelloWorldApp\HelloWorldApp-rws\target\HelloWorldApp-rws.war [INFO] WEB-INF\web.xml already added, skipping [INFO] [INFO] --- maven-install-plugin: 2.4 :install ( default -install) @ HelloWorldApp-ear --- [INFO] Installing E:\devsetup\workspacetemp\HelloWorldApp\HelloWorldApp-ear\target\HelloWorldApp-ear- 1.0 -SNAPSHOT.ear to E:\devsetup\M2\com\howtodoinjava\HelloWorldApp-ear\ 1.0 -SNAPSHOT\HelloWorldApp-ear- 1.0 -SNAPSHOT.ear [INFO] ------------------------------------------------------------------------ [INFO] Reactor Summary: [INFO] [INFO] HelloWorldApp 1.0 -SNAPSHOT ......................... SUCCESS [ 0.324 s] [INFO] HelloWorldApp-service .............................. SUCCESS [ 0.894 s] [INFO] HelloWorldApp-rws Maven Webapp ..................... SUCCESS [ 0.531 s] [INFO] HelloWorldApp-ear 1.0 -SNAPSHOT ..................... SUCCESS [ 0.565 s] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.487 s [INFO] Finished at: 2018 - 11 -18T22: 44 : 44 + 05 : 30 [INFO] ------------------------------------------------------------------------ |
The build generates an ear file with name
HelloWorldApp-ear-1.0-SNAPSHOT.ear
. Feel free to change the name of projects and generated packages as per your need.