java:在Jenkins管道中运行Ant时找不到

时间:2018-07-19 04:07:29

标签: java jenkins ant jenkins-pipeline

我在通过Jenkins管道运行ant时遇到问题。运行JenkinsFile时出现以下错误。

Started by user xxxxxx
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/xxxxxx_workspace
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Test)
[Pipeline] tool
[Pipeline] tool
[Pipeline] sh
[xxxxxx_workspace] Running shell script
+ java -version
openjdk version "1.8.0_151"
OpenJDK Runtime Environment (IcedTea 3.6.0) (Alpine 8.151.12-r0)
OpenJDK 64-Bit Server VM (build 25.151-b12, mixed mode)
[Pipeline] withAnt
[Pipeline] {
[Pipeline] sh
[xxxxxx_workspace] Running shell script
+ ant -d -version
/var/jenkins_home/tools/hudson.tasks.Ant_AntInstallation/ant_1_10_5/bin/ant: exec: line 1: /var/jenkins_home/tools/hudson.model.JDK/jdk_8u181/jre/bin/java: not found
[Pipeline] }
[Pipeline] // withAnt
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 127
Finished: FAILURE

我的JenkinsFile如下:

node {
  stage('Test') {
    env.JAVA_HOME="${tool 'jdk_8u181'}"
    env.ANT_HOME="${tool 'ant_1_10_5'}"
    env.PATH = "${env.JAVA_HOME}/bin:${env.ANT_HOME}/bin:${env.PATH}"
    sh "java -version"
    withAnt(installation: 'ant_1_10_5', jdk: 'jdk_8u181') {
    //  echo "Test ant"
      sh "ant -d -version"
    }
  }
}

我在Jenkins Global Tools Configuration中具有Java和Ant设置

JDK设置屏幕截图 https://i.stack.imgur.com/DdMRv.png

Ant Setup屏幕截图 https://i.stack.imgur.com/WQr7v.png

JDK和Ant的文件路径,可以确认它们是否可用。注意我正在使用docker,因此/ var / jenkins_home已安装到/ opt / jenkins,但无论如何您都可以在控制台输出中看到'sh“ java -version”'正常工作

[root@jenkins jre]# ls -al /opt/jenkins/tools/hudson.model.JDK/jdk_8u181/jre/bin/java
-rwxr-xr-x 1 centos centos 7734 Jul  7 16:06 /opt/jenkins/tools/hudson.model.JDK/jdk_8u181/jre/bin/java
[root@jenkins jre]# ls -al /opt/jenkins/tools/hudson.tasks.Ant_AntInstallation/ant_1_10_5/bin/ant
-rwxr-xr-x 1 centos centos 11638 Jul 19 10:54 /opt/jenkins/tools/hudson.tasks.Ant_AntInstallation/ant_1_10_5/bin/ant
[root@jenkins jre]#

1 个答案:

答案 0 :(得分:1)

通知

[xxxxxx_workspace] Running shell script
+ java -version
openjdk version "1.8.0_151"
                       ^^^

但是

line 1: /var/jenkins_home/tools/hudson.model.JDK/jdk_8u181/jre/bin/java: not found
                                                       ^^^

运行java -version时,您将获得PATH中的所有内容。在实际的编译中,您尝试在 不同 特定路径下执行Java。根据错误消息,路径很可能不存在。