如何通过 Gradle 和 -D 为我的测试提供系统属性 [英] How to give System property to my test via Gradle and -D

查看:27
本文介绍了如何通过 Gradle 和 -D 为我的测试提供系统属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Java 程序可以读取系统属性

I have a a Java program which reads a System property

System.getProperty("cassandra.ip");

我有一个以

gradle test -Pcassandra.ip=192.168.33.13

gradle test -Dcassandra.ip=192.168.33.13

但是System.getProperty 将始终返回null.

我发现的唯一方法是通过

The only way I found was to add that in my Gradle build file via

test {
    systemProperty "cassandra.ip", "192.168.33.13"
}

我如何通过 -D 来实现

How Do I do it via -D

推荐答案

-P 标志用于 gradle 属性,-D 标志用于 JVM 属性.由于测试可能会在新的 JVM 中分叉,因此传递给 gradle 的 -D 参数不会传播到测试中 - 听起来这就是您所看到的行为.

The -P flag is for gradle properties, and the -D flag is for JVM properties. Because the test may be forked in a new JVM, the -D argument passed to gradle will not be propagated to the test - it sounds like that is the behavior you are seeing.

您可以像之前一样在 test 块中使用 systemProperty,但是通过传入的 gradle 属性将其作为基础 -P:

You can use the systemProperty in your test block as you have done but base it on the incoming gradle property by passing it with it -P:

test {
    systemProperty "cassandra.ip", project.getProperty("cassandra.ip")
}

或者,如果您通过 -D 传入它

or alternatively, if you are passing it in via -D

test {
    systemProperty "cassandra.ip", System.getProperty("cassandra.ip")
}

这篇关于如何通过 Gradle 和 -D 为我的测试提供系统属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆