如何修复 groovy.lang.MissingMethodException:没有方法签名:Setup.call() 适用于参数类型:(java.util.LinkedHashMap

问题描述 投票:0回答:1

Error:hudson.remoting.ProxyException:groovy.lang.MissingMethodException:没有方法签名:Setup.call()适用于参数类型:(java.util.LinkedHashMap)值:[[CreationDate:2024-01- 13]] 可能的解决方案:call(groovy.lang.Closure)、wait()、any()、wait(long)、main([Ljava.lang.String;)、each(groovy.lang.Closure)

有两个脚本,管道脚本,其中 stageone 调用设置脚本(在另一个存储库中)并发送 jenkins 参数 createDate。

我该怎么做才能成功使用这个参数?当前的 def 调用代码与此功能无关,因此我需要修改已有的内容。我尝试了各种选项,但无法克服上述错误。

管道脚本:

node {
    stage ('stageone') {
        Setup(CreationDate: params.createDate)
    }

安装脚本:

def call(Closure body) {
    def config = [:]
    if (body) {
        body.resolveStrategy = Closure.DELEGATE_FIRST
        body.delegate = config
groovy jenkins-groovy
1个回答
0
投票

您可以在Setup.groovy中创建另一个调用方法

class StartupScript{            // <=== this just to emulate script in jenkins

    // existing call method calling a new one
    def call(Closure body){ call(null, body) }
    // new call method has 2 params but last one is optional
    def call(Map _config, Closure body=null){
        def config = _config ?: [:]
        if(body){
            body.resolveStrategy = Closure.DELEGATE_FIRST
            body.delegate = config
            body()
        }
        println "config = ${config}"
        return config
    }

}                               // <=== this just to emulate script in jenkins

def Startup=new StartupScript() // <=== this just to emulate script in jenkins

assert Startup(AAA:"aaa") == [AAA:"aaa"]
assert Startup{AAA="aaa"} == [AAA:"aaa"]
assert Startup(AAA:"aaa"){ BBB="bbb" } == [AAA:"aaa",BBB:"bbb"]

或保留一种方法并检查参数类型

    def call(Object body){
        if (body instanceof Closure) {
            ...old code here...
        } else if (body instanceof Map) {
            ...new code here...
            println(body.CreationDate)
        } else throw new Exception($"unsupported argument type: ${body?.getClass()}")
    }
© www.soinside.com 2019 - 2024. All rights reserved.