4

I want to pass username and password in Jenkins by passing through property file and Use this credential in Java program to login to my app.

  1. Inject property file in Jenkins
  2. Use credentials from that file in my Java program to login to my app.

OR, Other way around it, I can pass it as Password passing parameter for my jenkins job but I am not getting a way to fetch this password in my java program.

Any help would be appreciated. Thanks in advance.

1
  • 1
    The easiest way seems to be to pass it as a system property via the command line (-D option when you start the JVM) Commented Feb 2, 2018 at 5:07

1 Answer 1

3

Step 1) prepare a java property file with username and password and put togerther with java code

username = xxx
password = xxx

Step 2) Config jenkins job to inject environment variable from property file

Option 1: check Inject environment variables to the build process under Build Environment section
enter image description here

Option 2: Add a Inject environment variable build step and move it to the top on others build steps
enter image description here

For both options, specify the credential property file path relative to jenkins job workspace

Step 3) specify system property: username and password in java cmd line

for example: java -DUserName="${username}" -DPassWord="${password}"

Note:
1. ${xxx}, the xxx from the key in property file, case sensitive.
2. Please use double quote in case username or password includes space
3. The pattern -Dxxx="${yyy}" can work on both Execute linux shell and Execute windows batch build step.

enter image description here More about Java command

Step 4) obtain ssytem property: username and password in Java code

String userName = System.getProperty("UserName")
// the parameter: "UserName" from -Dxxxx, also case sensitive

enter image description here

More about system property

Sign up to request clarification or add additional context in comments.

7 Comments

can you explain step#3 and 4 in more detail?
but in this way, I will have to pass default password in java code which could be access or view by others. which is not good in terms of security reason.
System.getProperty() has another version only accept one argument: system proterty name, no default value required.
But this property file will be inside my Java project. Am I right? So In future if my credentials will get changed, I will have to access my java project & will need to change property file over there. Am I right?
If the username and password appear in job's console output is acceptable? If yes, i can update answer to generate auth file during job running(not exist source code). If no, you need to add credential file by Jenkins admin, then choose credential in job configuration.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.