2017-03-27 109 views
0

我想了解如何将参数传递给jenkins的maven项目。 首先,我选择了一般配置在詹金斯,并单击 “这个项目是参数化”,那么如何将参数传递给jenkins的maven项目

名称=> my_parameter

选择=>桌面, 的ipad, 平板

然后,源管理=>混帐我写的git repo链接的仓库,它是确定的。

末步的构建=>执行shell =>命令=> MVN测试-DdeviceType = $ my_parameter

我的Maven项目有一个参数命名字符串设备。

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

然后与詹金斯参数建立

$ /bin/sh -xe /var/folders/j9/gyf9715j0hs32m4gd8h_gw4m55zss4 /T/hudson7304038831620598368.sh 
+ mvn test -DdeviceType=desktop 
[INFO] Scanning for projects... 
device type: null[0m 

为什么设备类型返回NULL?怎么了? 感谢您的帮助...

回答

0

那么,至少,“-D”命令行参数设置为“mvn”设置“系统属性”,而不是“环境变量”。不要拨打“System.getenv()”,请拨打“System.getProperty()”。

+0

我试过device = System.getProperty(“deviceType”); System.out.println(“device type:”+ device); 没有区别。设备类型返回null –

+0

这是因为提供给mvn的系统道具不会转换为可用于基于surefire的单元测试的系统属性。如果您仍想使用系统属性(最好使用env vars),请参阅http://maven.apache.org/surefire/maven-surefire-plugin/examples/system-properties.html了解如何设置surefire系统属性。 –

0

我终于找到答案通过尝试。但我不确定这是否是解决问题的最佳方法。我选择

BUİLD => invoke top-level maven targets. 
Goals => test 

但重要的问题是詹金斯参数名称必须是同一个名字的Maven项目的变量名。

device=System.getenv("deviceType"); 

然后使用参数在jenkins上生成它的工作!

相关问题