简体   繁体   English

如何在Jenkins中注入属性文件并在Java代码中使用它

[英]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. 我想通过传递属性文件在Jenkins中传递用户名和密码,并在Java程序中使用此凭据登录我的应用程序。

  1. Inject property file in Jenkins 在Jenkins中注入属性文件
  2. Use credentials from that file in my Java program to login to my app. 在我的Java程序中使用该文件的凭据登录我的应用程序。

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. 或者,换句话说,我可以将它作为密码传递参数传递给我的jenkins工作,但我没有办法在我的java程序中获取此密码。

Any help would be appreciated. 任何帮助,将不胜感激。 Thanks in advance. 提前致谢。

Step 1) prepare a java property file with username and password and put togerther with java code 步骤1)准备一个带有用户名和密码的java属性文件,并放入java代码

username = xxx
password = xxx

Step 2) Config jenkins job to inject environment variable from property file 步骤2)配置jenkins作业从属性文件中注入环境变量

Option 1: check Inject environment variables to the build process under Build Environment section 选项1:检查将Inject environment variables to the build process Build Environment部分下Inject environment variables to the build process
在此输入图像描述

Option 2: Add a Inject environment variable build step and move it to the top on others build steps 选项2:添加Inject environment variable构建步骤,并将其移至其他构建步骤的顶部
在此输入图像描述

For both options , specify the credential property file path relative to jenkins job workspace 对于这两个选项 ,请指定相对于jenkins作业工作空间的凭据属性文件路径

Step 3) specify system property: username and password in java cmd line 步骤3)指定系统属性:java cmd行中的用户名和密码

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

Note : 注意
1. ${xxx} , the xxx from the key in property file, case sensitive. 1. ${xxx} ,来自属性文件中的键的xxx ,区分大小写。
2. Please use double quote in case username or password includes space 2.如果用户名或密码包含空格,请使用双引号
3. The pattern -Dxxx="${yyy}" can work on both Execute linux shell and Execute windows batch build step. 3.模式-Dxxx="${yyy}"可以在Execute linux shellExecute windows batch build步骤中工作。

在此输入图像描述 More about Java command 更多关于Java命令

Step 4) obtain ssytem property: username and password in Java code 步骤4)获取ssytem属性:Java代码中的用户名和密码

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

在此输入图像描述

More about system property 更多关于系统属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java Spring:如何使用“@Value”注解注入“Environment”属性? - Java Spring: How to use `@Value` annotation to inject an `Environment` property? 如何将属性文件中的值注入到JSR自定义注释中 - How to Inject values from a property file into JSR Custom Annotation 如何将属性值注入枚举? - How to inject property values into enum? 如何使用 Java 属性文件? - How to use Java Property File? 我们如何将参数值从Jenkins作业传递到pom.xml,然后再传递到JAVA代码中的系统属性? - How can we pass the parameter values from Jenkins job to pom.xml and then to my system property in JAVA code? Java:如何将键/值数据存储区(如memcached)与Java属性文件代码一起使用? - Java: How do you use key/value datastores like memcached with Java property file code? 如何将属性文件中的值注入字段? - How to inject value from property file into field? 如何在java中的play框架中使用属性文件? - How to use property file in play framework in java? Java注入最终属性 - Java inject of final property 如何不对Java中的程序进行硬编码? 使用的属性文件。 但是无法与多个xpath一起使用? - How to not Hard code the program in java? Used property file. But unable to use it with multiple xpaths?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM