简体   繁体   English

在 settings.gradle.kts 中读取 Gradle 属性

[英]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")

Based on answer基于答案

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.

相关问题 使用Gradle Kotlin DSL在settings.gradle.kts中设置gradle.ext - Set gradle.ext in settings.gradle.kts with Gradle Kotlin DSL 在 `settings.gradle.kts` 中使用 `dependencyResolutionManagement` 时,如何在 `gradle.build.kts` 中配置自定义 maven 依赖项? - How does one configure custom maven dependency in `gradle.build.kts` while using `dependencyResolutionManagement` in `settings.gradle.kts`? Gradle KTS 构建文件 - Gradle KTS Build Files 将 build.gradle 迁移到 build.gradle.Kts:无法解析 Properties 类 - Migrating the build.gradle to build.gradle.Kts : Not able to resolve Properties class 关于gradle.properties,settings.gradle,gradle-wrapper.properties和local.properties的一些说明 - Some light on gradle.properties, settings.gradle, gradle-wrapper.properties and local.properties 摇篮设置 - Gradle Settings androidTest Groovy 模拟 build.gradle.kts - androidTest Groovy analog in build.gradle.kts 未解决的参考:build.gradle.kts 中的 grgit - Unresolved reference: grgit in build.gradle.kts gradle.kts中如何设置全局变量? - How to set global variables in gradle.kts? gradle kts 文件中的 applyNativeModulesAppBuildGradle(project) 和 applyNativeModulesSettingsGradle - applyNativeModulesAppBuildGradle(project) and applyNativeModulesSettingsGradle in gradle kts file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM