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

自定义Gradle配置无法声明带排除项的依赖

问题解决方法

错误根源是Groovy DSL语法使用错误:exclude(group = "xxx", module = "xxx")里的=会被Groovy解析为试图修改依赖对象的只读group属性,从而触发报错。

正确写法(两种可选)

方案一:使用Groovy命名参数(推荐)

configurations {
    myConf
}

repositories {
    mavenCentral()
}

dependencies {
    myConf("commons-beanutils:commons-beanutils:1.9.4") {
        exclude group: "commons-collections", module: "commons-collections"
    }
}

方案二:显式传入Map对象

configurations {
    myConf
}

repositories {
    mavenCentral()
}

dependencies {
    myConf("commons-beanutils:commons-beanutils:1.9.4") {
        exclude([group: "commons-collections", module: "commons-collections"])
    }
}

修改后验证结果

执行gradle dependencies,输出会显示commons-collections已被排除:

> Task :dependencies

------------------------------------------------------------
Root project 'tmp-project'
------------------------------------------------------------

myConf
\--- commons-beanutils:commons-beanutils:1.9.4
     \--- commons-logging:commons-logging:1.2

A web-based, searchable dependency report is available by adding the --scan option.

BUILD SUCCESSFUL in 1s
1 actionable task: 1 executed

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 01:45:26