簡體   English   中英

如何在Jenkins管道中導入類文件?

[英]How to import a file of classes in a Jenkins Pipeline?

我有一個包含類的文件。 范例:


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管道腳本中導入文件,因此可以創建我的一個類的對象。 例如 :


def vTest = new Test()
echo vTest.fTest()
def vTest2 = new Test2()
echo vTest2.func()

如何在Jenkins Pipeline中導入文件? 謝謝。

您可以這樣:

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")
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM