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

Maven子POM中依赖abc的scope未从provided更新为compile的解决方法

Maven子模块覆盖父POM依赖Scope的解决方案

问题场景

父POM中定义依赖:

<dependency>
        <groupId>xxx</groupId>
        <artifactId>abc</artifactId>
        <scope>provided</scope>
</dependency>

子POM中尝试修改Scope为compile:

<dependency>
    <groupId>xxx</groupId>
    <artifactId>abc</artifactId>
    <scope>compile</scope>
</dependency>

但依赖树中该依赖仍显示:

xxx:abc:jar:1.0.1:provided (scope not updated to compile)

解决方法

方法1:子POM中通过dependencyManagement覆盖配置

在子POM中先通过<dependencyManagement>重新定义该依赖的Scope为compile,再在<dependencies>中引入:

<!-- 先在dependencyManagement中覆盖配置 -->
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>xxx</groupId>
            <artifactId>abc</artifactId>
            <scope>compile</scope>
            <!-- 版本可继承父POM,也可显式指定 -->
            <version>1.0.1</version>
        </dependency>
    </dependencies>
</dependencyManagement>

<!-- 再引入依赖 -->
<dependencies>
    <dependency>
        <groupId>xxx</groupId>
        <artifactId>abc</artifactId>
    </dependency>
</dependencies>

方法2:调整父POM依赖配置(若允许修改父POM)

将父POM中该依赖从<dependencies>移至<dependencyManagement>:

<!-- 父POM中的配置 -->
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>xxx</groupId>
            <artifactId>abc</artifactId>
            <version>1.0.1</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

之后子POM直接引入并修改Scope即可生效:

<dependencies>
    <dependency>
        <groupId>xxx</groupId>
        <artifactId>abc</artifactId>
        <scope>compile</scope>
    </dependency>
</dependencies>

原理说明

Maven中,<dependencies>节点的依赖会直接被子模块继承,子模块无法直接覆盖其Scope;而<dependencyManagement>用于统一管理依赖的版本、Scope等配置,子模块引入依赖时可按需覆盖其中的配置项。父POM在<dependencies>中定义的provided依赖,子模块默认会继承该Scope,必须通过dependencyManagement覆盖才能修改。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 06:43:29