2017-03-27 105 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上生成它的工作!

相關問題