Chiefwarpaint
Chiefwarpaint

Reputation: 683

Is it possible to pass JVM arguments to a Jenkins 2.0 build?

I have a Java app that retrieves username/password creds from a resource property file.

For obvious reasons, I don't include the actual username/password values when I commit to SVN. I just make sure they're replaced with bogus values before committing.

Now I'm wanting to build my app on a remote Jenkins 2.0 box, however, since the values being stored in SVN are bogus, when Jenkins checks out my code and runs my integration tests, they fail because they can't authenticate when using the bogus values.

I was thinking maybe I could add some logic to my application that will first try to retrieve the username/password creds via a System.getProperty() and if those properties don't exist, then try looking at the resource file.

So, is it possible with Jenkins 2.0 to pass an argument to the JVM during a build so that a System.getProperty() would work?

Upvotes: 0

Views: 4641

Answers (1)

Daniel Hernández
Daniel Hernández

Reputation: 4276

You can do this using Jenkins Pipeline with declarative syntax:

Jenkinsfile:

pipeline {
    agent {
        label 'master'
    }

    environment  {
        //foo var
        foo = "Hello !";
    }

    stages {
        stage("test-var") {
            steps {
                // If you are using LINUX use EXPORT
                bat "set foo=\"${env.foo}\""
                bat "java -jar /someplace/DummyMain.jar"
            }
        }
    }
}

Please notice I am using: System.getenv("foo"); instead of getProperty Java Class:

/**
 *
 * @author daniel
 */
public class DummyMain {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        String property = System.getenv("foo");
        System.out.println(property);
    }

}

You will get: Hello ! when you run your Pipeline. If you are more familiar with Script Groovy syntax instead of Pipeline Declarative Syntax should not be a problem.

Just write the logic for read the Property File and you are set !

Upvotes: 1

Related Questions