Multi-Module Maven Project using Console

Learn how to create a 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 to Create

1.1. Parent Project

The parent maven project is of packaging type ‘pom‘. It makes the project an aggregator – it won’t produce other artifacts.

1.2. Maven Child Projects or 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. Creating Multi-module Maven Project from Console

Let’s create a maven project having modules packaging ear, war, and jar types.

Projects Relationship
Projects Relationship

1.1. Create Parent Project with Packaging type ‘pom’

Create the parent project will the 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"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<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 Child Projects

In the console, navigate to the parent project folder and create further modules for services (common code and DAO), rws (rest services or web component), and ear (final deployment package).

$ 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 all the dependencies (e.g., add services to the war file and add the war file inside the ear file). Also, all maven plugins to build ear and war files artifacts.

<?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/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.howtodoinjava</groupId>
	<artifactId>HelloWorldApp</artifactId>
	<packaging>pom</packaging>
	<version>1.0-SNAPSHOT</version>
	<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>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
	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</groupId>
		<artifactId>HelloWorldApp</artifactId>
		<version>1.0-SNAPSHOT</version>
	</parent>
	<artifactId>HelloWorldApp-ear</artifactId>
	<name>HelloWorldApp-ear</name>
	<url>http://maven.apache.org</url>

	<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
	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</groupId>
		<artifactId>HelloWorldApp</artifactId>
		<version>1.0-SNAPSHOT</version>
	</parent>

	<artifactId>HelloWorldApp-rws</artifactId>
	<packaging>war</packaging>
	<name>HelloWorldApp-rws Maven Webapp</name>
	<url>http://maven.apache.org</url>

	<dependencies>
		 <dependency>
            <groupId>com.howtodoinjava</groupId>
            <artifactId>HelloWorldApp-service</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
	</dependencies>

	<build>
		<finalName>HelloWorldApp-rws</finalName>
	</build>
</project>

Read More : How to create multi-module maven project in eclipse

<?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</groupId>
		<artifactId>HelloWorldApp</artifactId>
		<version>1.0-SNAPSHOT</version>
	</parent>

	<artifactId>HelloWorldApp-service</artifactId>
	<name>HelloWorldApp-service</name>
	<url>http://maven.apache.org</url>
</project>

2. Building the Project

To build the projects, run $ mvn clean install command on parent’s pom.xml file.

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] ------------------------------------------------------------------------E:\devsetup\workspacetemp\HelloWorldApp&gt;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 the name HelloWorldApp-ear-1.0-SNAPSHOT.ear.

Feel free to change the name of projects and generated packages as per your need.

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.