What does the parent tag in Maven pom represent?
Asked Answered
L

4

116

E.g.:

<parent>
    <groupId>mycompany.trade.com</groupId>
    <artifactId>mycompany.trade.</artifactId>
    <version>1.1.1.0-SNAPSHOT</version>
</parent>

Does it mean that Maven will search for parent pom? If yes, where, in which order? May be in folder up 1 level? Or in local repository or in repo?

Leash answered 6/11, 2011 at 10:28 Comment(1)
It bears mentioning somewhere that the parent tag is optional. Setting it to something arbitrary may be mostly harmless, but may also have unintended side-effects later. E.g. SonarCloud.io assumes you want to link to parent site.Torpid
I
105

Yes, maven reads the parent POM from your local repository (or proxies like nexus) and creates an 'effective POM' by merging the information from parent and module POM.

See also Introduction to the POM

One reason to use a parent is that you have a central place to store information about versions of artifacts, compiler-settings etc. that should be used in all modules.

Illyricum answered 6/11, 2011 at 10:50 Comment(0)
A
22

The common dependencies,Properties,constants etc can be definded in central parent project pom.xml

The main important thing is the parent project cannot be distributed and it looks quite similar to a regular "pom.xml" except that it also has a packaging tag

    <groupId>com.company.demo</groupId>
    <artifactId>MavenInheritance</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

The child now able to inherit this using

   <parent>
        <groupId>com.company.demo</groupId>
        <artifactId>MavenInheritance</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
Aldredge answered 6/10, 2017 at 7:43 Comment(0)
C
16

As the name suggests, we can point out a parent pom.xml file for the current pom.xml file. Doing so, dependencies, properties, constants and many more defined at the parent pom.xml file also get merged with the current pom.xml (child pom.xml) file. Say you have a parent tag in your projects pom.xml that looks like below:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.8.RELEASE</version>
</parent>

Then maven reads that parent POM from your local repository (or from repository managers like sonatype, jfrog, etc that you have configured) and creates a Resultant POM by combining the parent POM and your module’s POM.

To see the combined result use the following mvn command:

mvn help:effective-pom  
Callaghan answered 12/9, 2019 at 16:28 Comment(1)
What do tags in parent tag means?Barrick
I
0

This is the practice that is used in multi-modules projects where we need to inherit the dependencies from the parent projects.

Ischia answered 4/12, 2022 at 0:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.