用于定义自定义Maven存储库的Gradle函数?
我最终不得不在我的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文件中,可以使用allProjects
或subProjects
.我不想将这些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
}
}
}
然后您应该可以拨打电话:
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
}
}
}