我有一个简单的管道脚本,可以访问groovy类(位于共享库中), 管道脚本将对自身的引用传递给该类, 然后该类调用脚本中找到的函数或闭包, 该函数访问同一脚本中的成员,
结果是一个groovy.lang.MissingPropertyException异常。
这是实际的(可运行的)代码:
Jenkins管道脚本:
@Library("testLib") _
import my.domain.Tester;
public Tester tester = new Tester(this);
def closure()
{
echo "tester: " + tester;
}
node("master")
{
tester.test();
}
com.domain.Tester.groovy类:
package my.domain
class Tester
{
Script scriptRef = null;
public Tester(Script scriptRef)
{
this.scriptRef = scriptRef;
}
public void test()
{
this.scriptRef.closure();
}
}
收到异常:
groovy.lang.MissingPropertyException: No such property: tester for class: WorkflowScript
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:53)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.getProperty(ScriptBytecodeAdapter.java:458)
at com.cloudbees.groovy.cps.sandbox.DefaultInvoker.getProperty(DefaultInvoker.java:39)
at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:20)
at WorkflowScript.closure(WorkflowScript:9)
at my.domain.Tester.test(Tester.groovy:8)
at WorkflowScript.run(WorkflowScript:23)
at ___cps.transform___(Native Method)
at com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.get(PropertyishBlock.java:74)
at com.cloudbees.groovy.cps.LValueBlock$GetAdapter.receive(LValueBlock.java:30)
at com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.fixName(PropertyishBlock.java:66)
at sun.reflect.GeneratedMethodAccessor538.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.ConstantBlock.eval(ConstantBlock.java:21)
at com.cloudbees.groovy.cps.Next.step(Next.java:83)
at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:174)
at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:163)
at org.codehaus.groovy.runtime.GroovyCategorySupport$ThreadCategoryInfo.use(GroovyCategorySupport.java:129)
at org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:268)
at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:163)
at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:186)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:370)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$200(CpsThreadGroup.java:93)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:282)
at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:270)
at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:67)
at java.util.concurrent.FutureTask.run(Unknown Source)
at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:131)
at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
at jenkins.security.ImpersonatingExecutorService$1.run(ImpersonatingExecutorService.java:59)
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
因此很明显,由于管道脚本通过了 cps转换 过程,因此发生了某种情况,并且脚本成员不再存在(不是按名称,也不是按范围[可能]),
答案 0 :(得分:0)
基于daggett解决方案的说明:
由于这只是一个常规类,所以应该应用常规范围界定规则, 在常规情况下,如果您声明var /'def',则可以不使用'def'前缀而将其声明为在脚本中的任何地方全局可用,
因此将管道脚本中的声明更改为此:
tester = new Tester(this)
就是这样!
Jenkins管道脚本:
@Library("testLib") _
import my.domain.Tester;
// note the definition without the 'def' or Tester or public
tester = new Tester(this);
def closure()
{
echo "tester: " + tester;
}
node("master")
{
tester.test();
}
或:
自Groovy 1.8版及更高版本开始,我们可以添加@Field批注并保持声明不变,
how-do-i-create-and-access-the-global-variables-in-groovy
所以我们还可以更改管道,使其看起来像这样:
Jenkins管道脚本:
@Library("testLib") _
import my.domain.Tester;
// note the definition without the 'def' or Tester or public
@Field Tester tester = new Tester(this);
def closure()
{
echo "tester: " + tester;
}
node("master")
{
tester.test();
}