groovy 如何在settings.gradle的pluginManagement中使用全局环境属性?

deikduxw  于 2022-11-01  发布在  其他
关注(0)|答案(1)|浏览(166)

全球Gradle.properties:

USERNAME=<username>
PASSWORD=<password>

Gradle设置文件:

pluginManagement {
    plugins {
        id 'plugin.plugin.plugin' version '0.0.1'
    }
    repositories {
        maven {
            credentials{
                username = "${USERNAME}" != "" ? "${USERNAME}" : System.getenv('USERNAME')
                password = "${PASSWORD}" != "" ? "${PASSWORD}" : System.getenv('PASSWORD')
            }
            url = '<custom plugin repository>'
        }
        gradlePluginPortal()
        mavenCentral()
    }
}

生成此项目时出现的错误:


* Exception is:

java.lang.NoClassDefFoundError: org/gradle/plugin/devel/internal/precompiled/FirstPassPrecompiledScript

如何在settings.gradle文件中成功使用pluginManagement中的全局属性?

2admgd59

2admgd591#

为什么不使用这样的方法来获取值:

artifactoryUser = getConfigurationProperty("ARTIFACTORY_USER", "artifactoryUser", null)
artifactoryPwd = getConfigurationProperty("ARTIFACTORY_PWD", "artifactoryPwd", null)

getConfigurationProperty将查找名为ARTIFACTORY_USER的系统环境变量,然后检查gradle.properties中是否有artifactoryUser变量,否则将返回默认值。

String getConfigurationProperty(String envVar, String sysProp, String defaultValue) {
    def result = System.getenv(envVar) ?: project.findProperty(sysProp)
    result ?: defaultValue
}

然后在存储库部分

maven {
        url = mavenRepoURL
        credentials {
            username artifactoryUser
            password artifactoryPwd
        }
    }

相关问题