java中gradle,如何在Java代码中访问gradle参数

I have some experience with java and I am new to gradle and I joined a project in which I have to modify the gradle file.

Here is my build.gradle file

apply plugin: 'java'

apply plugin: 'idea'

sourceCompatibility = 1.5

version = '1.0'

dependencies {

testCompile 'org.testng:testng:6.9.10',

'org.seleniumhq.selenium:selenium-java:2.53.0'

}

test {

useTestNG()

testLogging.showStandardStreams = true

}

I then run my test suite using the following command from the mac terminal

./build test

I want to pass a parameter named environment

Based on this value of this parameter, I need to configure my urls and run tests for that environment. Something like ./build test environment=dev or ./build test environment=qa

And in my java code I would do something like this

if(env == 'dev') {

url = "my dev url";

user = "my dev user name"

} else if(env == 'qa') {

url = "my qa url";

user = "my qa user name"

}

How can I pass this parameter in the terminal ?

A small snippet of how I can use this parameter in my code would be of great help (my java code does not have a main method).

Note: I have already used a property file and achieved this behaviour, but my team does not want to make changes in code to set the environment. So I had to discard those changes.

解决方案

From command line to gradle you can use system properties or project properties. It will be either:

./gradlew test -Denv=dev

or

./gradle test -Penv=dev

The properties above can be now read in a build.gradle, you need to pass them to tests as well, it must be done with system properties so:

test {

systemProperty 'env', System.properties['env'] ?: 'dev'

}

for system properties from command line or:

test {

systemProperty 'env', project.hasProperty('env') ? project.env : 'dev'

}

In test classes use just:

System.getProperty("env")

to get the value you need.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值