2

Using Gradle, I'd like to be able to disable transitivity on one group of dependencies, while still allowing on others. Something like this:

// 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 won't accept this syntax. I can get it to work if I do this:

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 }

But that requires me to specify the property on each dependency, when I'd rather group them together.

Anyone have a suggestion for a syntax that will work on this?

Ryan Nelson
  • 4,466
  • 5
  • 29
  • 45

2 Answers2

5

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
    }
}
Peter Niederwieser
  • 121,412
  • 21
  • 324
  • 259
3

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.

skipy
  • 4,032
  • 2
  • 23
  • 19