Multiple dependency configurations

How to convert the below Ivy dependencies into the Gradle ones?

<dependency org="joda" name="joda-time" rev="2.1" conf="build,webapp,test,spock->default" />
<dependency org="hibernate" name="hibernate" rev="3.2.6" conf="*->default,standalone;test,webapp->deserialization" />

I can never parse the Ivy file’s configurations but if you want to depend on a specific configuration of a library you can use the following (assuming you want to depend on build)

dependencies {
    compile group: 'org.hibernate', name: 'hibernate-core', version: '3.6.7.Final', configuration: 'build'
}

If you want to add a dependency to a specific configuration

//define foo configuration
configurations {
    foo
}

dependencies {
    foo group: 'org.hibernate', name: 'hibernate-core', version: '3.6.7.Final', configuration: 'build'
}

If you want some more help with this you can check out https://docs.gradle.org/current/userguide/artifact_dependencies_tutorial.html

Yes, that I know so far. But the question really was - how to add a dependency in its multiple configurations?

I think you would need to add multiple dependency declarations for each one. You could do something like

dependencies {
  ['build', 'webapp', 'test' , 'spock'].each { conf ->
    compile group: 'joda', name: 'joda-time', version: '2.1', configuration: conf
  }

If you want to make it easy to add / remove a configuration

2 Likes

Ethan, thanks for this! You save me a ton of time. Worked like a charm!