我们如何将参数值从 Jenkins 作业传递到 pom.xml,然后传递到我的 JAVA 代码中的系统属性?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31101266/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can we pass the parameter values from Jenkins job to pom.xml and then to my system property in JAVA code?
提问by Atishay
My question may look a little easy to answer but I couldn't really work it out.
我的问题可能看起来有点容易回答,但我真的无法解决。
I am working on a project where I have configured the Jenkins job for my build.
我正在为我的构建配置 Jenkins 作业的项目中工作。
What i have done so far is that I have made a maven job which has the pom.xml path configured and with two goals 'clean test' . When I run it it works just fine.
到目前为止我所做的是我做了一个 maven 工作,它配置了 pom.xml 路径并有两个目标 'clean test' 。当我运行它时,它工作得很好。
Now I wanna pass the Paramter value BROWSER
from Jenkins which may have value like Firefox, Chrome and i want it to pass to java code so that specific browser may open.
现在我想BROWSER
从 Jenkins传递参数值,它可能具有像 Firefox、Chrome 这样的值,我希望它传递给 java 代码,以便可以打开特定的浏览器。
I have used the system property in my code:
我在我的代码中使用了系统属性:
protected static final String BROWSER = System.getProperty("BROWSER","chrome");
protected static final String BROWSER = System.getProperty("BROWSER","chrome");
Now I want to pass the value from jenkins to pom and then java code.
现在我想将值从 jenkins 传递给 pom,然后传递给 java 代码。
Here is my pom which is faulty:
这是我的pom有问题:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<systemPropertyVariables>
<BROWSER>${env.BROWSER}</BROWSER>
</systemPropertyVariables>
</configuration>
</plugin>
But here it gives me error like cannot resolve symbol env.BROWSER
但在这里它给了我这样的错误 cannot resolve symbol env.BROWSER
WorkAround: As a workaround I have made the command in Jenkins goals look like this:
变通方法:作为变通方法,我使 Jenkins 目标中的命令如下所示:
clean test -DREMOTE_DRIVER=${REMOTE_DRIVER} -DSELENIUM_HOST=${SELENIUM_HOST} -DBROWSER=${BROWSER}
But I think its not the ideal way of doing it.
但我认为这不是理想的做法。
I have already made the parameters in my Jenkins build Parameter Name - "BROWSER" with choices of 'firefox' 'chrome' and 'ie'.
我已经在我的 Jenkins 构建参数名称 - “浏览器”中设置了参数,并选择了“firefox”、“chrome”和“ie”。
Now i wana just call it in pom ( i used ${env.BROWSER} ) and then pass to my system property in java code. (It is not working for me)
现在我只是在 pom 中调用它(我使用了 ${env.BROWSER} ),然后在 java 代码中传递给我的系统属性。(这对我不起作用)
采纳答案by Eranda
Environmental properties in jenkins are not the system properties of JVM. To set the properties either you need to set it using commandline.
export BROWSER =chrome
or
you can set it in This build is parameterized
section of your jenkins build configuration.
jenkins 中的环境属性不是 JVM 的系统属性。要设置属性,您需要使用命令行进行设置。
export BROWSER =chrome
或者您可以在This build is parameterized
jenkins 构建配置的部分设置它。