2

使用 Gradle,我希望能够禁用一组依赖项的传递性,同时仍然允许其他依赖项。像这样的东西:

// transitivity enabled
compile(
  [group: 'log4j', name: 'log4j', version: '1.2.16'],
  [group: 'commons-beanutils', name: 'commons-beanutils', version: '1.7.0']
)

// transitivity disabled
compile(
  [group: 'commons-collections', name: 'commons-collections', version: '3.2.1'],
  [group: 'commons-lang', name: 'commons-lang', version: '2.6'],
) { 
  transitive = false
}

Gradle 不会接受这种语法。如果我这样做,我可以让它工作:

compile(group: 'commons-collections', name: 'commons-collections', version: '3.2.1') { transitive = false }
compile(group: 'commons-lang', name: 'commons-lang', version: '2.6']) { transitive = false }

但这需要我指定每个依赖项的属性,而我宁愿将它们组合在一起。

有人对适用于此的语法有建议吗?

4

2 回答 2

5

首先,有一些方法可以简化(或至少缩短)您的声明。例如:

compile 'commons-collections:commons-collections:3.2.1@jar'
compile 'commons-lang:commons-lang:2.6@jar'

或者:

def nonTransitive = { transitive = false }

compile 'commons-collections:commons-collections:3.2.1', nonTransitive
compile 'commons-lang:commons-lang:2.6', nonTransitive

为了一次创建、配置和添加多个依赖项,您必须引入一些抽象。就像是:

def deps(String... notations, Closure config) { 
    def deps = notations.collect { project.dependencies.create(it) }
    project.configure(deps, config)
}

dependencies {
    compile deps('commons-collections:commons-collections:3.2.1', 
            'commons-lang:commons-lang:2.6') { 
        transitive = false
    }
}
于 2012-05-11T17:43:44.963 回答
3

创建单独的配置,并在所需配置上设置传递 = false。在依赖项中,只需将配置包含到 compile 或它们所属的任何其他配置中

configurations {
    apache
    log {
        transitive = false
        visible = false //mark them private configuration if you need to
    }
}

dependencies {
    apache 'commons-collections:commons-collections:3.2.1'
    log 'log4j:log4j:1.2.16'

    compile configurations.apache
    compile configurations.log
}

以上让我禁用了日志相关资源的传递依赖项,而我将默认传递 = true 应用于 apache 配置。

根据 tair 的评论在下面编辑:

这会解决吗?

//just to show all configurations and setting transtivity to false
configurations.all.each { config->
    config.transitive = true
    println config.name + ' ' + config.transitive
}

并运行 gradle 依赖项

查看依赖项。我正在使用 Gradle-1.0,并且在使用传递性 false 和 true 时,就显示依赖关系而言,它的行为还可以。

我有一个活动项目,当使用上述方法将传递性转换为 true 时,我有 75 个依赖项,而当传递到 false 时,我有 64 个。

值得做一个类似的检查并检查构建工件。

于 2012-05-12T02:28:07.367 回答