简体   繁体   English

Maven 在 Jenkins 中找不到

[英]Maven not found in Jenkins

I am running my Maven/Spring project in Jenkins (just testing it out, first time) using the shell script option with:我正在使用 shell 脚本选项在 Jenkins 中运行我的 Maven/Spring 项目(只是第一次测试):

mvn spring-boot:run

I get the build error: /Users/Shared/Jenkins/tmp/jenkins8087926087546049217.sh: line 2: mvn: command not found我收到构建错误: /Users/Shared/Jenkins/tmp/jenkins8087926087546049217.sh: line 2: mvn: command not found

How can I fix this?我怎样才能解决这个问题? Its a Spring-boot app.它是一个 Spring-boot 应用程序。 It works fine when I run mvn spring-boot:run via command line.当我通过命令行运行mvn spring-boot:run时它工作正常。

"Manage Jenkins"->"Global Tool Configuration" “管理Jenkins”->“全局工具配置” 在此处输入图片说明

if still not work, add the following to "Execute shell"如果仍然不起作用,请将以下内容添加到“执行shell”

export MAVEN_HOME=/opt/maven
export PATH=$PATH:$MAVEN_HOME/bin
mvn --version
mvn clean package

在此处输入图片说明

Try this, Navigate to Manage Jenkins and click on Global Tool Configuration, In Maven section click on the 'Add Maven' button and provide the maven installation path in MAVEN_HOME and save configurations.试试这个,导航到管理 Jenkins 并单击全局工具配置,在 Maven 部分单击“添加 Maven”按钮并在 MAVEN_HOME 中提供 Maven 安装路径并保存配置。

Jenkins 中的 Maven 配置

I had a similar problem on windows.我在 Windows 上遇到了类似的问题。 Maven worked fine from console, but failed when called as cmd command from Jenkins. Maven 从控制台运行良好,但在 Jenkins 中作为 cmd 命令调用时失败。

The reason is that Jenkins is installed and run as a ‚system user' service.原因是 Jenkins 作为“系统用户”服务安装和运行。 This means the process can't see the ENV variables and PATH of your user (user level).这意味着该进程无法看到您的用户(用户级别)的 ENV 变量和 PATH。 Setting up maven in PATH and ENV variables on system level makes the trick.在系统级别的 PATH 和 ENV 变量中设置 maven 是诀窍。

PS: @Charo the maven config moved from Configure System to Global Tool Configuratin in newer Jenkins versions PS:@Charo 在较新的 Jenkins 版本中,maven 配置从配置系统移动到全局工具配置

makesure you maven config is true.确保您的 Maven 配置为 true。 you can use cmd and run:您可以使用cmd并运行:

mvn -version

if no error, try如果没有错误,请尝试

mvn spring-boot:run
export MAVEN_HOME=/opt/maven
export PATH=$PATH:$MAVEN_HOME/bin
mvn --version
mvn clean package

and then mvn install然后 mvn 安装

I had faced the similar issue where I have put the Name As maven-3.3.9我遇到了类似的问题,我将名称设置为 maven-3.3.9

And Placed the MAVEN_HOME LIKE : E:\\Softwares\\apache-maven-3.3.9\\bin并放置了 MAVEN_HOME LIKE : E:\\Softwares\\apache-maven-3.3.9\\bin

I have faced this issue in my ubuntu 16.04 machine and the actual reason is the maven bin directory path is not set $PATH variable.我在 ubuntu 16.04 机器上遇到过这个问题,实际原因是 maven bin 目录路径没有设置 $PATH 变量。

How to check?如何检查?

  1. you can append below lines in execute shell prompt of Jenkins GUI.您可以在 Jenkins GUI 的执行 shell 提示中附加以下几行。

     echo $M2_HOME echo $PATH mvn clean install

    When you will build again then you will see M2_HOME or maven bin path is not set in $PATH variable.当您再次构建时,您将看到 M2_HOME 或 maven bin 路径未在 $PATH 变量中设置。

  2. Or you can log in in to Jenkins user from the root and run following commands.或者您可以从 root 登录到 Jenkins 用户并运行以下命令。

     su - Jenkins echo $PATH

Solution::解决方案::


 1. logged in to root user.
 2. then this command cd /etc/profile.d/
 3. vim maven.sh then add below lines
    export M2_HOME=/usr/local/maven
    export PATH=$PATH:$M2_HOME/bin
 4. run this command chmod +x maven.sh
 5. then login to jenkins user (su - jenkins) then see again maven path is set or not via (echo $PATH) command.
 6. If the path is set then restart Jenkins service 
    service jenkins restart
    and re-login in Jenkin GUI (http://localhost:8080/)
 7. Now rebuild your job then you will get success :-)

I also met the problem and solved it.我也遇到了这个问题并解决了。 I think you may be using a "FreeStyle Project" instead of a "Maven Project" when you creating a new project via "New Item" button.我认为当您通过“新建项目”按钮创建新项目时,您可能使用的是“FreeStyle 项目”而不是“Maven 项目”。

In FreeStyle Project, Jenkins may not read environment variables in Linux server and the Maven installations in "Global Tool Configuration", so I configured environment variables repeatedly in "Configure System".在FreeStyle项目中,Jenkins可能不会在“全局工具配置”中读取Linux服务器和Maven安装中的环境变量,所以我在“配置系统”中重复配置了环境变量。 Then it worked.然后它起作用了。

Maybe this is an stupid answer but I forgot to put maven as tool in the Jenkinsfile.也许这是一个愚蠢的答案,但我忘了把 maven 作为 Jenkinsfile 中的工具。

pipeline {
    agent any
    tools {
        maven 'Maven 3.8.6'
        jdk 'Java 17.0.4.1'
    }
    stages {

    }
}

You can also get the mvn not found error if your agent is a docker image without maven in it如果您的代理是 docker 图像,其中没有 maven,您也可能会遇到 mvn not found 错误

  1. set Java JDK in jenkins manage在 jenkins 管理中设置 Java JDK
  2. set Maven in jenkins maange在 jenkins maange 中设置 Maven

in Command execute set在命令执行集

set path=C:\Program Files\apache-maven-3.8.7\bin -> your path from environment in windows cd C:\Users\X\IdeaProjects\ "nameofprocject" -> just copy from InteliJi/Eclipse mvn -Dtest=MainPageTests test -> goal from Maven to run设置路径=C:\ =MainPageTests 测试 -> 目标从 Maven 运行

i'am facing the same problem i configured the maven home and jdk home in jenkins configuration file Apache Maven 3.6.3 Maven home: /usr/share/maven Java version: 11.0.11, vendor: Ubuntu, runtime: /usr/lib/jvm/java-11-openjdk-amd64 Default locale: en, platform encoding: UTF-8 OS name: "linux", version: "5.4.0-90-generic", arch: "amd64", family: "unix"我面临同样的问题我在 jenkins 配置文件 Apache Maven 3.6.3 Maven home: /usr/share/maven Java version: 11.0.11, vendor: Ubuntu, runtime: /usr/lib 中配置了 maven home 和 jdk home /jvm/java-11-openjdk-amd64 默认语言环境:en,平台编码:UTF-8 操作系统名称:“linux”,版本:“5.4.0-90-generic”,arch:“amd64”,系列:“unix” ”

but i have a problem in jenkins its says that mvn command not found但是我在 jenkins 中有一个问题,它说找不到 mvn 命令

i'am running jenkins in docker我在 docker 中运行 jenkins

thank you for helping me感谢你们对我的帮助

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

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