Java 如何从jenkins向maven项目传递参数
我想从jenkins那里学习如何将参数传递给maven项目。 首先,我在Jenkins中选择了常规配置,然后单击“此项目已参数化” Name=>my_参数 选项=>桌面, ipad, 平板电脑 然后,sourcemanagement=>git存储库我写了git repo链接,一切正常 最后一步构建=>ExecuteShell=>Command=>mvn测试-DdeviceType=$my_参数 我的maven项目有一个名为String device的参数Java 如何从jenkins向maven项目传递参数,java,macos,maven,jenkins,parameter-passing,Java,Macos,Maven,Jenkins,Parameter Passing,我想从jenkins那里学习如何将参数传递给maven项目。 首先,我在Jenkins中选择了常规配置,然后单击“此项目已参数化” Name=>my_参数 选项=>桌面, ipad, 平板电脑 然后,sourcemanagement=>git存储库我写了git repo链接,一切正常 最后一步构建=>ExecuteShell=>Command=>mvn测试-DdeviceType=$my_参数 我的maven项目有一个名为String device的参数 String device; publ
String device;
public static DesiredCapabilities caps=null;
@BeforeSuite
public void initializeDriver() throws MalformedURLException{
device=System.getenv("deviceType");
System.out.println("device type: "+ device);
if (device.contains("ipad")) {
caps = new DesiredCapabilities();
caps.setCapability("browserName", "iPad");
caps.setCapability("platform", "MAC");
caps.setCapability("device", "iPad Mini 4");
caps.setCapability("browserstack.local", "true");
caps.setCapability("browserstack.debug", "true");
caps.setCapability("safariAllowPopups", "true");
caps.setCapability("acceptSslCerts", "true");
caps.setCapability("browserstack.autoWait", "0");
caps.setCapability("requireWindowFocus", "true");
driver=new RemoteWebDriver(new URL(URL), caps);
}
然后在jenkins上使用参数进行构建
$ /bin/sh -xe /var/folders/j9/gyf9715j0hs32m4gd8h_gw4m55zss4 /T/hudson7304038831620598368.sh
+ mvn test -DdeviceType=desktop
[INFO] Scanning for projects...
device type: null[0m
为什么设备类型返回null?发生了什么?
感谢您的帮助……至少,“
-D
”命令行参数设置为“mvn
”设置为“系统属性”,而不是“环境变量”。不是调用“System.getenv()
”,而是调用“System.getProperty()
”我最终通过尝试找到了答案。但我不确定这是否是解决问题的最好办法。我选择了
BUİLD => invoke top-level maven targets.
Goals => test
但重要的问题是Jenkins中的参数名必须和maven项目变量名相同
device=System.getenv("deviceType");
然后在jenkins上使用参数进行构建,它可以工作 对于试图将变量从Jenkins管道传递到本地pom.xml文件的人,您可以通过mvn clean install console命令传递参数 在POM.xml中
<properties>
<maven.test.skip>false</maven.test.skip>
</properties>
这里,maven.test.skip是我在pom.xml中设置的变量,我尝试了device=System.getProperty(“deviceType”);System.out.println(“设备类型:+设备”);没有区别。设备类型返回Null这是因为提供给mvn的系统道具不会转换为基于surefire的单元测试可用的系统属性。如果您仍然希望使用系统属性(最好使用env vars),请查看如何设置surefire系统属性。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
<configuration>
<skipTests>${maven.test.skip}</skipTests>
<skip>${maven.test.skip}</skip>
</configuration>
</plugin>
sh 'mvn clean install -Dmaven.test.skip=${SKIP_JUNIT_TEST}'