首页 > 解决方案 > 在运行已经完成的任务之前构建 Gradle getProperties

问题描述

我正在尝试使用 gradle 版本 4.8+ 的 Java、Serenity-BDD 项目,但应用程序没有提取 -Denvironment 和 -Dservicebranches 的 CLI 参数。我将这些属性作为我的 local.properties 文件中的空白值,并且在我的应用程序运行时它们没有被分配。

  • ./gradlew --build-cache build -Dwebdriver.remote.url=${SELENIUM_REMOTE_URL} -Denvironment=${ENVIRONMENT} -Dservicebranches=${SERVICE_BRANCHES} -Dtags=${TAGS}

我有一个 local.properties 文件,其中的属性被成功地依赖注入到项目中(通过 Serenity-Spring)。我希望这些 CLI 参数将覆盖这些值:

servicebranches=
environment=local

但是现在,CLI 参数中指定的任何内容都没有传递到项目中。通过 DI,或者通过显式获取 build.gradle 中的环境变量,我尝试过的方法没有奏效。

这是我在 build.gradle 中尝试过的一些事情:

//task integrationTests() {
//    doFirst
//        {
//            def environment = System.getProperty('environment')
//            def servicebranches = System.getProperty('servicebranches')
//        }
//    tasks.build.execute()
//}
//integrationTests.dependsOn(build)

//build.doFirst{
//    systemProperties System.properties
//    def environment = System.properties['environment']
//    environment = environment //This actually flags with 'Silly assignment'
//}

build.doFirst{
    def environment = System.getProperty('environment')
    def servicebranches = System.getProperty('servicebranches')
}

最新的似乎仍然缺少一步,因为程序仍在运行,但 args 仍然没有通过。我什至尝试过 -Denvironment=potato,但没有出现任何错误,因为我没有名为该名称的属性或属性文件。

我也尝试过使用 -P 标签而不是 -D 标签,但这似乎也不起作用。

我要做的就是使用 build.gradle 来使用 System.getProperty('environment') 和 System.getProperty('servicebranches'),然后再使用 Serenity 附带的已创建的“构建”任务。我该怎么做?我是否构建了一个全新的任务,在其中使用这些 getProperties,然后调用构建任务?我是否必须在 local.properties 文件中指定这些相同命名变量的分配?

标签: gradleserenity-bddsystem-properties

解决方案


-D 用于 Gradle 中的系统属性。尝试使用 -P 代替(https://docs.gradle.org/current/userguide/build_environment.html#sec:project_properties


推荐阅读