Home > Back-end >  Maven: why are dependencies declared in parent pom not inherited by child pom?
Maven: why are dependencies declared in parent pom not inherited by child pom?

Time:11-04

When I declare a dependency in a parent pom like -

<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.demo</groupId>
    <artifactId>deps</artifactId>
    <packaging>pom</packaging>
    <version>0.0.1-SNAPSHOT</version>
<dependencyManagement>
        <dependencies>    
               <!-- not relevant for this question -->
        </dependencies>
    </dependencyManagement>
<dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.3.12</version>
        </dependency>
</dependencies>
</project>

above i have declared spring-core as a dependency for parent pom.
Now in child pom, i am importing the parent pom -

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

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.demo</groupId>
                <artifactId>deps</artifactId>
                <version>0.0.1-SNAPSHOT</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
                           <!-- not relevant for this question -->

    </dependencies>
</project>

Now on looking at the dependencies inherited by child pom, there are none. Should NOT the spring-core jar be inherited by child project in all cases. As the parent pom directly depends on this jar and is it not passed on/inherited by child projects.

Note: This question is not about dependency management and versions
I understand dependencyManagement, which is to ensure that a set of projects have the same version and scope of a depencency.

CodePudding user response:

There is a difference between placing a parent tag and Bill of materials (importing the pom in the dependency management section)

Using <parent> is a "real inheritance" in maven. You define this tag on the child pom and by that you will get all the dependencies defined in the parent pom automatically (also properties and plugins).

The Bill of Materials on the other hand (This is how its called in the official documentation) doesn't import any dependencies by itself, however it allows to avoid specifying the versions of the dependencies in the pom of your application, because you define them in this BOM.

So to answer your question, you should really rewrite the child pom as:

<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.demo</groupId>
    <artifactId>deps2</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <parent>
       <groupId>com.demo</groupId>
       <artifactId>deps</artifactId>
       <version>0.0.1-SNAPSHOT</version>
    </parent>
    <dependencies>
                           <!-- not relevant for this question -->

    </dependencies>
</project>

CodePudding user response:

Your child pom is a standalone pom because you didn't specified a parent. You define a parent by adding this tag :

<parent>
    <groupId>yourpackage</groupId>
    <artifactId>yourartifactid</artifactId>
    <version>version</version>
</parent>

In your case, this block should do the trick :

<parent>
    <groupId>com.demo</groupId>
    <artifactId>deps</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</parent>

I know that your question was not about depedencies management; but for people that would not know the difference I'll write some words about that.

Note that by importing your pom in <dependenciesManagement> you don't have any impact given that it only defines intentions of use but not concrete import. The <dependencies> contains the concrete imports and it's only its content that you can use in your application.

  • Related