Java Jenkins 注入环境变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31203816/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jenkins inject environment variable
提问by Velth
In a Jenkins job I'm doing a couple of actions that reside in the pre-step build, such as executing a shell script. With the use of the Jenkins plugin "EnvInject" I want to inject environment variables into my maven build (Unit tests) so that those can be used inside my Java unit tests. Inside the shell script im doing something similar as:
在 Jenkins 工作中,我正在执行一些驻留在预构建中的操作,例如执行 shell 脚本。通过使用 Jenkins 插件“EnvInject”,我想将环境变量注入到我的 Maven 构建(单元测试)中,以便可以在我的 Java 单元测试中使用这些变量。在 shell 脚本中,我正在做类似的事情:
echo "ip=$IP" >> unit-test.properties
While building Jenkins outputs the following:
在构建 Jenkins 时输出以下内容:
[EnvInject] - Injecting environment variables from a build step.
[EnvInject] - Injecting as environment variables the properties file path 'unit-test.properties'
[EnvInject] - Variables injected successfully.
But the "ip" variable is not available inside my Java code (unit test). When I do a full print of both System.getProperties() and System.getenv()
I do not see the "ip" enlisted.
但是“ip”变量在我的 Java 代码(单元测试)中不可用。当我对两者进行完整打印时,System.getProperties() and System.getenv()
我没有看到“ip”被登记。
Do I need to perform any special actions for maven to pass the variable to my Java code? Is something else wrong?
我是否需要为 maven 执行任何特殊操作才能将变量传递给我的 Java 代码?还有什么问题吗?
I'm pretty much stuck from this point onward, I do want to inject a key=value
from a pre-step into my Java code.
从这一点开始,我几乎被困住了,我确实想key=value
从预步骤中将 a 注入到我的 Java 代码中。
采纳答案by Velth
My solution:
我的解决方案:
Create a "Build a free-style software project".
创建一个“构建一个自由风格的软件项目”。
- Jenkins > New Item > Build a free-style software project
- Add 1st step: Execute shell @ Build, and echo
key=value
pairs to a .properties file - Add 2nd step: Inject environment variables, use the .properties file as defined in step 2
- Add 3rd step: Invoke top-level Maven targets
- Jenkins > 新项目 > 构建一个自由风格的软件项目
- 添加第 1 步:执行 shell @Build,并回显
key=value
对到一个 .properties 文件 - 添加第 2 步:注入环境变量,使用第 2 步中定义的 .properties 文件
- 添加第 3 步:调用顶级 Maven 目标
All custom environment variables are accessible with the key
as defined in step #2.
This was the only way I found to inject environment variables from shell to java.
所有自定义环境变量都可以通过key
第 2 步中定义的方式访问。这是我发现将环境变量从 shell 注入到 java 的唯一方法。
回答by Aditya
I had a similar requirement in my project, except my project was Maven. To use a variable value from jenkins to my java code, I used -DargLine="-DEnv=$Environment"inside "Build->Advanced->JVM Options". From my java code, I fetched the value of "Env" using System.getProperty(). FYI "Environment" is my Jenkins Variable, and "Env" is variable which is storing the value passed from jenkins into its variable(Environment) and fetched in java code using System.getProperty().
我在我的项目中有类似的需求,除了我的项目是 Maven。要将 jenkins 中的变量值用于我的 java 代码,我在“Build->Advanced->JVM Options”中使用了-DargLine="-DEnv=$Environment" 。从我的 java 代码中,我使用 System.getProperty() 获取了“Env”的值。仅供参考,“环境”是我的 Jenkins 变量,“环境”是变量,它将从 jenkins 传递到其变量(环境)中的值存储起来,并使用 System.getProperty() 在 Java 代码中获取。