HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Maven / Multi-module maven project – Console

Multi-module maven project – Console

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.

Projects Relationship
Projects Relationship

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"
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 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"?>
<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>
<?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>

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

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.

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.

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