[英]Reading Gradle properties in settings.gradle.kts
So I'm using Gradle Kotlin DSL, I want to know if it's possible to read gradle properties inside settings.gradle.kts
?所以我正在使用 Gradle Kotlin DSL,我想知道是否可以在
settings.gradle.kts
中读取 gradle 属性?
I have gradle.properties
file like this:我有这样的
gradle.properties
文件:
nexus_username=something
nexus_password=somepassword
I've done it like this, but still can't read the properties.我已经这样做了,但仍然无法读取属性。
dependencyResolutionManagement {
repositories {
mavenCentral()
google()
maven { setUrl("https://developer.huawei.com/repo/") }
maven { setUrl("https://jitpack.io") }
maven {
setUrl("https://some.repository/")
credentials {
val properties =
File(System.getProperty("user.home")+"\\.gradle", "gradle.properties").inputStream().use {
java.util.Properties().apply { load(it) }
}
username = properties["nexus_username"].toString()
password = properties["nexus_password"].toString()
}
}
}
}
You can access gradle parameters using providers (since 6.2)您可以使用提供程序访问 gradle 个参数(自 6.2 起)
val usernameProvider = providers.gradleProperty("nexus_username")
val passwordProvider = providers.gradleProperty("nexus_password")
dependencyResolutionManagement {
repositories {
maven {
setUrl("https://some.repository/")
credentials {
username = usernameProvider.getOrNull()
password = passwordProvider.getOrNull()
}
}
}
}
To work on Groovy, you need to replace the variable declaration with:要处理 Groovy,您需要将变量声明替换为:
def usernameProvider = providers.gradleProperty("nexus_username")
def passwordProvider = providers.gradleProperty("nexus_password")
Maybe this can help You using the values from your ~/gradle/gradle.properties.也许这可以帮助您使用 ~/gradle/gradle.properties 中的值。
repositories {
maven {
// Values from File ~/.gradle/gradle.properties
val nexusUser: String by project
val nexusPassword: String by project
credentials {
username = nexusUser
password = nexusPassword
}
}
You can access values set in gradle.properties
in both build.gradle.kts
and settings.gradle.kts
using delegate properties (Kotlin DSL only, because delegate properties is a Kotlin feature.). You can access values set in
gradle.properties
in both build.gradle.kts
and settings.gradle.kts
using delegate properties (Kotlin DSL only, because delegate properties is a Kotlin feature.).
gradle.properties gradle.properties
kotlin.code.style=official
# Your values here
testValue=coolStuff
build.gradle.kts build.gradle.kts
val testValue: String by project
settings.gradle.kts设置.gradle.kts
val testValue: String by settings
After googling for a while, looking for answer.谷歌搜索了一段时间后,寻找答案。 To keep those variable from being pushed into the repository It's best to use environment variable rather than
gradle.properties
.为了防止这些变量被推送到存储库中,最好使用环境变量而不是
gradle.properties
。
Simply because in settings.gradle.kts
file doesn't have function to call properties.仅仅是因为在
settings.gradle.kts
文件中没有 function 来调用属性。 And just to force reading public gradle.properties
file is such a hussle.而仅仅强制读取公共
gradle.properties
文件就是这么麻烦。
After setting your environment variables, you can call that variable in settings.gradle.kts
like this:设置环境变量后,您可以在
settings.gradle.kts
中调用该变量,如下所示:
dependencyResolutionManagement {
repositories {
google()
mavenCentral()
maven { setUrl("https://jitpack.io") }
maven {
setUrl(System.getenv("nexus_url"))
credentials {
username = System.getenv("nexus_username")
password = System.getenv("nexus_password")
}
}
}
}
The variable will not be pushed to the repository, using it this way also helps you in CI/CD like github action, where you can just set the variable as secret.该变量不会被推送到存储库,以这种方式使用它还可以帮助您在 CI/CD 中进行 github 操作,您可以在其中将变量设置为秘密。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.