如何在Maven管道中使用Maven插件

问题描述

我在withMaven上使用詹金斯(Jenkins)腰线:

我尝试:

stage('Download binary') {
    steps {
        script {
            def Constants = com.comp.Constants
            withMaven(globalMavenSettingsConfig: 'empty-global-settings',mavenSettingsConfig: Constants.CONfig_SETTINGS_ID) {                  
                // download jar file
                sh "$MVN_CMD org.apache.maven.plugins:maven-dependency-plugin:get --define artifact=${artifact} --define transitive=false"
                sh "$MVN_CMD org.apache.maven.plugins:maven-dependency-plugin:copy --define artifact=${artifact} --define mdep.stripVersion=true --define outputDirectory=${WORKSPACE}"
            }
        }
    }
}

但是我有这个错误

$ docker exec 023505fd08232c4b1e385684169e15195a16d6af31928308374582e757e0e520 env printenv MAVEN_HOME
$ docker exec 023505fd08232c4b1e385684169e15195a16d6af31928308374582e757e0e520 env printenv M2_HOME
$ docker exec 023505fd08232c4b1e385684169e15195a16d6af31928308374582e757e0e520 env /bin/sh -c "which mvn"
[withMaven] Maven installation not specified in the 'withMaven()' step and not found on the build agent
...
hudson.remoting.ProxyException: groovy.lang.MissingPropertyException: No such property: MVN_CMD for class: WorkflowScript

我尝试这样做并解决了我的问题(这很奇怪而且很丑):

stage('stub for workaroud') {
    steps {
        script {
            def Constants = com.comp.Constants
            checkoutGitSCM(
                    url: "${Constants.BITBUCKET_URL}/stub.git",tag: Constants.STUB_TAG
                 )
            }
        }
    }
}

很奇怪,如果我添加一个checkoutGitSCM$MVN_CMD的步骤,并且有[withMaven] using Maven installation provided by the build agent with the environment variable MAVEN_HOME=/usr/local/share/maven

编辑:

添加sh "env"命令。在我拥有checkoutGitSCM之前,

MVN_CMD=/home/jenkins/workspace/MyJob@tmp/withMaven10a4d8db/mvn
MVN_CMD_DIR=/home/jenkins/workspace/ODM_Common_XLD_deploy@tmp/withMaven10a4d8db

如果我使用sh "$MVN_CMD_DIR/mvn org.apache.maven.plugins:maven-dependency-plugin:get...不再起作用,则需要在checkoutGitSCM之前使用withMaven

编辑2:

我将FINE的日志级别添加org.jenkinsci.plugins.pipeline.maven.WithMavenStepExecution2,结果是:

Sep 04,2020 3:29:45 PM FINE org.jenkinsci.plugins.pipeline.maven.WithMavenStepExecution2 obtainMavenExec
Setting up maven: null
Sep 04,2020 3:29:45 PM FINE org.jenkinsci.plugins.pipeline.maven.WithMavenStepExecution2 obtainMavenExec
Searching for Maven through MAVEN_HOME and M2_HOME environment variables...
Sep 04,2020 3:29:45 PM FINE org.jenkinsci.plugins.pipeline.maven.WithMavenStepExecution2 obtainMavenExec
Calling printenv on docker container...
Sep 04,2020 3:29:45 PM FINE org.jenkinsci.plugins.pipeline.maven.WithMavenStepExecution2 obtainMavenExec
NO maven installation discovered on docker container through MAVEN_HOME and M2_HOME environment variables
Sep 04,2020 3:29:45 PM FINE org.jenkinsci.plugins.pipeline.maven.WithMavenStepExecution2 obtainMavenExec
Found exec for maven on: null

编辑3:

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)