exclusions在pom.xml中的作用

在 Maven 的 pom.xml 文件中,<exclusions> 元素用于排除传递性依赖中的特定依赖项。

传递性依赖是指当您声明一个依赖时,Maven 会自动引入该依赖所依赖的其他库。但有时您可能不希望引入某些传递性依赖,这时就可以使用 <exclusions> 。

<dependencies>
    <dependency>
        <groupId>org.example</groupId>
        <artifactId>parent-library</artifactId>
        <version>1.0.0</version>
        <exclusions>
            <exclusion>
                <groupId>com.another.example</groupId>
                <artifactId>unwanted-library</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

在上述示例中,声明了对 org.example:parent-library 的依赖,并排除了其传递性依赖中的 com.another.example:unwanted-library 。

咱们经常说:是什么,为什么,怎么用,接下里就看在哪里用?

<exclusions> 不仅常用于父项目向子项目传递依赖时,在一般的项目依赖管理中也经常使用。

在父项目中定义依赖并进行排除,可以确保子项目继承父项目依赖配置时,不会引入不需要的传递性依赖。但即使在普通的项目依赖配置中,如果某个依赖引入了不必要的传递性依赖,也可以使用 <exclusions> 来进行排除。

以下是一个普通项目中使用<exclusions>的例子:

假设你的项目中使用了spring-boot-starter-web依赖,而这个依赖会传递性地引入logback相关的依赖。但由于某种原因,你不想在项目中使用logback,而是想使用其他日志框架,比如log4j2。那么你可以在pom.xml中这样配置:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>...</version> 
        <exclusions>
            <exclusion>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-classic</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-slf4j-impl</artifactId>
        <version>...</version> 
    </dependency>
</dependencies>

看,写在GAV的后面

<exclusions>
            <exclusion>

 然后在写个GA就行了,V都不用写

在上述例子中,通过<exclusions>排除了spring-boot-starter-web中传递的ch.qos.logback:logback-classic依赖。

接下来是一个父传子项目的例子:

有一个父项目parent-project,其中定义了一些通用的依赖。在父项目的pom.xml中,使用<dependencyManagement>来管理依赖的版本:

<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>org.example</groupId>
    <artifactId>parent-project</artifactId>
    <version>1.0.0</version>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>5.3.20</version> 
            </dependency>
        </dependencies>
    </dependencyManagement>
    <!-- 其他配置 -->
</project>

然后有一个子项目child-project,继承自父项目:

<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>
    <parent>
        <groupId>org.example</groupId>
        <artifactId>parent-project</artifactId>
        <version>1.0.0</version>
    </parent>
    <artifactId>child-project</artifactId>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId> 
            <!-- 这里不需要指定版本,将继承父项目中定义的版本 -->
        </dependency>
    </dependencies>
</project>

在子项目中,不需要再指定spring-core的版本,它将自动继承父项目<dependencyManagement>中定义的5.3.20版本。

如果子项目中想要排除父项目中某个依赖的传递性依赖,同样可以使用<exclusions>。例如,父项目中的某个依赖传递性地引入了commons-logging,而子项目不想使用它,可以在子项目的pom.xml中这样配置

<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>
    <parent>
        <groupId>org.example</groupId>
        <artifactId>parent-project</artifactId>
        <version>1.0.0</version>
    </parent>
    <artifactId>child-project</artifactId>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId> 
            <exclusions>
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
</project>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值