2

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.

spierce7
  • 14,797
  • 13
  • 65
  • 106

2 Answers2

6

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

The same can be accomplished for buildscript repositories:

buildscript.repositories.ext.myRepo = {
    buildscript.repositories.maven {
        url "https://mymavenurl/releases"
        credentials() {
            username USERNAME
            password PASSWORD
        }
    }
}
spierce7
  • 14,797
  • 13
  • 65
  • 106
Krzysztof Atłasik
  • 21,985
  • 6
  • 54
  • 76
  • To me it only works for repositories but not for buildscript.repositories – Gavriel Jan 08 '18 at 22:35
  • 2
    this works: project.buildscript.repositories.ext.mavenMyPluginsRelease = { project.buildscript.repositories.maven {... – Gavriel Jan 08 '18 at 23:34
2

If you use these repository in all your projects, you can define them in an initialization script. Just create a file $HOME/.gradle/init.gradle which contains

allprojects {
    repositories {
        jcenter()

        maven {
            url "https://mymavenurl/releases"
            credentials(Credentials) {
                username USERNAME
                password PASSWORD
            }
        }
    }
} 
Wilco Greven
  • 2,085
  • 1
  • 10
  • 9