Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
616 views
in Technique[技术] by (71.8m points)

groovy - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...