如何排除Maven继承的子依赖中的父pom依赖?

dgtucam1  于 2023-04-11  发布在  Maven
关注(0)|答案(2)|浏览(500)

我有一个Jackson版本2.3在父POM和我的孩子POM我需要版本2.9,有没有办法排除父POM依赖?

tp5buhyn

tp5buhyn1#

您可以使用dependencyManagement节覆盖依赖项的版本:

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

这是一个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>

这是一个覆盖依赖版本的子版本的示例:

<?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>

这是为子级解析的依赖项

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

r7knjye22#

只要它是一个简单的替换相同的groupIdartifactId,你可以简单地添加依赖到子pom。子pom中的版本号将是有效的。
父POM:

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

子POM:

  • (不同的版本,也不包括与父版本捆绑的XML解析器)*
<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>

相关问题