【问题标题】:Java properties Files and GradleJava 属性文件和 Gradle
【发布时间】:2015-05-07 20:01:47
【问题描述】:

我有一个使用 gradle 运行测试套件设置的 Java TestNG 项目。目前,大多数测试都是从 constants.properties 文件中提取某些参数。运行 gradle 任务时如何在命令行上修改这些? gradle -DapplicationKey=0000 会替换我的 constants.properties 文件中的 applicationKey=1234 行吗?

编辑: 为了更清楚地了解情况和问题,我的 constants.properties 文件包含大约 400 到 500 个已定义的属性。我想避免完全重写 gradle 中的这些属性。当 Jenkins 在构建作业中运行相同的命令时,我只想从命令行覆盖这些属性。

【问题讨论】:

    标签: java properties gradle testng


    【解决方案1】:

    您希望将系统属性传递给用于运行测试的 JVM。这是一种配置Test类型的所有任务的方法:

    tasks.withType(Test) {
      systemProperty 'applicationKey', System.getProperty('applicationKey', '1234')
    }
    

    或者只是一项任务

    test {
        useTestNG()
        systemProperties = [
                applicationKey: System.getProperty('applicationKey', '1234')
        ]
    }
    

    您还可以使用systemProperties = System.getProperties()将Gradle环境中的所有系统属性复制到子虚拟机

    【讨论】:

    • 这很好,但我想在命令行上执行此操作。原因是我们通常命中的目标注册在 constants.properties 文件中,但我们希望 jenkins 命中的目标却没有。我想设置 jenkins 以覆盖命令行中的 constants.properties 文件以命中 jenkins 目标。
    • 你试过我的代码了吗?如果您的测试读取系统属性并在 Jenkins 中使用 gradle test -DapplicationKey=0000 启动它们,会发生什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-19
    • 1970-01-01
    • 2022-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多