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

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

问题描述

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

  System.getProperty(cassandra.ip); 

我有一个Gradle构建文件,我以
$ b开头$ b

  gradle test -Pcassandra.ip = 192.168.33.13 

  gradle test -Dcassandra.ip = 192.168.33.13 

但是, System.getProperty 总是会返回 null

我发现的唯一方法是在我的Gradle构建文件中通过

  test {
systemPropertycassandra.ip,192.168.33.13
}

我该怎么办它通过-D

解决方案

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



你可以像你一样在你的 test 块中使用systemProperty,但是通过传递给它的方法将它基于传入的gradle属性-P:

  test {
systemPropertycassandra.ip,project.getProperty(cassandra.ip)
}

或者,如果您是通过-D传递的

  test {
systemPropertycassandra.ip,System.getProperty(cassandra.ip)
}


I have a a Java program which reads a System property

System.getProperty("cassandra.ip");

and I have a Gradle build file that I start with

gradle test -Pcassandra.ip=192.168.33.13

or

gradle test -Dcassandra.ip=192.168.33.13

however System.getProperty will always return null.

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

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

How Do I do it via -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.

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")
}

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

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

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

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