How to specify properties on groups of dependencies in Gradle?

扶醉桌前 提交于 2019-12-06 07:27:19

First, there are ways to simplify (or at least shorten) your declarations. For example:

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

Or:

def nonTransitive = { transitive = false }

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

In order to create, configure, and add multiple dependencies at once, you'll have to introduce a little abstraction. Something like:

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
    }
}

Create separate configurations, and have transitive = false on the desired configuration. In the dependencies, simply include configurations into compile or any other configuration they belong to

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
}

The above lets me disable the transitive dependencies for log related resources while I have the default transitive = true applied for apache configuration.

Edited Below as per tair's comment:

would this fix?

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

and run gradle dependencies

to view the dependencies. I am using Gradle-1.0 and it behaves ok as far as showing the dependencies is concerned, when using both transitive false and true.

I have an active project where when turning transitive to true using above method, I have 75 dependencies and I have 64 when transitive to false.

worth doing a similar check with and check the build artifacts.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!