Sushil Ks
Sushil Ks

Reputation: 403

How to exclude the parent pom dependency inside inherited child dependency for Maven?

I have a Jackson with version 2.3 in the parent pom and in my child pom I require version 2.9, is there any way to exclude the parent pom dependency?

Upvotes: 2

Views: 13909

Answers (2)

JonathanDavidArndt
JonathanDavidArndt

Reputation: 2742

As long as it is a simple replacement with the same groupId and artifactId, you can simply add the dependency to the child pom. The version number in the child pom will be the one that is effective.

Parent POM:

<dependency>
    <groupId>com.MyProjectName</groupId>
    <artifactId>core-config</artifactId>
    <version>1.00</version>
</dependency>

Child POM:
(a different version, and also exclude the XML parser that comes bundled with the parent)

<dependency>
    <groupId>com.MyProjectName</groupId>
    <artifactId>core-config</artifactId>
    <version>0.99</version>
    <exclusions>
        <exclusion>
            <groupId>xml-apis</groupId>
            <artifactId>xml-apis</artifactId>
        </exclusion>
        <exclusion>
            <groupId>xerces</groupId>
            <artifactId>xercesImpl</artifactId>
        </exclusion>
    </exclusions>
</dependency>

Upvotes: 0

gdegani
gdegani

Reputation: 856

You can override the dependency's version using the dependencyManagement section:

 <dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.fasterxml.jackson</groupId>
            <artifactId>jackson-bom</artifactId>
            <version>2.8.8</version>
        </dependency>
    </dependencies>
</dependencyManagement>
<dependencies>
    <dependency>
        <groupId>com.fasterxml.jackson</groupId>
        <artifactId>jackson-bom</artifactId>
    </dependency>
</dependencies> 

This is an example of parent:

<?xml version="1.0" encoding="UTF-8"?>
 <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>test</groupId>
<artifactId>test-parent</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
    <module>test-child</module>
</modules>

<dependencies>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.14</version>
    </dependency>
</dependencies>

And this is an example of child that overwrite the dependency version:

<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
    <artifactId>test-parent</artifactId>
    <groupId>test</groupId>
    <version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>test-child</artifactId>


<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.25</version>
        </dependency>
    </dependencies>
</dependencyManagement>
<dependencies>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
    </dependency>
</dependencies>

This are the dependencies resolved for the child

mvn dependency:tree
[INFO] Scanning for projects...
[INFO] 
[INFO] --------------------------< test:test-child >---------------------------
[INFO] Building test-child 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ test-child ---
[INFO] test:test-child:jar:1.0-SNAPSHOT
[INFO] \- org.slf4j:slf4j-api:jar:1.7.25:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.703 s
[INFO] Finished at: 2018-07-23T18:08:48+02:00
[INFO] ------------------------------------------------------------------------

Upvotes: 2

Related Questions