groovy,脚本:这和jenkinsfile

时间:2018-02-14 14:34:08

标签: jenkins groovy jenkins-pipeline

基于此: println in "call" method of "vars/foo.groovy" works, but not in method in class

我正在尝试使用jenkins shared libraries从Jenkins管道中创建的类打印到控制台。我有以下内容:

MyPipeline.groovy

node("test") {
  stage("Test") {
          def a = new A(script:this)
          echo "Calling A.a()"
          a.a()
  }
}

A.groovy

class A {
    Script script;
    public void a() {
        script.echo("Hello from A")

        def b = new B(script)
        echo "Calling B.b()"
        b.b()
    }
}

B.groovy

class B {
    Script script;
    public void b() {
        script.echo("Hello from B")
    }
}

当我跑步时,我得到:

"Hello from A"

然后我从B得到错误:

groovy.lang.GroovyRuntimeException: Could not find matching constructor for: samples.B(samples.MyPipeline)

在委托给其他类时,如何在我的类中打印到控制台/构建日志 - 如上例中的B?

正如以下回答所示,我现在尝试将 A.groovy 更新为:

class A {
    Script script;
    public void a() {
        script.echo("Hello from A")
        def b = new B()
        b.script = script
        //def b = new B(script)
        echo "Calling B.b()"
        b.b()
    }
}

但这只是一个新错误:

hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: samples.A.echo() is applicable for argument types: (java.lang.String) values: [Calling B.b()]
Possible solutions: each(groovy.lang.Closure), getAt(java.lang.String), wait(), a(), every(), grep()

1 个答案:

答案 0 :(得分:1)

根据groovy的Initializing beans with named parameters and the default constructor

只需调用空构造函数并设置参数脚本

def b = new B()

b.script = script
  

使用像:

这样的bean      

类Server {       字符串名称       群集群}

     

而不是在后续语句中设置每个setter如下:

     

def server = new Server()

     

server.name =“Obelix”

     

server.cluster = aCluster

同时替换以下echo

 echo "Calling B.b()"

使用script.echo方法:

 script.echo("Calling B.b()")
相关问题