Java 在 Jenkins 中找不到 Maven

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

Maven not found in Jenkins

javaspringmavenspring-mvcjenkins

提问by Charo

I am running my Maven/Spring project in Jenkins (just testing it out, first time) using the shell script option with:

我正在 Jenkins 中运行我的 Maven/Spring 项目(只是测试它,第一次)使用 shell 脚本选项:

mvn spring-boot:run

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. It works fine when I run mvn spring-boot:runvia command line.

我怎样才能解决这个问题?它是一个 Spring-boot 应用程序。当我mvn spring-boot:run通过命令行运行时它工作正常。

采纳答案by Anil Kumar Athuluri

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 安装路径并保存配置。

Maven config in Jenkins

Jenkins 中的 Maven 配置

回答by JinWeiMin

makesure you maven config is true. you can use cmdand run:

确保您的 Maven 配置为 true。您可以使用cmd并运行:

mvn -version

if no error, try

如果没有错误,请尝试

mvn spring-boot:run

回答by Bor Is

I had a similar problem on windows. Maven worked fine from console, but failed when called as cmd command from Jenkins.

我在 Windows 上遇到了类似的问题。Maven 从控制台运行良好,但在 Jenkins 中作为 cmd 命令调用时失败。

The reason is that Jenkins is installed and run as a ?system user‘ service. This means the process can‘t see the ENV variables and PATH of your user (user level). Setting up maven in PATH and ENV variables on system level makes the trick.

原因是 Jenkins 作为“系统用户”服务安装和运行。这意味着该进程无法看到您的用户(用户级别)的 ENV 变量和 PATH。在系统级别的 PATH 和 ENV 变量中设置 maven 是诀窍。

P.S.: @Charo the maven config moved from Configure System to Global Tool Configuratin in newer Jenkins versions

PS:@Charo 在较新的 Jenkins 版本中,maven 配置从配置系统移动到全局工具配置

回答by rakesh

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

回答by LIU YUE

"Manage Jenkins"->"Global Tool Configuration" enter image description here

“管理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

enter image description here

在此处输入图片说明

回答by mushroom_lb

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

and then mvn install

然后 mvn 安装

回答by Suneet Khurana

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.

    
    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.

  2. Or you can log in in to Jenkins user from the root and run following commands.

    
    su - Jenkins
    echo $PATH
    
  1. 您可以在 Jenkins GUI 的执行 shell 提示中附加以下几行。

    
    echo $M2_HOME
    echo $PATH
    mvn clean install
    

    当您再次构建时,您将看到 M2_HOME 或 maven bin 路径未在 $PATH 变量中设置。

  2. 或者您可以从 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 :-)