Maven Parent and Child POM Example

Lokesh Gupta

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.

Note that if any dependency or property is configured in both parent and child POMs with different values then the child POM value will take priority.

1. Contents of Parent POM

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 children. It has the same effect as writing them several times in individual pom files.
  • Properties – For example plugins, declarations, executions and IDs.
  • Configurations
  • Resources

2. A simple Example of Parent POM and Child POM

To match a parent POM, Maven uses two rules:

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

2.1. Parent POM

In the following example, 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 https://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>

2.2. Child POM

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

<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 https://maven.apache.org/xsd/maven-4.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>

3. Relative Path to Parent

By default, Maven looks for the parent POM first at the project’s root, then the local repository, and lastly in the remote repository. If the parent POM file is not located in any other place, then you can use the 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 https://maven.apache.org/xsd/maven-4.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>

4. Demo

Let’s learn to create maven projects with the parent-child relationships.

4.1. Create Parent Project

Project creation wizard.

Create Maven Project Wizard
Create Maven Project Wizard

Select Project Archetype.

Maven Quick Start Archtype
Maven Quick Start Archetype

Fill in the details and create a 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>

4.2. Create Child Project

Create a new maven project just like you did for the parent project. Change project-specific details like its name etc.

Create Maven Child Project
Create Maven Child Project

Now update the 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. Your child project has inherited the parent project. To test this, just remove all dependencies from child project’s pom.xml.

Now check its 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 the comment section.

Happy Learning !!

Ref: Maven Inheritance

Comments

Subscribe
Notify of
guest
18 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode