maven如何排除依赖呢?

欣喜 Maven教程 发布时间:2025-04-17 09:31:18 阅读数:13273 1
在日常开发中
我们经常需要将指定jar包从maven中排除,此时该如何操作呢?下文笔者将一一道来,如下所示
在pom.xml中使用 `<exclusions>`标签 
    即可实现jar包排除
例:

排除直接依赖

假设你有一个依赖`A`
   而`A`依赖于`B`
     但你不想使用`B`
	  可通过以下方式排除`B`:

<dependencies>
    <dependency>
        <groupId>com.example</groupId>
        <artifactId>A</artifactId>
        <version>1.0.0</version>
        <exclusions>
            <exclusion>
                <groupId>com.example</groupId>
                <artifactId>B</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

排除传递依赖

传递依赖
  指一个依赖项依赖于其他依赖项
   如果你需要排除某个传递依赖
   可以使用相同的方法:
 
<dependencies>
    <dependency>
        <groupId>com.example</groupId>
        <artifactId>C</artifactId>
        <version>2.0.0</version>
        <exclusions>
            <exclusion>
                <groupId>com.example</groupId>
                <artifactId>D</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

使用`<dependencyManagement>`排除依赖

在`<dependencyManagement>`部分定义依赖
  且需要排除某个传递依赖
   也可以使用`<exclusions>`标签:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>E</artifactId>
            <version>3.0.0</version>
            <exclusions>
                <exclusion>
                    <groupId>com.example</groupId>
                    <artifactId>F</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
</dependencyManagement>

使用`mvn dependency:tree`查看依赖树

在排除依赖之前
 建议使用`mvn dependency:tree`命令查看项目的依赖树
  以确定需要排除的具体依赖项:

mvn dependency:tree
假设你有一个项目依赖于`spring-boot-starter-web`
 但你不想使用`spring-boot-starter-tomcat`
 可使用这样的方法排除:
 
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <!-- 添加其他依赖 -->
</dependencies>
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: https://www.Java265.com/Maven/202504/8430.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者