HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Maven / Maven – Parent and Child POM Example

Maven – Parent and Child POM Example

Maven parent POM (or super POM) is used to structure the project to avoid redundancies or duplicate configurations using inheritance between pom files. It helps in easy maintenance in long term.

If any dependency or property is configured in both parent and child POMs with different values then the child POM value will take the priority.
Table of Contents

Parent POM Contents
Parent POM and Child POM
Parent POM Relative Path
Demo

Parent POM Contents

A parent POM can be declared with packaging pom. It is not meant to be distributed because it is only referenced from other projects.

Maven parent pom can contain almost everything and those can be inherited into child pom files e.g

  • Common data – Developers’ names, SCM address, distribution management etc.
  • Constants – Such as version numbers
  • Common dependencies – Common to all child. It has same effect as writing them several times in individual pom files.
  • Properties – For example plugins, declarations, executions and IDs.
  • Configurations
  • Resources

Parent POM and Child POM Example

To match a parent POM, Maven uses two rules:

  1. There is a pom file in project’s root directory or in given relative path.
  2. Reference from child POM file contains the same coordinates as stated in the parent POM file.

Parent POM

Here parent POM has configured basic project information and two dependencies for JUnit and spring framework.

<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.demo</groupId>
	<artifactId>MavenExamples</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>pom</packaging>

	<name>MavenExamples Parent</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<junit.version>3.8.1</junit.version>
		<spring.version>4.3.5.RELEASE</spring.version>
	</properties>

	<dependencies>
	
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>${junit.version}</version>
			<scope>test</scope>
		</dependency>
		
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${spring.version}</version>
		</dependency>
		
	</dependencies>
</project>

Child POM

Now child POM need to refer the parent POM using parent tag and specifying groupId/artifactId/version attributes. This pom file will inherit all properties and dependencies from parent POM and additionally can include extra sub-project specific dependencies as well.

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

	<!--The identifier of the parent POM-->
	<parent>
		<groupId>com.howtodoinjava.demo</groupId>
		<artifactId>MavenExamples</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>

	<modelVersion>4.0.0</modelVersion>
	<artifactId>MavenExamples</artifactId>
	<name>MavenExamples Child POM</name>
	<packaging>jar</packaging>

	<dependencies>		
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-security</artifactId>
			<version>${spring.version}</version>
		</dependency>
	</dependencies>

</project>

Parent POM Relative Path

By default, Maven looks for the parent POM first at project’s root, then the local repository, and lastly in the remote repository. If parent POM file is not located in any other place, then you can use code tag. This relative path shall be relative to project root.

The relative path, if not given explicitly, defaults to .., i.e. the pom in the parent directory of the current project.
<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">

	<!--The identifier of the parent POM-->
	<parent>
		<groupId>com.howtodoinjava.demo</groupId>
		<artifactId>MavenExamples</artifactId>
		<version>0.0.1-SNAPSHOT</version>
		<relativePath>../baseapp/pom.xml</relativePath>
	</parent>

	<modelVersion>4.0.0</modelVersion>
	<artifactId>MavenExamples</artifactId>
	<name>MavenExamples Child POM</name>
	<packaging>jar</packaging>

</project>

Demo

Let’s learn to create maven projects with parent child relationship.

1) Create Maven Parent Project

Project creation wizard.

Create Maven Project Wizard
Create Maven Project Wizard

Select Project Archtype.

Maven Quick Start Archtype
Maven Quick Start Archtype

Fill details and create project.

Create Maven Parent Project
Create Maven Parent Project

Now change packaging from jar to pom in pom.xml.

<packaging>jar</packaging> //previous

<packaging>pom</packaging> //New

Additionally, add project properties and dependencies.

<properties>
	<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	<junit.version>3.8.1</junit.version>
	<spring.version>4.2.3.RELEASE</spring.version>
</properties>

<dependencies>
	<dependency>
		<groupId>junit</groupId>
		<artifactId>junit</artifactId>
		<version>${junit.version}</version>
		<scope>test</scope>
	</dependency>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-core</artifactId>
		<version>${spring.version}</version>
	</dependency>
</dependencies>

2) Create Maven Child Project

Create a new maven project just like you did for parent project. Change project specific details like it’s name etc.

Create Maven Child Project
Create Maven Child Project

Now update child project’s pom.xml file with parent reference.

<!--The identifier of the parent POM -->
<parent>
	<groupId>com.howtodoinjava.demo</groupId>
	<artifactId>MavenExamples</artifactId>
	<version>0.0.1-SNAPSHOT</version>
</parent>

Now you are free to use parent pom’s elements such as properties. You child project has inherited the parent project. To test this, just remove all dependencies from child project’s pom.xml.

Now check it’s libraries in Java Build Path. You will see all parent’s dependencies there.

Java Build Path of Child Project
Java Build Path of Child Project

Drop me your questions in comment section.

Happy Learning !!

Ref: Maven Inheritance

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

    July 3, 2020

    Parent common dependency jars are available in child under lib folder and it is. working in STS/ ECLIPSE/RAD/ or IDE.But when same used do mvn clean install, manifest file dependency jars are not appended. Due to this we are getting run time error class not found

  2. Aks

    May 22, 2020

    What if I want to use parent classes in child module how to do that in spring?

  3. Omar

    December 17, 2019

    Hi, I have a Parent and Child POM which I stored in my personal GitHub, they both work as intended, but only if I build them sepparatelly in my machine, building the Child alone doesn’t build the parent POM and the jar, class and pom files of the parent do not get created. I have created a release in GitHub to which I attached the POM and JAR files of the parent but no luck, I also tried to use JitPack with the GitHub release, but same as before…the mvn install and mvn build instructions failed to find the files of the parent.

    Here’s my personal Git repo for reference: https://github.com/omarjmc

  4. Ramazan Cinardere

    August 18, 2019

    One question.

    How can the child project import/use classes from the parent class?

    • Lokesh Gupta

      August 18, 2019

      I think you should change the application design. You can use only what you include. Not the other way.

  5. Chris

    August 13, 2019

    Hi,

    This parent pom example has error. tag is not closed and ” is missing

  6. Jonathan

    July 18, 2019

    Hi,
    corrected Typo.

    If I want to exclude the dependency from parent pom to be inherit in child pom.

    can i do that ?
    if yes then how?

    Br,
    Jonathan

  7. fesh

    January 25, 2019

    In general yes, but you shouldn’t place property in it suppose to be fixed. The correct approach is to use rpropertiest in dependecies, plugin configuration etc.

  8. amol mangla

    December 25, 2018

    I have a parent pom in which i have defined a properties

     
    <project>
         <modelVersion>4.0.0</modelVersion>
         <groupId>com.mycompany.app</groupId>
         <artifactId>my-module</artifactId>
         <version>${artifact.version}</version>
    .....
    .....
    <properties>
                <artifact.version>1.12.0</artifact.version>
    </properties>
    </project>
    

    I have multiple modules in my workspace, so to keep version in one place, I have done above changes.

    Now in all my child modules defining the parent tag as below.

    <parent>
         <groupId>com.mycompany.app</groupId>
          <artifactId>my-module-child</artifactId>
          <version>${artifact.version}</version>
         <relativePath>../my-module/pom.xml</relativePath>
    </parent>
    

    Please suggest if this is correct approach to use the relative path or there are any issue with above code.

  9. Krishna Kumar

    December 2, 2018

    I am trying to reference classes of child module from the parent module and it is giving error that class not found. Also, when compiling though mvn compile the parent project, it doesn’t compile the module. Even after entering garbage chars, compile is getting passed for parent project. Please help.

  10. miguel

    August 21, 2018

    Is there a way to reference parent javascript with maven?

    • Lokesh Gupta

      August 21, 2018

      Maven can help in extracting jars and put JS files in predefined locations. If does not reference it.

  11. Atul Chavan

    February 1, 2018

    Thanks a lot for such an easy explanation, i am new to maven, but understood the concept completely.

  12. Omar

    January 2, 2018

    Hello,

    If you have such an example and you try to do a mvn release:prepare release:perform , it will cruch :

    The version could not be updated: ${spring.version} …

    Can you please advise ?

    Thank you in advance!

  13. Sreenu Yalam

    June 27, 2017

    I have written some common code in parent project. That i need to utilize all its child projects. But while preparing the build. The parent code is not including in the child projects. What could be the reason?

    • Lokesh Gupta

      June 27, 2017

      Please provide you parent and child pom files.

  14. Haroon

    June 11, 2017

    Can you please give any sample project to refer with detailed steps..!

    • Lokesh Gupta

      June 12, 2017

      I updated the post with a demo. Please refer it.

  15. Sharvesh

    June 6, 2017

    Do I need to mvn install Parent project then child project?

    • Lokesh Gupta

      June 6, 2017

      Yes. Follow these steps:

      1) Create parent maven project. Set packaging to pom.
      2) Create any maven project and add parent tag.
      3) You should be able to use parent pom’s properties.

  16. Sharvesh

    June 6, 2017

    How to execute Parent & Child POM project?
    Do I need to mvn install Parent project then child project?

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