本文介绍了重构gradle中的Maven块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个android项目,并且有许多使用相同凭据的自定义存储库:

I'm working on an android project, and have a number of custom repositories using the same credentials:

中的方法MavenArtifactRepository maven(Action<? super MavenArtifactRepository> action) a>类(请参阅此处),如下所示:

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

编辑,来自以下注释:解决方案将关闭,如下所示.我认为这里需要使用curry方法(请参见强制关闭),但也许还有其他方法可以简化...

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

这篇关于重构gradle中的Maven块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 07:10