【问题标题】:How can I exclude dependencies brought in from other sub-projects?如何排除从其他子项目引入的依赖项?
【发布时间】:2016-02-28 20:24:48
【问题描述】:

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

我有一个子项目:

:commons:widget

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("groupID:ModuleID")吗?
  • transitive=false
  • Re: transitive=false 如果您要随意排除所有内容,那么拥有子项目并尝试重用东西是没有意义的。我需要一些具体的东西。
  • @RaGe 我注意到 Maven 会执行 groupId:projectId 之类的操作,但如果您的子组件是 :something:somethingelse:yetevensomethingelse,那么您的 groupId:projectId 是什么?

标签: gradle dependencies


【解决方案1】:

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
}

【讨论】:

  • 已经尝试排除模块,但会仔细检查以确定。它可能不是在正确的情况下。 transitive=false 不是很具体。这就像用核武器打蚊子一样。
  • @user447607 只是不要忘记将project 放入(),因为它已在答案中完成。不是compile project(":commons:some-other-project"),而是compile(project(":commons:some-other-project"))。我已经测试过了,但项目结构有点不同
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    Kotlin DSL 答案

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

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

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

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

    【讨论】:

      猜你喜欢
      • 2014-09-22
      • 2019-12-13
      • 2015-08-28
      • 1970-01-01
      • 2020-08-17
      • 2014-03-08
      • 2022-10-02
      • 1970-01-01
      • 2016-01-13
      相关资源
      最近更新 更多