Quick tip: Passing parameters from Jenkins to Maven

I saw bits and pieces of information all over the internet about parameters and properties and command-line arguments, but what I was looking for I didn’t find: a simple, straightforward explanation of how to use a Paramaterized Build in Jenkins to pass arguments through to the jUnit tests that run the functional tests that I’ve built on Webdriver. So: Β here it is!

Step 1: Command-line via Maven to jUnit

Use the System.getProperty tor ead system properties in Java:

        remoteHost = System.getProperty("remoteHost");
    	if (remoteHost == null) remoteHost = "http://localhost:4444/wd/hub";
    	
    	browserName = System.getProperty("browserName");
    	if (browserName == null) browserName = "Internet Explorer";

And use -D to pass them with Maven:

mvn test -DbrowserName=Firefox

Voila!

 

Step 2: Jenkins to Maven

First make the build a paramaterized build:

jenkins_params1

 

Then adjust the maven build step as shown:

 

jenkins_params2

 

(Quotes are important here because of Internet Explorer)

 

Voila!

 

16 thoughts on “Quick tip: Passing parameters from Jenkins to Maven

  1. Hi!
    May be you can help me with suggest issue.
    Few times i search solution about System.getProperty there used in https://wiki.jenkins-ci.org/display/JENKINS/Build+Flow+Plugin or https://wiki.jenkins-ci.org/display/JENKINS/Groovy+plugin.
    My issue is take downstream Jenkins job some environment variables.
    I use pre build `choise parameter`, there named GOAL.
    After that define build flow using flow DSL like this:

    t = System.getProperty(‘GOAL’)
    println t
    / /build(“my downstream_job”, target: t)

    output of println function is `null`, but `choise parameter` was defined before start build.
    Thanks! And excuse for bad English.

    1. You missed a step: when you invoke Maven, you need to pass the parameter in the command-line. Should be something like “-DGoal=$GOAL”. See step two of the blog post for a screenshot.

      1. Yes, it’s not mistake — i don’t use ant or maven.
        My goal only give environment variable to downstream job.
        After that this value will use in shell script. That’s all. =)
        I think you had experience in Groovy DSL using. =)

  2. It’s so simple.
    For use pre build params in downstream job’s we should use this construction.

    println params; \\ show all pre build parameters in map form: [ENVVAR:value]
    t = params.ENVVAR;
    build(“Our_downstream_Job”, target: t)

    =)

  3. Hi.
    How can I do the same, but not for code – but for pom.xml
    For example: I have in pom.xml

    ???????.xml

    And I have 10 different “testng.xml” in Project.
    So I’d like to pass the name of “testng.xml” to pom.xml to execute particular “testng.xml”.

    Thanks!

    1. I believe that you want to look at environment variables for this? Like, reading the environment variables inside your pom.xml? I’m not 100% sure I understand your question correctly, but that might help you find what you need.

  4. Thank you! I was trying to pass in which environment Maven should deploy to and your post connected the dots between JVM variables to Jenkins variables to Maven Variables.

  5. Hi,

    I am trying to define and use an environment variable in Jenkins -> Manage Jenkins -> Configure System -> Global properties .

    I have a maven project that needs to access the variable defined here.

    Can you please share some information about how can I do it?

Leave a Reply

Your email address will not be published. Required fields are marked *