简体   繁体   English

如何将参数从 jenkins 传递给 Maven 项目

[英]How to pass parameter to maven project from jenkins

I want to learn how to pass parameter to maven project from jenkins.我想学习如何从 jenkins 将参数传递给 maven 项目。 Firstly i chose General Configuration in Jenkins and click "This project is parameterized" then首先我在 Jenkins 中选择 General Configuration 并单击“This project is parameterized”然后

Name => my_parameter名称 => my_parameter

Choices => desktop, ipad, tablet选择 => 台式机、ipad、平板电脑

Then, Source Management => git repositories i wrote git repo link,it was OK.然后,Source Management => git repositories 我写了 git repo 链接,没关系。

Last Step Build => Execute Shell => Command => mvn test -DdeviceType=$my_parameter最后一步构建 => 执行 Shell => 命令 => mvn test -DdeviceType=$my_parameter

My maven Project has a parameter named String device.我的 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);
    }

Then build with parameters on jenkins然后在 jenkins 上使用参数构建

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

Why does device type return null?为什么设备类型返回null? What's wrong?怎么了? Thanks for your helps...谢谢你的帮助...

Well, at the least, the " -D " commmand-line parameter to " mvn " sets "system properties", not "environment variables".好吧,至少,“ mvn ”的“ -D ”命令行参数设置了“系统属性”,而不是“环境变量”。 Instead of calling " System.getenv() ", call " System.getProperty() ".不要调用“ System.getenv() ”,而是调用“ System.getProperty() ”。

For someone trying to pass variable from Jenkins pipeline to local pom.xml file., what you can do is pass the argument through mvn clean install console command.对于试图将变量从 Jenkins 管道传递到本地 pom.xml 文件的人,您可以做的是通过 mvn clean install 控制台命令传递参数。

in your POM.xml在你的 POM.xml 中

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

use this variable setting up plugin使用这个变量设置插件

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

Now we will set up Jenkins side of things:现在我们将设置 Jenkins 方面的事情:

What I did here is created a boolean build parameter value SKIP_JUNIT_TEST that can control the value passed through arguments.我在这里所做的是创建了一个布尔构建参数值 SKIP_JUNIT_TEST,它可以控制通过参数传递的值。

After creating build parameter, in your pipeline code you can pass the argument like this:创建构建参数后,在您的管道代码中,您可以像这样传递参数:

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

here maven.test.skip is the variable I have set in pom.xml这里 maven.test.skip 是我在 pom.xml 中设置的变量

i finally found answer by trying.我终于通过尝试找到了答案。 But i dont sure whether thats the best way for solution.但我不确定这是否是解决方案的最佳方式。 i chose我选择了

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

But important issue is parameter name in Jenkins must be same name your maven projects variable name.但重要的问题是 Jenkins 中的参数名称必须与您的 Maven 项目变量名称相同。

 device=System.getenv("deviceType");

Then build with parameters on jenkins it works!然后在 jenkins 上使用参数构建它可以工作!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从CLI传递参数到Maven插件? - How to pass parameter to Maven plugin from CLI? 如何从詹金斯传递应用程序用户名和密码到Maven - How to pass application userName and password from jenkins to maven 如何从远程 Jenkins 触发本地 maven 项目? - How can I Trigger local maven project from remote Jenkins? 从maven传递一个java参数 - pass a java parameter from maven Jenkins通过浏览器URL作为Maven Selenium Java测试的参数 - Jenkins pass browser URL as parameter of a Maven Selenium Java test 如何设置Jenkins从其他具有应用程序依赖关系的maven项目构建maven项目 - How to setup Jenkins to build an maven project from other maven projects with dependencies within the application TestNG:将参数从Maven传递到DataProvider - TestNG: Pass parameter to DataProvider from Maven 无法将参数从命令行传递到Maven项目。 我想传递可以运行测试的服务器URL - Not able to pass parameter from command line to maven project. I want to pass server url on which I can run my test 通过Maven在Eclipse中进行测试,但是从Jenkins执行失败 - Tests in Eclipse with Maven pass, but execution from Jenkins fails 使用Jenkinsfile,Maven和Java从参数化的Jenkins项目传递变量 - Passing variables from parameterized Jenkins project using Jenkinsfile, Maven, and Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM