Gradle中的最佳实践,用于全局配置Maven存储库

时间:2016-11-09 14:36:38

标签: maven gradle repository

在Maven中有settings.xml文件,我在其中配置存储库(如Sonatype Nexus服务器上的Maven存储库)。

在我的Gradle项目中,Maven资源库的URL直接在我的build.gradle文件中配置。

Gradle在全局和构建文件之外配置存储库的最佳做法是什么?

1 个答案:

答案 0 :(得分:3)

我在%GRADLE_USER_HOME%/gradle.properties

中添加了以下内容
nexus.user=somecoolguy
nexus.password=guessme

然后我使用这个片段

allprojects {
    repositories {
        def repoUrls = [
            'https://mynexus:8081/nexus/content/groups/foo',
            'https://mynexus:8081/nexus/content/groups/bar',
            'https://mynexus:8081/nexus/content/groups/baz'
        }
        repoUrls.each { String repoUrl ->
            maven {
                url repoUrl
                credentials {
                    username project.properties['nexus.user']
                    password project.properties['nexus.password']
                }
            }
        }
    }
}

您可以轻松将代码段转换为custom plugin,并可能将其改进为

  1. 使用加密密码
  2. project.hasProperty('nexus.user')返回false时失败