如何在Jenkins管道中导入类文件? [英] How to import a file of classes in a Jenkins Pipeline?

查看:94
本文介绍了如何在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 Pipeline中导入文件?谢谢.


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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆