Java 如何在 Jenkins 中为每个单独的 Slave 节点配置 Maven 安装?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28387142/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 06:07:52  来源:igfitidea点击:

How to configure Maven installation in Jenkins per individual Slave node?

javamavenjenkins

提问by uvsmtid

Symptom

症状

If I create a Maven job in Jenkins (New Item=> Maven project, using the maven plugin) with all defaults and run it, I get this error:

如果我在 Jenkins(新项目=> Maven 项目,使用 Maven 插件)中使用所有默认值创建一个 Maven 作业并运行它,我会收到此错误:

Started by user anonymous
Building on master in workspace /var/lib/jenkins/jobs/job_name/workspace
ERROR: A Maven installation needs to be available for this project to be built.Either your server has no Maven installations defined, or the requested Maven version does not exist.
Finished: FAILURE

Maven runs perfectly from command line with just mvn.

Maven 只需使用mvn.

Unacceptable Workaround

不可接受的解决方法

There is a system-wide Jenkins configuration for Maven installation: Manage Jenkins=> Configure System=> Maven/ Maven installations. And if I add Maven installation using this web UI (by providing pre-installed path in MAVEN_HOMEas /usr/share/maven), the job runs SUCCESSFULLY.

Maven 安装有一个系统范围的 Jenkins 配置:管理 Jenkins=>配置系统=> Maven/ Maven 安装。如果我使用此 Web UI 添加 Maven 安装(通过在MAVEN_HOMEas 中提供预安装路径/usr/share/maven),该作业将成功运行。

Question: Why is it global Jenkins config and not Slave-specific one?

问题:为什么它是全局 Jenkins 配置而不是特定于 Slave 的配置?

The global Jenkins config does not make sense: Maven is run per Slave, not per Jenkins.

全局 Jenkins 配置没有意义:Maven 是每个 Slave 运行的,而不是每个 Jenkins 运行的。

The zoo of Slaves where Jenkins runs jobs may contain Slaves with various platforms, OSes, environments where different versions of Maven are installed in different locations.

Jenkins 运行作业的奴隶动物园可能包含具有各种平台、操作系统、不同版本 Maven 安装在不同位置的环境的奴隶。

Question: How would I configure Maven installation per Slave node?

问题:如何为每个 Slave 节点配置 Maven 安装?

Setting environment variables like MAVEN_HOMEand M2_HOMEto the same path for entire system on Slave node didn't work.

设置环境变量一样MAVEN_HOME,并M2_HOME为整个系统的Slave节点上的相同路径没有工作。

Versions

版本

Both Jenkins Master and Slave are Linux hosts. Jenkins version: 1.598

Jenkins Master 和 Slave 都是 Linux 主机。詹金斯版本:1.598

采纳答案by Tim Webster

Assuming you have Java and Maven installed on your slave:

假设您的从站上安装了 Java 和 Maven:

  1. Go to Manage Jenkins -> Manage Nodes
  2. Click configure icon on the node you wish to configure
  3. Scroll down to 'Node Properties' and tick the 'Tool Locations' checkbox
  4. Fill in the options for Java and Maven.
  1. 转到管理 Jenkins -> 管理节点
  2. 单击要配置的节点上的配置图标
  3. 向下滚动到“节点属性”并勾选“工具位置”复选框
  4. 填写 Java 和 Maven 的选项。

It should now work (even if you have configured a Maven installation on the master).

它现在应该可以工作了(即使您已经在 master 上配置了 Maven 安装)。

回答by giuspen

If you do NOT add Maven to the Master configuration and you just install it on every slave with their own possibly different environment variables (example with version 3.2.5)

如果您没有将 Maven 添加到 Master 配置中,并且您只是将它安装在每个具有可能不同的环境变量的从属设备上(例如版本 3.2.5)

M2_HOME=C:\apache-maven-3.2.5
M2=C:\apache-maven-3.2.5\bin
Path+=;C:\apache-maven-3.2.5\bin

then every Jenkins slave will just use Maven with local settings.

那么每个 Jenkins slave 都会使用 Maven 和本地设置。

回答by Anonymous User

For me, the solution was to go to Manage Jenkins -> Global Tool Configuration, and set up maven there. You can access this via /configureTools (ex: http://your-ip:8080/jenkins/configureTools)

对我来说,解决方案是转到 Manage Jenkins -> Global Tool Configuration,然后在那里设置 maven。您可以通过 /configureTools 访问它(例如:http://your-ip:8080/jenkins/configureTools

Example: Maven installations

示例:Maven 安装

  • Name: my_maven_3.3.9
  • MAVEN_HOME: /usr/share/maven
  • 名称:my_maven_3.3.9
  • MAVEN_HOME:/usr/share/maven

Additional configurations for other common tools at /configureTools :

/configureTools 中其他常用工具的其他配置:

Git installations

Git 安装

  • Name: my_git_2.11.0
  • Path to Git executable: /usr/bin/git
  • 名称:my_git_2.11.0
  • Git 可执行文件的路径:/usr/bin/git

JDK installations

JDK 安装

  • Name: my_jdk_1.8.0_161
  • JAVA_HOME: /opt/jdk/jdk1.8.0_161
  • 名称:my_jdk_1.8.0_161
  • JAVA_HOME:/opt/jdk/jdk1.8.0_161