DariusL
DariusL

Reputation: 4077

Can I put my dependencies and repositories in another file?

I have a requirement that some of my dependencies are in a separate file. How can I accomplish this? One of the samples on docs.gradle.org is:

List groovy = ["org.codehaus.groovy:groovy-all:2.4.7@jar",
           "commons-cli:commons-cli:1.0@jar",
           "org.apache.ant:ant:1.9.6@jar"]
List hibernate = ['org.hibernate:hibernate:3.0.5@jar',
              'somegroup:someorg:1.0@jar']

dependencies {
    runtime groovy, hibernate
}

Something like this would work, but I'd also like to specify my repositories in the same file.

Edit:

The solution I came up with

stuff.gradle

repositories {
    maven {
        credentials {
            username 'stuff'
            password 'stuff'
        }
        url 'stuff'
    }
}

dependencies {
    compile 'things'
}

And in build.gradle

apply from: 'path/to/stuff.gradle'

After dependencies, though I'm not sure if it makes a difference. It was surprisingly simple. There were no changes to build.gradle apart from the apply statement, it still has the normal repositories and dependencies closures. Thanks to Opal for putting me on the right track.

Upvotes: 3

Views: 1194

Answers (1)

Opal
Opal

Reputation: 84756

Here you go:

lol.gradle:

ext.repos = { mavenCentral(); mavenLocal() }

ext.groovy = [
          "org.codehaus.groovy:groovy-all:2.4.7@jar",
           "commons-cli:commons-cli:1.0@jar",
           "org.apache.ant:ant:1.9.6@jar",
           ]
ext.hibernate = [
          'org.hibernate:hibernate:3.0.5@jar',
          ]

build.gradle

apply from: 'lol.gradle'
apply plugin: 'java'

repositories repos

dependencies {
  runtime groovy, hibernate
}

Since repositories can be kept as a list, the simplest way of keeping repositories with no workarounds is to use a closure. If you use a list to keep repositories as well it will fail with with method resolution error for both mavenLocal and mavenCentral.

If you prefer to keep repositories as a list however then the following piece of code can be used:

lol.gradle

ext.repos = ['mavenCentral', 'mavenLocal',]

ext.groovy = [
          "org.codehaus.groovy:groovy-all:2.4.7@jar",
           "commons-cli:commons-cli:1.0@jar",
           "org.apache.ant:ant:1.9.6@jar",
           ]
ext.hibernate = [
          'org.hibernate:hibernate:3.0.5@jar',
          ]

build.gradle

apply from: 'lol.gradle'
apply plugin: 'java'

repositories { r ->
  repos.each { n -> r."$n"() }
}

dependencies {
  runtime groovy, hibernate
}

Upvotes: 6

Related Questions