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!

 

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

  6. Thank you for sharing!
    It works even without D key, just like this
    “clean test”
    And test run in standalone Selenium container:
    “docker run –rm -d -p 4444:4444 –name=firefox –network=mynetwork selenium/standalone-firefox”

  7. Hello Bay and thank you for this page! As you say, this straightforward how-to is nowhere else to be found on the internet.

  8. hi,

    I am unable to run single testcase
    maven
    HPALM
    Jenkins
    mvn clean test -Dtest=$testname -DExecuteFromAPALM=$ExecuteFromHPALM

    but not working
    when i di same from cmd prompt : mvn clean test -Dtest=$testname -DExecuteFromAPALM=$ExecuteFromHPALM
    it worked

  9. Super useful, thank you!! Sometimes the easiest bits of information are the hardest to find – luckily Google ranked your article as No. 1 for the search “how to parameterize a jenkins maven project” which made my life easier.
    I used your hint, and hoping that the string for the parameter is not case-sensitive 🙂
    Happy Testing!!

  10. Hi,
    I am trying to pass the value of parameter to maven build command in jenkins pipeline using groovy script but the command is not treating the value of the variable, rather its taking that variable as value. PFB example:

    jenkins parameter name – Environment

    stage (build)
    bat ‘mvn clean install -Denvironment=$Environment

  11. Hi Bay I have to pass three Stringparameter inside a method and execute an orchestration in my maven project . The code is written in Java rest assured framework . How to pass this three parameter from Jenkins . Please help

Leave a Reply

Your email address will not be published.