【问题标题】:I have a groovy utils file with a few methods in it how can one method call another我有一个 groovy utils 文件,其中包含一些方法,一种方法如何调用另一种方法
【发布时间】: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


【解决方案1】:

没关系,这确实有效。 当我尝试在 groovysh 中本地运行文件内容时出现错误。但是在 Jenkins 管道中运行时没有错误

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-10
    • 1970-01-01
    • 2012-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-05
    • 2014-05-08
    相关资源
    最近更新 更多