JavaGian , Free Online Tutorials, JavaGian provides tutorials and interview questions of all technology like java tutorial, android, java frameworks, javascript, ajax, core java, sql, python, php, c language etc. for beginners and professionals.
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.
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:
There is a pom file in project’s root directory or in given relative path.
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.
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.
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.
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
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.