如何修复'哈德逊.remoting.ProxyException:groovy.lang.MissingMethodException:无方法签名:testFunc.call()'

oxcyiej7  于 8个月前  发布在  其他
关注(0)|答案(3)|浏览(61)

我正在尝试使用Jenkins中的共享库调用函数。共享库groovy文件位于存储库的/vars文件夹中。
我已尝试使用其他名称作为共享库文件。我也尝试使用testFunc.call()、testFunc、testFunc()调用它
testFunc.groovy

import hudson.model.*
import hudson.EnvVars
import groovy.json.JsonSlurperClassic
import groovy.json.JsonBuilder
import groovy.json.JsonOutput
import groovy.json.*
import java.net.URL
def coveragepercentage
def testFunc(){

    def param = "${env:TestCoverage}"
    echo param
    def paramInt = param as int
    echo "Integer "+ paramInt
    def jsondata = readFile(file:'./target/site/munit/coverage/munit-coverage.json')
           def data = new JsonSlurperClassic().parseText(jsondata)
           coveragepercentage = data.coverage
           echo "${coveragepercentage}"

        if(coveragepercentage > paramInt){
           println("This value is smaller")
           sh "exit 1"
           currentBuild.result = 'FAILURE'
                }
}

使用上述共享库的Jenkinsfile

@Library('shared-lib2') _
import hudson.model.*
import hudson.EnvVars
import groovy.json.JsonSlurperClassic
import groovy.json.JsonBuilder
import groovy.json.JsonOutput
import groovy.json.*
import java.net.URL
pipeline{
  agent any
    tools {
      maven 'Maven'
      jdk  'JAVA'
        }
    stages {
        stage('build') {
            steps {
                configFileProvider([configFile(fileId: 'config', variable: 'MAVEN_SETTINGS_XML')]) {
                sh "mvn -s $MAVEN_SETTINGS_XML clean package"
                }
            }
        }
        stage('test function'){
            steps{
                script{
                testFunc()
                }
            }
        }

        stage('MUnit Test Report') {
               steps{
                script {
                  publishHTML(target:[allowMissing: false,alwaysLinkToLastBuild: true,keepAll: true,reportDir: 'target/site/munit/coverage',reportFiles: 'summary.html',reportName: 'MUnit Test Report',reportTitles: 'MUnit Test Coverage Report'])
                }
                  }
            }   
    }

错误:哈德逊.remoting.ProxyException:groovy.lang.MissingMethodException:无方法签名:testFunc.call()适用于参数类型:()values:[]

gc0ot86w

gc0ot86w1#

在我的情况下,

call() {

虽然应当

def call() {

可惜了

pb3skfrl

pb3skfrl2#

testFunc.groovy中,需要将函数从testFunc重命名为call()
在这些更改之后,可以将您的函数作为声明性步骤调用。

x7rlezfr

x7rlezfr3#

我把它添加到我的Groovy中,它对我很有效。不知道这是否有帮助!

解决方案

def call(body) {
def config = [:]
body.resolveStrategy = Closure.DELEGATE_FIRST
body.delegate = config
body()

pipeline{
//your code
}
}

相关问题