Gradle 函数来定义自定义 Maven 存储库? [英] Gradle function to define custom maven repository?

查看:28
本文介绍了Gradle 函数来定义自定义 Maven 存储库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最终不得不在我的 build.gradle 文件中到处定义我的 Maven 存储库.定义通常非常繁琐:

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

注意上面的 gradle 如何提供一种很好的方式来定义常用的 maven 存储库(即 jcenter()).我想要一种在插件或父 gradle 脚本中的方法来在函数中或静态地定义存储库,然后只需在 repositories 块中调用它: myMavenRepo().

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

我缺乏对 groovy 的了解,所以我不太了解我需要解析我看到的 groovy 源以找到一种很好的方法来做到这一点.我该怎么做?

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?

我知道在父 gradle 文件中,我可以使用 allProjectssubProjects.我不想将这些 Maven 存储库添加到所有模块中,而只想添加到特定模块中.

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.

推荐答案

试试这个:

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

然后你应该可以调用:

repositories {
    mavenCentral()
    myRepo()
}

对于 buildscript 存储库也可以完成相同的操作:

The same can be accomplished for buildscript repositories:

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

这篇关于Gradle 函数来定义自定义 Maven 存储库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆