Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
645 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 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...