First answer provided by @ToYonos will work fine, but if you are looking for a solution based on a configuration block, you can use the method MavenArtifactRepository maven(Action<? super MavenArtifactRepository> action)
from RepositoryHandler class (see here), as follows:
// 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")
}
EDIT from comments below: solution will Closure would look as follow. I think the use of curry
method (see Currying closure) is needed here, but maybe there are other ways to simplify...
// 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")
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…