【问题标题】:How to pass parameter to Maven project from Jenkins如何从 Jenkins 向 Maven 项目传递参数
【发布时间】:2017-08-20 05:32:22
【问题描述】:

我想学习如何从 Jenkins 向 Maven 项目传递参数。 首先我在 Jenkins 中选择了 General 配置,然后点击 This project is parameterized 然后:

姓名:my_parameter

选择:desktop, ipad, tablet

源码管理→Git→Repositories→Repository URL:我写了Git repo链接,没问题。

构建 → 执行 shell → 命令:mvn test -DdeviceType=$my_parameter

我的 Maven 项目有一个名为 String device 的参数。

    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?怎么了? 感谢您的帮助...

【问题讨论】:

标签: java macos maven jenkins parameter-passing


【解决方案1】:

在 Jenkins 中

在作业配置中,点击“This project is parameterised”并添加一个参数(字符串参数,如下例所示):

添加带有名称 (PARAM_NAME_TEST) 的参数和未设置参数时将使用的默认值

在“Goals and options”下的构建部分,使用 maven 命令传递参数值,例如(使用 mvn -D 定义属性) ):

clean test -DPARAM_NAME_TEST=${PARAM_NAME_TEST}

在 Maven 项目中

获取参数值使用:

 String paramValue = System.getProperty("PARAM_NAME_TEST");

【讨论】:

    【解决方案2】:

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

    构建调用顶级 Maven 目标

    目标:test

    但重要的问题是 Jenkins 中的参数名称必须与您的 Maven 项目变量名称相同。

    device=System.getenv("deviceType");
    

    然后在 Jenkins 上使用参数构建它就可以了!

    【讨论】:

      【解决方案3】:

      对于试图将变量从 Jenkins 管道传递到本地 pom.xml 文件的人,您可以做的是通过 mvn clean install 控制台命令传递参数。

      在你的 POM.xml 中

          <properties>
             <maven.test.skip>false</maven.test.skip>
          </properties>
      

      使用这个变量设置插件

      <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>
      

      现在我们将设置 Jenkins 方面的东西:

      我在这里所做的是创建一个布尔构建参数值 SKIP_JUNIT_TEST,它可以控制通过参数传递的值。

      创建构建参数后,在您的管道代码中,您可以像这样传递参数:

      sh 'mvn clean install -Dmaven.test.skip=${SKIP_JUNIT_TEST}'
      

      这里的maven.test.skip是我在pom.xml中设置的变量

      【讨论】:

      • surefire:test 目标自己识别属性maven.test.skip(“ ... 用户属性:maven.test.skip”)。您不必为此声明额外的&lt;build&gt;&lt;plugin&gt;...。只需&lt;properties&gt;&lt;maven.test.skip&gt;... 就足够了。此外,您不必同时声明&lt;skip&gt;&lt;skipTests&gt;。前者是后者的超集,即它甚至不编译测试。而且,OP 既没有提到也没有用 pipeline 标记问题,并且显然使用了 UI 配置。
      【解决方案4】:

      好吧,至少,“mvn”的“-D”命令行参数设置“系统属性”,而不是“环境变量”。不要调用“System.getenv()”,而是调用“System.getProperty()”。

      【讨论】:

      • 我试过 device=System.getProperty("deviceType"); System.out.println("设备类型:"+设备);没有区别。设备类型返回 null
      • 这是因为提供给 mvn 的系统属性不会转换为可用于基于 surefire 的单元测试的系统属性。如果您仍想使用系统属性(为此最好使用 env vars),请查看 maven.apache.org/surefire/maven-surefire-plugin/examples/… 了解如何设置安全系统属性。
      猜你喜欢
      • 2022-10-08
      • 2013-09-20
      • 1970-01-01
      • 2018-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-27
      • 1970-01-01
      相关资源
      最近更新 更多