HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Spring Boot 2 / Spring boot multi-module maven project example

Spring boot multi-module maven project example

Learn to create spring boot project having multiple modules. The parent project will work as container for base maven configurations. The child modules will be actual spring boot projects – inheriting the maven properties from parent project.

1. Spring boot maven parent project

The parent project is single point to trigger build process for all the modules and optionally generate a deployable package (e.g. ear, war etc). It’s pom.xml file consist the list of all modules as well as common dependencies and properties inherited by child projects.

As we are creating spring boot project, we will need to add spring-boot-starter-parent dependency. It is the parent POM providing dependency and plugin management for Spring Boot-based applications.

It contains the default versions of Java to use, the default versions of dependencies that Spring Boot uses, and the default configuration of the Maven plugins.

$ mvn archetype:generate -DgroupId=com.howtodoinjava
                        -DartifactId=HelloWorldApp
                        -DarchetypeArtifactId=maven-archetype-quickstart
                        -DinteractiveMode=false
<?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>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.0.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

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

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

</project>

2. Child modules – ear, war, jar

The child modules can be any project and can be creating any packaging. We can create any kind of dependency between modules and bundle them together as well. It is very much individual’s requirement specific.

In out case, we are creating an ear file, a war file and a jar file. Jar file is bundled into war file, which is bundled into ear file. The ear file is final package to be deployed to on application servers.

$ 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

Please add the 3rd-party libraries and dependecies as per requirements.

2.1. jar package

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

	<dependencies>
		<dependency>
			<groupId>com.h2database</groupId>
			<artifactId>h2</artifactId>
			<scope>runtime</scope>
		</dependency>
	</dependencies>
</project>

2.2. war package

<?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>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<!-- Package jar in the war file -->
		<dependency>
			<groupId>com.howtodoinjava</groupId>
			<artifactId>HelloWorldApp-service</artifactId>
			<version>1.0-SNAPSHOT</version>
		</dependency>

	</dependencies>

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

2.3. ear package

<?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>
		<!-- Package war in the ear file -->
		<dependency>
			<groupId>com.howtodoinjava</groupId>
			<artifactId>HelloWorldApp-rws</artifactId>
			<version>1.0-SNAPSHOT</version>
			<type>war</type>
		</dependency>
	</dependencies>

	<!-- Plugin to bundle the ear file-->
	<build>
		<pluginManagement>
			<plugins>
				<plugin>
					<groupId>org.apache.maven.plugins</groupId>
					<artifactId>maven-ear-plugin</artifactId>
					<version>3.0.1</version>
					<configuration>
						<finalName>HelloWorldApp-${project.version}</finalName>
						<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>

3. Maven build

To compile and build all projects in single command, go to parent project and run ‘mvn clean install’ command. This command will generate us an ear file with name HelloWorldApp-1.0-SNAPSHOT.ear.

E:\HelloWorldApp>mvn clean install
...
...
...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] HelloWorldApp 1.0-SNAPSHOT ......................... SUCCESS [  0.428 s]
[INFO] HelloWorldApp-service .............................. SUCCESS [  1.000 s]
[INFO] HelloWorldApp-rws Maven Webapp ..................... SUCCESS [  1.322 s]
[INFO] HelloWorldApp-ear 1.0-SNAPSHOT ..................... SUCCESS [  0.813 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.063 s
[INFO] Finished at: 2018-11-19T09:28:31+05:30
[INFO] ------------------------------------------------------------------------

Happy Learning !!

Read More:

Multi-module maven project using console
Multi-module maven project using eclipse

Share this:

  • Twitter
  • Facebook
  • LinkedIn
  • Reddit

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

    April 18, 2020

    I was getting issue like “child module of pom.xml does not exist” in the command at POINT 2, with the below command
    $ mvn archetype:generate -DgroupId=com.howtodoinjava
    -DartifactId=HelloWorldApp-ear
    -DarchetypeArtifactId=maven-archetype-quickstart
    -DinteractiveMode=false

    But it got resolved when I enveloped in the parent pom.xml by “”

  2. Vikrant Korde

    February 9, 2020

    How can i launch multi-module application?
    When i use “mvn spring-boot:run” it gives me below error

    [ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.2.0.RELEASE:run (default-cli) on project multi-module-library: Unable to find a suitable main class, please add a ‘mainClass’ property -> [Help 1]

    However when i execute “mvn clean install” it works fine and all modules shows success. what am i missing something here?

  3. Ajay Rathore

    November 27, 2019

    After creating the parent project, it you try to create child project it will fail, so change parent project packaging to pom

  4. Amol Dalvi

    September 6, 2019

    Hi, can you elaborate more about spring container behavior in this structure. I have same requirement in my project where a child project bean is getting used in my parent project. Now I am unsure how to do such configuration so that same bean will be useful for both parent and child project. Let me know any more details required. and Thank you so much for this post, it really helps!

Comments are closed on this article!

Search Tutorials

Spring Boot 2 Tutorial

  • Spring Boot – Introduction
  • Spring Boot – Starter parent
  • Spring Boot – Starter templates
  • Spring Boot – Multi-module project
  • Spring Boot – Annotations
  • Spring Boot – Auto configuration
  • Spring Boot – AOP
  • Spring Boot – Logging
  • Spring Boot – DevTools
  • Spring Boot – WAR Packaging
  • Spring Boot – REST API
  • Spring Boot – CRUD
  • Spring Boot – OAuth2
  • Spring Boot – Testing
  • Spring Boot – RestTemplate
  • Spring Boot – Thymeleaf
  • Spring Boot – Hibernate
  • Spring Boot – DataSource
  • Spring Boot – Error Handling
  • Spring Boot – Caching
  • Spring Boot – Retry
  • Spring Boot – BasicAuth
  • Spring Boot – H2 Database
  • Spring Boot – Ehcache 3.x
  • Spring Boot – Gson
  • Spring Boot – RMI
  • Spring Boot – Send Email
  • Spring Boot – Interview Questions

Spring Boot Tutorial

  • Spring Boot – CommandLineRunner
  • Spring Boot – Configure Jetty
  • Spring Boot – Tomcat Default Port
  • Spring Boot – Context Root
  • Spring Boot – SSL [https]
  • Spring Boot – Get all loaded beans
  • Spring Boot – PropertyEditor
  • Spring Boot – @EnableScheduling
  • Spring Boot – Jersey
  • Spring Boot – SOAP Webservice
  • Spring Boot – SOAP Client
  • Spring Boot – JMSTemplate
  • Spring Boot – REST APIs
  • Spring Boot – JSP View
  • Spring Boot – Actuator endpoints
  • Spring Boot – Role Based Security
  • Spring Boot – RSS / ATOM Feed
  • Spring Boot – Ehcache 2.x

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

  • Java 15 New Features
  • Sealed Classes and Interfaces
  • EdDSA (Ed25519 / Ed448)