【发布时间】:2018-08-11 01:30:22
【问题描述】:
是否可以有一个 .groovy 文件,其中定义了一些实用函数,并让其中一个函数在该文件中调用另一个函数?
注意:对于上下文,这用于 vars 文件夹下的 Jenkins Pipeline 库。我想让一个用于参数验证的函数在同一个 groovy 脚本文件中调用另一个函数。
即让 someFunction 使用 doSomething 函数,下面是一些伪代码。
//utils.groovy
def doSomething(def a) {
def aPrime = a
if (a == 'somethingSpecial') {
//handle it
//some logic goes here
aPrime = b
}
return aPrime
}
def someFunction(def x) {
y = doSomething(x);
more stuff.. using y
return someResult
}
def dodad() {
...
}
def whatsIt(){
...
}
在我的实际代码中,我收到类似No signature of method: groovysh_evaluate.myCommonFunct() is applicable for argument types: () values: []的错误
【问题讨论】:
-
这是一个典型的 Groovy 错误消息,基本上是说“你尝试调用一些东西,它确实存在 noch”。显然,该函数没有参数,所以这可能是一个问题。调用
this.myCommonFunct()或确保在任何使用它的方法之前定义myCommonFunct()也可能会有所帮助,因为这是一个脚本而不是一个类。 -
在我看来,您遇到的问题与最初对您的看法不同。在 groovy 中,您的要求完全有可能。由于您提到了 Jenkins Pipeline,我们需要查看 Pipeline 脚本来提供帮助。那里一定有什么问题。 myCommonFunct() 是在哪里定义的?
标签: jenkins groovy jenkins-pipeline