Learn how to create multi module maven project in eclipse IDE. In this maven tutorial, we will learn to create nested maven projects in eclipse.
1. Create multi module maven project
Let’s create a maven project having modules packaging ear, war and jar types. We are creating the structure of an enterprise application where application will be deployed to applications servers (e.g. weblogic, websphere) as an EAR (Enterprise Application aRchive) file.
This EAR will contain one (or many) WAR (Web Application Resource) files and each war will contain the service project which has common code to all war files and packaging type in JAR (Java ARchive).
1.1. Create parent project – packaging type ‘pom’
Create a new maven project in eclipse. Set it’s packaging type to ‘pom‘.
1.2. Create maven module – packaging type ‘ear’
Create a new maven module in the parent project. Change its packaging type to
'ear'
. This module can have maven ear plugin which will ultimately build the ear file to be deployed on servers.1.3. Create maven modules – packaging type ‘war’ and ‘jar’
Similar to ear module, create two more modules for war file and service jar file. Change their packaging respectively and add maven plugins.
1.4. Final result
Now observe the final project structure in eclipse and verify the pom.xml files for all projects.
<? 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.app</ groupId > < artifactId >HelloWorldApp</ artifactId > < version >0.0.1-SNAPSHOT</ version > < packaging >pom</ packaging > < 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 >web</ module > < module >service</ module > < module >ear</ module > </ modules > </ project > |
<? xml version = "1.0" ?> < project < modelVersion >4.0.0</ modelVersion > < parent > < groupId >com.howtodoinjava.app</ groupId > < artifactId >HelloWorldApp</ artifactId > < version >0.0.1-SNAPSHOT</ version > </ parent > < artifactId >ear</ artifactId > < name >ear</ name > < packaging >ear</ packaging > < properties > < project.build.sourceEncoding >UTF-8</ project.build.sourceEncoding > </ properties > < dependencies > < dependency > < groupId >com.howtodoinjava.app</ groupId > < artifactId >web</ artifactId > < version >0.0.1-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.app</ groupId > < artifactId >web</ artifactId > < uri >web-0.0.1-SNAPSHOT.war</ uri > <!-- Set custom context root --> < contextRoot >/application</ contextRoot > </ webModule > </ modules > </ configuration > </ plugin > </ plugins > </ pluginManagement > </ build > </ project > |
<? xml version = "1.0" ?> < project < modelVersion >4.0.0</ modelVersion > < parent > < groupId >com.howtodoinjava.app</ groupId > < artifactId >HelloWorldApp</ artifactId > < version >0.0.1-SNAPSHOT</ version > </ parent > < groupId >com.howtodoinjava.app</ groupId > < artifactId >web</ artifactId > < version >0.0.1-SNAPSHOT</ version > < name >web</ name > < packaging >war</ packaging > < properties > < project.build.sourceEncoding >UTF-8</ project.build.sourceEncoding > </ properties > < dependencies > < dependency > < groupId >com.howtodoinjava.app</ groupId > < artifactId >service</ artifactId > < version >0.0.1-SNAPSHOT</ version > </ dependency > </ dependencies > </ project > |
<? xml version = "1.0" ?> < project < modelVersion >4.0.0</ modelVersion > < parent > < groupId >com.howtodoinjava.app</ groupId > < artifactId >HelloWorldApp</ artifactId > < version >0.0.1-SNAPSHOT</ version > </ parent > < groupId >com.howtodoinjava.app</ groupId > < artifactId >service</ artifactId > < version >0.0.1-SNAPSHOT</ version > < name >service</ name > < packaging >jar</ packaging > < properties > < project.build.sourceEncoding >UTF-8</ project.build.sourceEncoding > </ properties > < dependencies > < dependency > < groupId >junit</ groupId > < artifactId >junit</ artifactId > < version >3.8.1</ version > < scope >test</ scope > </ dependency > </ dependencies > </ project > |
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] service [jar] [INFO] web [war] [INFO] ear [ear] [INFO] ... ... [INFO] --- maven-jar-plugin: 2.4 :jar ( default -jar) @ service --- [WARNING] JAR will be empty - no content was marked for inclusion! [INFO] Building jar: E:\devsetup\workspacetemp\HelloWorldApp\service\target\service- 0.0 . 1 -SNAPSHOT.jar [INFO] ... ... [INFO] Packaging webapp [INFO] Assembling webapp [web] in [E:\devsetup\workspacetemp\HelloWorldApp\web\target\web- 0.0 . 1 -SNAPSHOT] [INFO] Processing war project [INFO] Copying webapp resources [E:\devsetup\workspacetemp\HelloWorldApp\web\src\main\webapp] [INFO] Webapp assembled in [ 47 msecs] [INFO] Building war: E:\devsetup\workspacetemp\HelloWorldApp\web\target\web- 0.0 . 1 -SNAPSHOT.war [INFO] WEB-INF\web.xml already added, skipping [INFO] ... ... [INFO] [INFO] --- maven-ear-plugin: 3.0 . 1 :ear ( default -ear) @ ear --- [INFO] Copying artifact [war:com.howtodoinjava.app:web: 0.0 . 1 -SNAPSHOT] to [web- 0.0 . 1 -SNAPSHOT.war] [INFO] Copy ear sources to E:\devsetup\workspacetemp\HelloWorldApp\ear\target\ear- 0.0 . 1 -SNAPSHOT [INFO] Building jar: E:\devsetup\workspacetemp\HelloWorldApp\ear\target\ear- 0.0 . 1 -SNAPSHOT.ear [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Reactor Summary: [INFO] [INFO] HelloWorldApp 0.0 . 1 -SNAPSHOT ....................... SUCCESS [ 0.328 s] [INFO] service ............................................ SUCCESS [ 0.839 s] [INFO] web ................................................ SUCCESS [ 0.838 s] [INFO] ear 0.0 . 1 -SNAPSHOT ................................. SUCCESS [ 0.588 s] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.749 s [INFO] Finished at: 2018 - 11 -18T15: 04 : 52 + 05 : 30 [INFO] ------------------------------------------------------------------------ |
The build generates an ear file with name
ear-0.0.1-SNAPSHOT.ear
. Feel free to change the name of projects and generated packages as per your need.