spierce7 spierce7 - 1 month ago 11
Groovy Question

Gradle function to define custom maven repository?

I end up having to define my maven repository all over the place in my

build.gradle
files. The definition can often be very cumbersome:

repositories {
jcenter()

maven {
url "https://mymavenurl/releases"
credentials(Credentials) {
username USERNAME
password PASSWORD
}
}
}


Notice above how gradle offers a nice way of defining commonly used maven repositories (i.e.
jcenter()
). I'd like a way in a plugin or in a parent gradle script to define the repository in a function, or statically, and then just call it inside the
repositories
block:
myMavenRepo()
.

My knowledge of groovy is lacking, so I don't quite have the understanding I need to parse the groovy sources that I'm seeing to find a nice way to do this. How would I do this?

I'm aware that in a parent gradle file, I could use
allProjects
or
subProjects
. I don't want to add these maven repositories to all modules, but instead only specific ones.

Answer

Try something like this:

repositories.ext.myRepo = {
    repositories.maven {
        url "https://mymavenurl/releases"
        credentials() {
            username USERNAME
            password PASSWORD
        }
    }
}

Then you should be able to call:

repositories {
    mavenCentral()
    myRepo()
}
Comments