如何在Gradle中指定依赖关系组的属性?

Rya*_*son 2 build dependency-management gradle

使用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
}
Run Code Online (Sandbox Code Playgroud)

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 }
Run Code Online (Sandbox Code Playgroud)

但是,当我宁愿将它们组合在一起时,这需要我在每个依赖项上指定属性.

有人建议使用可以解决此问题的语法吗?

Pet*_*ser 5

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

compile 'commons-collections:commons-collections:3.2.1@jar'
compile 'commons-lang:commons-lang:2.6@jar'
Run Code Online (Sandbox Code Playgroud)

要么:

def nonTransitive = { transitive = false }

compile 'commons-collections:commons-collections:3.2.1', nonTransitive
compile 'commons-lang:commons-lang:2.6', nonTransitive
Run Code Online (Sandbox Code Playgroud)

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

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
    }
}
Run Code Online (Sandbox Code Playgroud)