【问题标题】:Jenkins pipeline mvn: command not foundJenkins管道mvn:找不到命令
【发布时间】:2020-04-09 18:53:14
【问题描述】:

我的测试作为 Maven 项目运行良好。它从 Git 中获取代码。

我想在管道中执行相同的操作,所以我为管道项目编写了以下脚本。

pipeline {
    agent any
    options {
        timestamps()
    }
    stages{ 
        stage('Just Testing') {
            steps {
                git "https://github.com/myRepo.git"
                script{
                        sh 'mvn test'
                }
                step([$class : 'Publisher', reportFilenamePattern : "**/testng-results.xml"])
            }
        }
    }   
}

但是当我执行时我得到 mvn: command not found error Java and Maven path is set correct in global tool configuration(我确信因为我能够执行其他 maven 项目,只有管道失败。)

控制台输出:

Running on Jenkins in /Users/abacker/.jenkins/workspace/parallelTestNG
[Pipeline] {
[Pipeline] timestamps
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Just Testing)
[Pipeline] git
00:03:52  No credentials specified
00:03:52   > git rev-parse --is-inside-work-tree # timeout=10
00:03:52  Fetching changes from the remote Git repository
00:03:52   > git config remote.origin.url https://github.com/myRepo.git # timeout=10
00:03:52  Fetching upstream changes from https://github.com/myRepo.git
00:03:52   > git --version # timeout=10
00:03:52   > git fetch --tags --force --progress -- https://github.com/myRepo.git +refs/heads/*:refs/remotes/origin/* # timeout=10
00:03:54   > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
00:03:54   > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
00:03:54  Checking out Revision e82f48106aa72d0275591926d9cd3226ba0fefd7 (refs/remotes/origin/master)
00:03:54   > git config core.sparsecheckout # timeout=10
00:03:54   > git checkout -f e82f48106aa72d0275591926d9cd3226ba0fefd7 # timeout=10
00:03:54   > git branch -a -v --no-abbrev # timeout=10
00:03:54   > git branch -D master # timeout=10
00:03:54   > git checkout -b master e82f48106aa72d0275591926d9cd3226ba0fefd7 # timeout=10
00:03:54  Commit message: "First commit"
00:03:54   > git rev-list --no-walk e82f48106aa72d0275591926d9cd3226ba0fefd7 # timeout=10
[Pipeline] script
[Pipeline] {
[Pipeline] sh
00:03:54  + mvn test
00:03:54  /Users/abacker/.jenkins/workspace/parallelTestNG@tmp/durable-818e64a8/script.sh: line 1: mvn: command not found
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // timestamps
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 127
Finished: FAILURE

我该如何解决这个问题?

【问题讨论】:

    标签: maven jenkins jenkins-pipeline


    【解决方案1】:

    您没有为您的 Maven 项目声明任何工具。

    这将是从 Jenkins 运行 mvn 目标的良好起点。 Declarative maven project.

    【讨论】:

    • 这行得通,谢谢。添加工具 { maven 'M2_HOME' jdk 'JAVA_HOME' } 在全局工具配置中设置路径。
    【解决方案2】:

    以下管道脚本为我完成了这项工作。与在 jenkins 中创建的 maven 项目不同,您需要添加 maven 和 java 作为脚本工具。 MAVEN_HOME - 在“管理 Jenkins”→“全局工具配置”中配置 JAVA_HOME - 在“管理 Jenkins”→“全局工具配置”中配置

    pipeline {
        agent any
        tools { 
          maven 'MAVEN_HOME' 
          jdk 'JAVA_HOME' 
        }
        stages {
          stage ('Build') {
            steps {
              sh 'mvn -B -ntp -Dmaven.test.failure.ignore verify'
            }
          }
    
        }
    }
    

    【讨论】:

      最近更新 更多