Learn how to create a multi-module maven project in eclipse IDE. In this maven tutorial, we will learn to create nested maven projects in Eclipse. 1. Multi-module Project Structure Let’s create a maven project having modules packaging ear, war and jar types. We are creating the structure of …
Learn how to create a multi-module maven project in eclipse IDE. In this maven tutorial, we will learn to create nested maven projects in Eclipse.
Let’s create a maven project having modules packaging ear, war and jar types. We are creating the structure of an enterprise application where the 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 include the service project which has common code to all war files.
Create a new maven project in eclipse. Set it’s packaging type to ‘pom‘.
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.
Similar to ear module, create two more modules for war file and service jar file. Change their packaging respectively and add maven plugins.
Now observe the final project structure in eclipse and verify the pom.xml files for all projects.
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <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> <url>http://maven.apache.org</url> <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 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <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 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <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 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <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> <url>http://maven.apache.org</url> <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 the 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.
Maven child projects inherit properties from the parent project. If either group id or version id is same as parent project for any maven module/project, then we can simply remove this entry.
We may face this error when have added the module dependency (war) without specifying the type attribute.
Drop me your question related to creating a multi-module maven project in eclipse IDE.
Happy Learning !!