Neel Desai
Neel Desai

Reputation: 71

How to Inject property file in Jenkins and use it values in Java Code

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.

Upvotes: 4

Views: 18459

Answers (1)

yong
yong

Reputation: 13712

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

Upvotes: 3

Related Questions