jenkins构建java项目找不到命令mvn,java的解决方法

Posted Oops!

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jenkins构建java项目找不到命令mvn,java的解决方法相关的知识,希望对你有一定的参考价值。

jenkins构建java项目时出现的报错情况:

$ mvn clean install
FATAL: command execution failed
java.io.IOException: error=2, No such file or directory
	at java.lang.UNIXProcess.forkAndExec(Native Method)
	at java.lang.UNIXProcess.<init>(UNIXProcess.java:247)
	at java.lang.ProcessImpl.start(ProcessImpl.java:134)
	at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
Caused: java.io.IOException: Cannot run program "mvn" (in directory "/var/lib/jenkins/workspace/es"): error=2, No such file or directory



报错情况2:

/var/lib/jenkins/tools/hudson.tasks.Maven_MavenInstallation/maven_3.5.3/bin/mvn clean install
which: no java in (/var/lib/jenkins/tools/hudson.tasks.Maven_MavenInstallation/maven_3.5.3/bin:/sbin:/usr/sbin:/bin:/usr/bin)
The JAVA_HOME environment variable is not defined correctly
This environment variable is needed to run this program
NB: JAVA_HOME should point to a JDK not a JRE
Build step \'Invoke top-level Maven targets\' marked build as failure
Finished: FAILURE


报错情况3:

[INFO] Changes detected - recompiling the module!
[INFO] Compiling 33 source files to /var/lib/jenkins/workspace/es/target/classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
[INFO] 1 error
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 02:42 min
[INFO] Finished at: 2018-05-29T18:32:22+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.5.1:compile (default-compile) on project ruhnn-visual-search-background: Compilation failure
[ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
[ERROR] 
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException


解决方法:

安装maven  并配置到jenkins配置里
下载地址  https://maven.apache.org/download.cgi
wget  http://mirror.bit.edu.cn/apache/maven/maven-3/3.5.2/binaries/apache-maven-3.5.2-bin.tar.gz
tar zxvf apache-maven-3.5.2-bin.tar.gz
mv apache-maven-3.5.2 /usr/local/
/usr/loca/apache-maven-3.5.2/bin/mvn --version
jenkins -> 系统管理 -> 全局工具配置
Maven Configuration -> Settings file in filesystem -> File Path /usr/local/apache-maven-3.5.2/conf/settings.xml
Global settings file on filesystem -> File Path 同上
下拉到Maven,Maven安装,Name(自定义,如maven3.5.2),MAVEN_HOME填 “/usr/local/apache-maven-3.5.2”
左下角“save”

安装maven 也可以参考:http://www.cnblogs.com/weifeng1463/p/8920765.html

 

 

安装jdk 

jenkins -> 系统管理 -> 全局工具配置

JDK安装
JDK  别名  JDK1.8 
自动安装
          从java.sun.com 安装
          版本  Java SE Development Kit 8u162 

 

 




最后测试可以成功构建:




以上是关于jenkins构建java项目找不到命令mvn,java的解决方法的主要内容,如果未能解决你的问题,请参考以下文章

自动化测试脚本中包含adb命令,jenkins构建时找不到adb解决办法

jenkins指定pom打包

jenkins执行mvn -f ${project_name} clean package报错:找不到父工程

jenkins执行mvn -f ${project_name} clean package报错:找不到父工程

Jenkins 项目找不到包;构建项目目标失败

wedriverio jenkins - 找不到 npm 命令错误