我正在处理一个android项目,并且有许多使用相同凭据的自定义存储库:
repositories {
maven {
url "<url1>"
credentials {
username = "<username>"
password = "<password>"
}
}
maven {
url "<url2>"
credentials {
username = "<username>"
password = "<password>"
}
}
}
是否可以定义方法(块?),这样我可以一次定义用户名和密码,而不必每次都重复?我希望能够做到:
repositories {
customMaven { url "<url1>"}
customMaven { url "<url2>"}
}
很抱歉,如果我在这里使用的术语不正确-gradle语法对我来说还是个谜。
答案 0 :(得分:3)
@ToYonos提供的第一个答案会很好用,但是如果您正在寻找基于配置块的解决方案,则可以使用RepositoryHandler中的MavenArtifactRepository maven(Action<? super MavenArtifactRepository> action)
方法类(see here),如下所示:
// a Closure that builds an Action for configuring a MavenArtifactRepository instance
def customMavenRepo = { url ->
return new Action<MavenArtifactRepository>() {
void execute(MavenArtifactRepository repo) {
repo.setUrl(url)
repo.credentials(new Action<PasswordCredentials>() {
void execute(PasswordCredentials credentials) {
credentials.setUsername("<username>")
credentials.setPassword("<password>")
}
});
}
};
}
// usage
repositories {
jcenter()
maven customMavenRepo("http://company.com/repo1")
maven customMavenRepo("http://company.com/repo2")
maven customMavenRepo("http://company.com/repo3")
}
编辑,来自以下注释:解决方案将关闭,如下所示。我认为这里需要使用curry
方法(请参阅Currying closure),但是也许还有其他方法可以简化...
// one closure with URL as parameter
ext.myCustomMavenClosure = { pUrl ->
url pUrl
credentials {
username = "<username>"
password = "<password>"
}
}
// helper function to return a "curried" closure
Closure myCustomMaven (url){
return myCustomMavenClosure.curry(url)
}
repositories {
// use closure directly
maven myCustomMavenClosure.curry ("http://mycompany.com/repo1")
// or use helper method
maven myCustomMaven("http://mycompany.com/repo2")
}
答案 1 :(得分:2)
Gradle就是关于这种定制的:
advert.title