HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Maven / Maven multi-module project in eclipse

Maven multi-module project in eclipse

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).

Projects Relationship
Projects Relationship

1.1. Create parent project – packaging type ‘pom’

Create a new maven project in eclipse. Set it’s packaging type to ‘pom‘.

Create new maven project
Create new maven project
Fill maven group id and artifact id
Fill maven group id and artifact id
Change packaging jar to pom
Change packaging jar 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.

Create new maven module
Create new maven module
Add Module Name
Add Module Name
Fill archetype details
Fill archetype details

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"?>
<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 
        http://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 http://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 http://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>web</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>web</name>
	<url>http://maven.apache.org</url>
	<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
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://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 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.

2. FAQs

2.1. GroupId is duplicate of parent groupId

Maven child projects inherit properties from 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.

Remove duplicate entries
Remove duplicate entries

2.2. Artifact is not a dependency of the project

we may face this error when have added the module dependency (war) without specifying the type attribute.

Module type attribute
Module type attribute

Drop me yoiur question related to creating a multi-module maven project in eclipse ide.

Happy Learning !!

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. Arun

    March 28, 2020

    I’m case of moving this to GitHub repository do I have to move all three projects to GitHub repo.

    • Lokesh Gupta

      March 28, 2020

      Yes

  2. Manjunatha

    June 12, 2019

    How to run the builded applications beacause i cannot run with java jar beacause it is giving no manifest attribute?

  3. Steve Warsa

    January 5, 2019

    Hi,

    Can you think of any reason why the deployment assembly on an EAR project (in project properties) would be empty? I think that is causing the EAR to not contain any of the modules underneath, so that when I deploy it to the local JBoss server, it is not a valid EAR. Looking for any suggesting why that deployment assembly would be empty.

    Thanks,
    Steve

Comments are closed on this article!

Search Tutorials

Maven Tutorial

  • Maven – Installation
  • Maven – Settings
  • Maven – Dependency Mgmt
  • Maven – Dependency Scopes
  • Maven – POM
  • Maven – Parent POM
  • Maven – Repositories
  • Maven – Local Repo Path
  • Maven – M2_REPO
  • Maven – Network Proxy
  • Maven – Enforce Java Versions
  • Maven – Simple Java Project
  • Maven – Web Project
  • Maven – Multi-module Project 1
  • Maven – Multi-module Project 2
  • Maven – Java Source Folders
  • Maven – BOM [Bill Of Materials]
  • Maven – Import Remote Catalogs
  • Maven – Create Custom Archetype
  • Maven – Compiler Level Mismatch
  • Maven – Ant Build
  • Maven – IntelliJ
  • Maven – JSTL Support
  • Maven – Tomcat Plugin
  • Maven – Uber Jar for Spring Boot
  • Maven – Shade Plugin
  • Maven – Remove corrupt jars

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces