You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何阻止依赖引入测试作用域的Log4J 1.2版本Maven依赖?

解决Log4J 1.2.12测试依赖自动下载的问题
  • 定位test范围依赖的来源
    执行mvn dependency:tree -Dscope=test命令,专门扫描测试阶段的依赖树,找到到底是哪个依赖间接引入了log4j:log4j:jar:1.2.12:test。普通的mvn dependency:tree可能不会完整展示test范围的依赖链,加上-Dscope=test就能精准定位。

  • 给对应父依赖添加排除规则
    找到引入该测试依赖的父依赖后,在这个依赖的配置里加入排除规则:

    <exclusions>
        <exclusion>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
        </exclusion>
    </exclusions>
    

    如果多个依赖都间接引入了,就逐个给这些依赖添加排除。

  • 全局排除(谨慎使用)
    要是不想逐个排查,可在pom.xml的<dependencyManagement>中强制屏蔽该依赖:

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>0</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    

    注意:全局排除可能影响其他依赖的正常运行,优先推荐精准定位后处理。

  • 检查Maven插件是否引入该依赖
    部分测试相关插件(如surefire)可能会间接拉取log4j测试依赖,检查pom.xml里的插件配置,若发现插件依赖了log4j,就在插件的<dependencies>节点中添加同样的排除规则。

内容的提问来源于stack exchange,提问作者Paul

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 22:42:15