如何排除从其他子项目引入的依赖项?

How can I exclude dependencies brought in from other sub-projects?

这不是重复的,因为其他解决方案都不起作用。

我有一个子项目:

:公地:小部件

gradle.build(子项目)类似于:

configurations {providedCompile}

dependencies {
  compile project(":commons:other-widget")
...other dependencies...
}

如果显示依赖关系:

+--- project :commons:some-other-project
    +--- project :commons:exclude-me-project (*)
    \--- org.apache.cxf:cxf-rt-frontend-jaxrs: -> 3.0.3 (*)

什么不起作用:

任何常用语法。我已经尝试了所有我能想到的变体。甚至去寻找 API 但找不到我需要的东西。

在此项目的依赖项部分: ...

compile project(":commons:some-other-project") {
 exclude (":commons:exclude-me-project")
}

结果:

Could not find method exclude() for arguments [:commons:some-other-project] on project 

我也试过:

compile ( project (':commons:some-other-project') ) {
  transitive = false
}

结果:它没有删除“:commons:some-other-project”的依赖项,而是删除了“:commons:some-other-project”。

我有一个大而复杂的项目要转换。我有很多这样的工作在我面前。给定一个项目作为依赖项,我如何从中排除一些东西?

exclude for dependencies 有一些不同的语法,所以尝试提供模块名称,它等于 exclude-me-project 名称,例如:

compile(project(":commons:some-other-project")) {
    exclude module: "exclude-me-project"
}

或者,您可以排除 commons 项目的所有传递依赖,但它会删除 some-other-project 项目的所有依赖,包括 exclude-me-project:

compile(project(":commons:some-other-project")) {
    transitive = false
}

对于新的 gradle 语法,您可以执行以下操作:

implementation (project(path: ':my_library_v1.0.0')) {
    exclude (group: 'com.google.code.gson', module: 'gson')
}

Kotlin DSL 答案

这将从另一个子项目中排除一个子项目:

implementation(project(":nice-subproject")) {
    exclude(module = "annoying-transitive-subproject")
}

另请注意,在测试夹具依赖项上使用 exclude 需要强制转换

implementation(testFixtures(project(":nice-subproject")) as ModuleDependency) {
    exclude(module = "annoying-transitive-subproject")
}