简体   繁体   English

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

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

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 . Maven 安装有一个系统范围的 Jenkins 配置:管理 Jenkins =>配置系统=> Maven / Maven 安装 And if I add Maven installation using this web UI (by providing pre-installed path in MAVEN_HOME as /usr/share/maven ), the job runs SUCCESSFULLY.如果我使用此 Web UI 添加 Maven 安装(通过在MAVEN_HOME提供预安装的路径作为/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 运行作业的 Slaves 动物园可能包含具有各种平台、操作系统、不同版本 Maven 安装在不同位置的环境的 Slaves。

Question: How would I configure Maven installation per Slave node?问题:如何为每个 Slave 节点配置 Maven 安装?

Setting environment variables like MAVEN_HOME and M2_HOME to the same path for entire system on Slave node didn't work.MAVEN_HOMEM2_HOME等环境变量设置为 Slave 节点上整个系统的相同路径不起作用。

Versions版本

Both Jenkins Master and Slave are Linux hosts. Jenkins Master 和 Slave 都是 Linux 主机。 Jenkins version: 1.598詹金斯版本: 1.598

Assuming you have Java and Maven installed on your slave:假设您的从站上安装了 Java 和 Maven:

  1. Go to Manage Jenkins -> Manage Nodes转到管理 Jenkins -> 管理节点
  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.填写 Java 和 Maven 的选项。

It should now work (even if you have configured a Maven installation on the master).它现在应该可以工作了(即使您已经在 master 上配置了 Maven 安装)。

For me, the solution was to go to Manage Jenkins -> Global Tool Configuration, and set up maven there.对我来说,解决方案是转到 Manage Jenkins -> Global Tool Configuration,然后在那里设置 maven。 You can access this via /configureTools (ex: http://your-ip:8080/jenkins/configureTools )您可以通过 /configureTools 访问它(例如: http://your-ip:8080/jenkins/configureTools

Example: Maven installations示例:Maven 安装

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

Additional configurations for other common tools at /configureTools : /configureTools 中其他常用工具的其他配置:

Git installations Git 安装

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

JDK installations JDK 安装

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

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 和本地设置。

To make it simple, just use my public docker image为了简单起见,只需使用我的公共 docker 镜像

docker push chiraggupta95/jenkinswithmvn:v1

In case you want to customize your own image, just check out my git repo for complete instructions.如果您想自定义自己的图像,只需查看我的 git repo 以获取完整说明。

 https://github.com/chiraggupta95apr/jenkins

Happy Coding :)快乐编码:)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM