groovy Jenkins管道:无此类DSL方法

nhhxz33t  于 2022-11-01  发布在  Jenkins
关注(0)|答案(4)|浏览(328)

这个代码在Jenkins管道中出现了一个错误。我不明白为什么?我遗漏了什么吗?

node {
   stage 'test'
   def whatThe = someFunc('textToFunc')
   {def whatThe2 = someFunc2('textToFunc2')}
}

def someFunc(String text){
    echo text
    text
}
def someFunc2(String text2){
    echo text2
    text2
}

错误:

java.lang.NoSuchMethodError:**No such DSL method 'someFunc'**found among [archive, bat, build, catchError, checkout, deleteDir, dir, echo, emailext, emailextrecipients, error, fileExists, git, input, isUnix, load, mail, node, parallel, properties, pwd, readFile, readTrusted, retry, sh, sleep, stage, stash, step, svn, timeout, timestamps, tool, unarchive, unstash, waitUntil, withCredentials, withEnv, wrap, writeFile, ws]
        at org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:124)
        at org.jenkinsci.plugins.workflow.cps.CpsScript.invokeMethod(CpsScript.java:117)
        at groovy.lang.MetaClassImpl.invokeMethodOnGroovyObject(MetaClassImpl.java:1280)
        at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1174)
        at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1024)
        at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:42)
        at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
        at com.cloudbees.groovy.cps.sandbox.DefaultInvoker.methodCall(DefaultInvoker.java:15)
        at WorkflowScript.run(WorkflowScript:4)
        at ___cps.transform___(Native Method)
        at com.cloudbees.groovy.cps.impl.ContinuationGroup.methodCall(ContinuationGroup.java:55)
        at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:106)
        at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixArg(FunctionCallBlock.java:79)
        at sun.reflect.GeneratedMethodAccessor878.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
        at com.cloudbees.groovy.cps.impl.ClosureBlock.eval(ClosureBlock.java:40)
        at com.cloudbees.groovy.cps.Next.step(Next.java:58)
        at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:154)
        at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:164)
        at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:360)
        at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$100(CpsThreadGroup.java:80)
        at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:236)
        at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:226)
        at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:47)
        at java.util.concurrent.FutureTask.run(Unknown Source)
        at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:112)
        at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
        at java.util.concurrent.FutureTask.run(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at java.lang.Thread.run(Unknown Source)
    Finished: FAILURE
6g8kf2rb

6g8kf2rb1#

请删除sumfunc2调用周围的多余括号:

node {
   stage 'test'
   def whatThe = someFunc('textToFunc')
   def whatThe2 = someFunc2('textToFunc2')
}

def someFunc(String text){
    echo text
    text
}
def someFunc2(String text2){
    echo text2
    text2
}

更新日期:

在Groovy中,如果方法的最后一个参数是Closure类型,那么在调用该方法时,闭包可以在括号之外,如下所示:

def foo(whatever, Closure c) {}

// Can be invoked as
foo(whatever, {
    // This is the second argument of foo of type Closure
})

// It is also the same as writing
foo(whatever) {
    // This is the second argument of foo of type Closure
}

原始函数引发的原因是以下代码

def whatThe = someFunc('textToFunc')
{def whatThe2 = someFunc2('textToFunc2')}

代码与相同

def whatThe = someFunc('textToFunc') {
    def whatThe2 = someFunc2('textToFunc2')
}

这意味着解释器要查找的是

someFunc(String text, Closure c)

而没有这样的方法

zsohkypk

zsohkypk2#

由于这个答案是我在查找“No such DSL method”消息时找到的第一个答案,我想补充的是,它也可能是不匹配的接口。在我的例子中,第一个参数是一个列表,但我试图用数组调用该方法。因此,请检查您的接口是否与您期望的匹配,以及您的参数是否正确传入。

htrmnn0y

htrmnn0y3#

在我的例子中,发生的事情是,我引用了一个变量,使用:正在扩展为空字符串的${}:
env.VAR1=env.VAR2
在我的例子中,var 2并不存在,所以我要做的是:
env.VAR1=env.VAR2 .
我犯了个愚蠢的错误。

eqqqjvef

eqqqjvef4#

您可能还忘记了在包括您的库...
也就是说

@Library('shared-library') _

相关问题