我在共享库的 /var 目录中使用了一些全局方法,一切正常。现在我需要保持进程的状态,所以我正在编写一个 groovy 类。基本上我在“/src”中有一个名为“ClassTest.groovy”的类,它是这样的;
class ClassTest {
String testString
def method1() { ... }
def method2() { ... }
}
在管道开始时
library 'testlibrary@'
import ClassTest
结果:
WorkflowScript:2:无法解析类 ClassTest @line 2,column 1.
import ClassTest
以前,我刚刚好
library 'testlibrary@' _
并使用这些方法
script {
libraryTest.method1()
...
libraryTest.method2()
}
方法在文件'/var/libraryTest.groovy'中,一切正常。所以我知道共享库就在那里,但我对 groovy / Jenkins 处理类 / 共享库的方式感到困惑。
导入类的正确方法是什么?我在文档中找不到简单的示例(带有 groovy 文件、文件结构和管道)。
编辑:我将文件移动到'src/com/company/ClassTest.groovy'并将管道修改为
@Library('testlibrary@') import com.company.ClassTest
def notification = new ClassTest()
但现在错误是
意外令牌:包@第 2 行
groovy 文件的前两行是:
// src/com/company/ClassTest.groovy
package com.company;