首页 > 解决方案 > 我有一个 groovy utils 文件,其中包含一些方法,一种方法如何调用另一种方法

问题描述

是否可以有一个 .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: []

标签: jenkinsgroovyjenkins-pipeline

解决方案


Nevermind this does work. I got the error when I tried to run the contents of the file locally in groovysh. But no errors when it ran in the Jenkins pipeline


推荐阅读