mavenmaven-2maven-3maven-pluginmaven-assembly-plugin

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?


Solution

  • 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] ------------------------------------------------------------------------