如何在 Jenkins 管道中导入类文件? [英] How to import a file of classes in a Jenkins Pipeline?
本文介绍了如何在 Jenkins 管道中导入类文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含类的文件.示例:
I have a file that contains classes. Example :
abstract class TestBase
{
String name
abstract def fTest()
def bobby(){
return "bobby"
}
}
class Test extends TestBase
{
def fTest(){
return "hello"
}
}
class Test2 extends TestBase
{
def fTest(){
return "allo"
}
def func(){
return "test :)"
}
}
我想在我的 Jenkins 流水线脚本中导入该文件,这样我就可以为我的班级创建一个对象.例如:
I want to import the file in my Jenkins pipeline script, so I can create an object of one of my class. For example :
def vTest = new Test()
echo vTest.fTest()
def vTest2 = new Test2()
echo vTest2.func()
<小时>
如何在我的 Jenkins 流水线中导入我的文件?谢谢.
How can I import my file in my Jenkins Pipeline ? Thx.
推荐答案
你可以这样做:
Classes.groovy
class A{
def greet(name){ return "greet from A: $name!" }
}
class B{
def greet(name){ return "greet from B: $name!" }
}
// this method just to have nice access to create class by name
Object getProperty(String name){
return this.getClass().getClassLoader().loadClass(name).newInstance();
}
return this
管道:
node{
def cl = load 'Classes.groovy'
def a = cl.A
echo a.greet("world A")
def b = cl.B
echo b.greet("world B")
}
这篇关于如何在 Jenkins 管道中导入类文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文