Build 如何在Gradle中指定依赖项组的属性?
使用Gradle,我希望能够禁用一组依赖项上的传递性,同时仍然允许其他依赖项上的传递性。大概是这样的:Build 如何在Gradle中指定依赖项组的属性?,build,gradle,dependency-management,Build,Gradle,Dependency Management,使用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
// 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 }
但这需要我指定每个依赖项的属性,而我更愿意将它们组合在一起
有没有人对这种语法有什么建议?首先,有一些方法可以简化(或至少缩短)您的声明。例如:
compile 'commons-collections:commons-collections:3.2.1@jar'
compile 'commons-lang:commons-lang:2.6@jar'
或:
为了一次创建、配置和添加多个依赖项,您必须引入一些抽象。比如:
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
}
}
创建单独的配置,并在所需配置上设置transitive=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
}
上面允许我禁用日志相关资源的可传递依赖项,同时将默认的transitive=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-1.0,当同时使用可传递false和true时,就显示依赖项而言,它的行为正常
我有一个活动项目,当使用上述方法将可传递性转换为true时,我有75个依赖项,当可传递为false时,我有64个依赖项
值得进行类似的检查并检查构建工件
//just to show all configurations and setting transtivity to false
configurations.all.each { config->
config.transitive = true
println config.name + ' ' + config.transitive
}