Java 如何配置 Hudson 作业以使用特定的 JDK?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/747878/
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
How can I configure a Hudson job to use a specific JDK?
提问by rewbs
I have a number of projects running on a Hudson slave. I'd like one of them to run Ant under Java6, rather than the default (which is Java5 in my environment).
我有许多在 Hudson slave 上运行的项目。我希望其中之一在 Java6 下运行 Ant,而不是默认(在我的环境中是 Java5)。
In the project configuration view, I was hoping to find either:
在项目配置视图中,我希望找到:
- An explicit option allowing me to set a custom JDK location to use for this project.
- A way to set custom environment variables for this project, which would allow me to set JAVA_HOME to the JDK6 location. The would make Ant pick up and run on Java6 as desired.
- 一个显式选项,允许我设置用于此项目的自定义 JDK 位置。
- 一种为此项目设置自定义环境变量的方法,这将允许我将 JAVA_HOME 设置为 JDK6 位置。这将使 Ant 根据需要选择并在 Java6 上运行。
Is there a way to do either of the above? If one of those facilities is available, I can't see how to access it. I'm running on Hudson 1.285.
有没有办法做到以上任何一项?如果这些设施之一可用,我看不出如何访问它。我在 Hudson 1.285 上运行。
I would rather avoid using an "execute shell" operation instead of the "invoke Ant" operation if possible: my slave is on z/OS and Hudson doesn't seem to create the temporary shell scripts properly on this platform (probably an encoding issue).
如果可能的话,我宁愿避免使用“执行 shell”操作而不是“调用 Ant”操作:我的从属设备在 z/OS 上,而 Hudson 似乎没有在这个平台上正确创建临时 shell 脚本(可能是编码问题) )。
采纳答案by rewbs
It turns out that if you make the build parametrised, any string parameters you add become environment variables. With this approach, it is possible to set any environment variable for the build, including JAVA_HOME, which is picked up by Ant.
事实证明,如果您使构建参数化,您添加的任何字符串参数都会成为环境变量。使用这种方法,可以为构建设置任何环境变量,包括 Ant 选取的 JAVA_HOME。
So the best solution for me was:
所以对我来说最好的解决方案是:
- In the job configuration page Tick "This build is parameterized"
- Add an new String parameter called JAVA_HOME and with the default value set to the JDK location
- 在作业配置页面中勾选“此构建已参数化”
- 添加一个名为 JAVA_HOME 的新字符串参数,并将默认值设置为 JDK 位置
It's not obvious that build string parameters become environment variables, but once you know that they do, it's easy to set the JDK this way.
构建字符串参数变成环境变量并不明显,但是一旦您知道它们会变成环境变量,就可以很容易地以这种方式设置 JDK。
The developers on the Hudson mailing list recommended another approachusing the master JDK configurations and overrides in the node configurations... but just setting the JAVA_HOME env var seems way easier to me.
Hudson 邮件列表上的开发人员推荐了另一种使用主 JDK 配置和节点配置中的覆盖的方法……但对我来说,仅设置 JAVA_HOME env var 似乎更容易。
回答by matt b
A way to set custom environment variables for this project, which would allow me to set JAVA_HOME to the JDK6 location. The would make Ant pick up and run on Java6 as desired.
一种为此项目设置自定义环境变量的方法,这将允许我将 JAVA_HOME 设置为 JDK6 位置。这将使 Ant 根据需要选择并在 Java6 上运行。
When configuring the Build steps for Ant, under "Invoke Ant", if you click "Advanced", you can set custom Java options. The on-screen help says:
在为 Ant 配置构建步骤时,在“调用 Ant”下,如果单击“高级”,则可以设置自定义 Java 选项。屏幕帮助说:
If your build requires a custom ANT_OPTS, specify it here. Typically this may be used to specify java memory limits to use, for example -Xmx512m. Note that other Ant options (such as -lib) should go to the "Ant targets" field.
如果您的构建需要自定义 ANT_OPTS,请在此处指定。通常,这可用于指定要使用的 Java 内存限制,例如 -Xmx512m。请注意,其他 Ant 选项(例如 -lib)应转到“Ant 目标”字段。
I have a feeling this won't work for the JDK to run under, however.
但是,我有一种感觉,这不适用于 JDK 运行。
Have you configured multiple JDK installations for this Hudson instance under Manage Hudson
/ Configure System
?
您是否在Manage Hudson
/下为这个 Hudson 实例配置了多个 JDK 安装Configure System
?
回答by Mark
We have both Java 5 and Java 6 configured for use in our Hudson instance.
我们在 Hudson 实例中配置了 Java 5 和 Java 6。
Under Manage Hudson -> Configuration System you can add a number of JDKs and specify the path for JAVA_HOME. In the configuration for each job you then selected which JDK you would like that job to run on.
在 Manage Hudson -> Configuration System 下,您可以添加多个 JDK 并指定 JAVA_HOME 的路径。在每个作业的配置中,您然后选择了您希望该作业在哪个 JDK 上运行。
回答by Mark
you can use like this.
你可以这样使用。
in batch command window intially u can set the variable name JAVA_HOME, Assign this custom variable name to PATH variable. then u can call an ant script to choose specific file.
在批处理命令窗口中,您可以设置变量名 JAVA_HOME,将此自定义变量名分配给 PATH 变量。然后你可以调用一个蚂蚁脚本来选择特定的文件。
Example:
例子:
set JAVA_HOME=C:/java/jdk1.6.1
设置 JAVA_HOME=C:/java/jdk1.6.1
PATH=%JAVA_HOME%/bin;%PATH%
路径=%JAVA_HOME%/bin;%PATH%
ant build.xml
蚂蚁构建.xml
回答by johannes
Have a look at the Setenv Plugin. There you can set Variables like JAVA_HOME=C:/java/jdk1.6.1 PATH=%JAVA_HOME%/bin;%PATH%
看看 Setenv 插件。在那里你可以设置变量,如 JAVA_HOME=C:/java/jdk1.6.1 PATH=%JAVA_HOME%/bin;%PATH%
回答by Ralph
We managed this problem by using two different Java Hudson Nodes. One for Java 6 and one for Java 7. Then we assigned the Jobs to the different Nodes according to there needs.
我们通过使用两个不同的 Java Hudson 节点来解决这个问题。一种用于 Java 6,一种用于 Java 7。然后我们根据需要将作业分配给不同的节点。
回答by Obaid
Individual slave agents can be configured to use specific JDKs on the system you run them on.
单个从代理可以配置为在运行它们的系统上使用特定的 JDK。
Manage Nodes > Slave > Configure > Environment Variables
管理节点 > 从属 > 配置 > 环境变量
For example
例如
Name: JAVA_HOME Value: C:\Program Files (x86)\Java\jdk1.6.0_45\
名称:JAVA_HOME 值:C:\Program Files (x86)\Java\jdk1.6.0_45\
回答by javatar
The best way is,
最好的办法是,
- Install all required JDKs to your system.
- Add those to jenkins under JDK title in configuration page.
- Install Maven Info plugin to jenkins
- Restart jenkins
- Go to your job configuration page.
- Select required JDK from the JDK combo box appear under Maven Info Plugin Configuration
- Build it
- Enjoy!!!
- 将所有必需的 JDK 安装到您的系统中。
- 将这些添加到配置页面中 JDK 标题下的 jenkins 中。
- 将 Maven Info 插件安装到 jenkins
- 重启詹金斯
- 转到您的作业配置页面。
- 从 JDK 组合框中选择所需的 JDK 出现在 Maven Info Plugin Configuration 下
- 建造它
- 享受!!!
回答by diptia
I had a problem where the installed JDK was JDK 8 whereas I wanted Jenkins to use JDK 7 . So , after installing JDK 7 on the Jenkins build box ,
我有一个问题,安装的 JDK 是 JDK 8 而我希望 Jenkins 使用 JDK 7 。因此,在 Jenkins 构建框上安装 JDK 7 之后,
I added the JDK path to jenkins Configurations :
我将 JDK 路径添加到 jenkins Configurations :
Jenkins -> Manage Jenkins -> Configure system -> JDK -> Add JDK name and Path
Jenkins -> Manage Jenkins -> Configure system -> JDK -> Add JDK name and Path
Also , Edit jenkins-runner.sh: Add the line:
另外,编辑 jenkins-runner.sh:添加以下行:
export JAVA_HOME=""
导出 JAVA_HOME=""
Change the last two lines to read:
将最后两行改为:
echo "/bin/java" $javaArgs -jar "$war" $args
echo "/bin/java" $javaArgs -jar "$war" $args
exec "/bin/java" $javaArgs -jar "$war" $args
exec "/bin/java" $javaArgs -jar "$war" $args