将列表从一个 groovy 文件导入到另一个文件

Import list from one groovy file to another

我有两个 groovy 个文件。

First.groovy:

class First {

    def getContent() {
        content = ["one", "two"]
    }
}

return this

和second.groovy:

{
    node(label) {

        def listClass = this.class.classLoader.parseClass("First.groovy")
        def CONTENT=listClass.getContent()
}

当我 运行 作业时,我从 jenkins 收到错误:

hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: java.lang.Class.getContent() is applicable for argument types: () values: []

请就将列表从一个 groovy 文件导入另一个文件的有效方法提出建议。

Jenkins 为您的情况提供了 load(filepath) 方法,因此您不必处理所有类加载器方法。

您的 First.groovy 中不一定需要 class,因此只需将其删除即可。

First.groovy

def getContent() {
    def content = ["one", "two"]
    return content
}

return this;

并这样称呼它:

def first = load 'First.groovy'
def content = first.getContent()