将Jenkins共享库用作类 [英] Using Jenkins Shared Libraries as classes
问题描述
我有一个Jenkins文件,并且我试图从共享库中实例化一个常规类.我得到无法解析类测试"
I have a Jenkins file, and i'm trying to instantiate a groovy class from my shared library. I get "unable to resolve class Test "
我在共享库中有一个src/com/org/foo.groovy文件:
I have a src/com/org/foo.groovy file in a shared library :
package com.org
class Test implements Serializable{
String val
Test(val) {
this.val = val
}
}
我正在尝试在我的jenkinsfile中实例化它
and I'm trying to instantiate it in my jenkinsfile
@Library('Shared-Library@master')
import com.org //also tried to use with .foo with no success
def t = new Test("a") //doesnt work
def t = new foo.Test("a")//doesnt work
def t = new com.org.foo.Test("a")//doesnt work
起作用的是,如果我将该文件称为一个类(我无权访问其构造函数).那就是:
What does work is if I refer to the file as a class (which I don't have the access to its constructor). That is:
@Library('Shared-Library@master')
def t = new foo.com.org.foo()
这很好,让我使用foo函数.但是,我失去了赋予类常量并使用参数构造它的能力.
This is nice, and lets me use foo functions. However, I lose the power to give the class constants and construct it with parameters.
有什么想法可以定义和使用共享库中的类吗? 谢谢
Any idea how I can define and use a class from shared library? Thanks
推荐答案
- 您的课程范围是默认范围.您可以将范围更改为公开
- 由于您在脚本块之外创建了一个类的对象,因此引发错误.尝试下面的代码,它应该可以工作.尝试下面的代码
@Library('Shared-Library@master')
import com.org.*;
stages{
stage('Demo') {
steps{
script{
def t = new Test("a") //this should work
}
}
}
}
这篇关于将Jenkins共享库用作类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!