在类环境中调用时,jenkins println输出中的Groovy脚本消失 [英] Groovy-script in jenkins println output disappears when called inside class environment

查看:798
本文介绍了在类环境中调用时,jenkins println输出中的Groovy脚本消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



示例脚本(outputclass.groovy):

  class OutputClass 
{
OutputClass()
{
println(Inside class)// This is not show in控制台
}
}

println(Outside class)//仅在控制台中显示
output = new OutputClass()

我使用Jenkins CLI执行groovy脚本

  java -jar .. \ jenkins-cli.jar -s JENKINS_SERVER_URL groovy outputclass.groovy 

它只输出:


外部类别$ / b>

看起来类似于使用System.out.println中的println,并且System.out指向日志文件,但类外部的println使用了其他的东西,它在脚本控制台。以下代码显示了该行为。

  System.out.println(First)
println(Second )

输出:


第二个


如何显式设置输出设备输出到Jenkins脚本控制台?

解决方案

我在这里找到了解决方案 http://mriet.wordpress.com



当Groovy插件启动时,将两个绑定传递给剧本。从绑定中,我们可以得到 out 变量。获取它并使用 out.println 输出到脚本控制台,而不是普通的println。



下面的脚本显示了解决方案。 p>

  import hudson.model。* 

//获取变量
def config = new HashMap()
def bindings = getBinding()
config.putAll(bindings.getVariables())

def out = config ['out']

class OutputClass
{
OutputClass(out)//必须将out变量传递给类
{
out.println(Inside class)
}
}

out.println(Outside class)
output = new OutputClass(out)


The output from println from within a class function is lost.

An example script (outputclass.groovy):

class OutputClass
{
    OutputClass()
    {
        println("Inside class")  // This will not show in the console
    }
}

println("Outside class")  // Only this is shown in the console
output = new OutputClass()

I use Jenkins CLI to execute the groovy script

java -jar ..\jenkins-cli.jar -s JENKINS_SERVER_URL groovy outputclass.groovy

It only outputs this:

Outside class

It seems like the class inmplicitly uses println from System.out.println, and System.out is directed to the log files, but the println outside the class is using something else, which is outputted in the script console. The following code shows the behavior.

System.out.println("First")
println("Second")

Output:

Second

How do I explicitly set the output device to output to the Jenkins script console?

解决方案

I found the solution myself here http://mriet.wordpress.com.

When the Groovy plugin starts is passes two bindings to the script. From the bindings we can get the out variable. Get it and use out.println to output to the script console, not the plain println.

The script below shows the solution.

import hudson.model.*

// Get the out variable
def config = new HashMap()
def bindings = getBinding()
config.putAll(bindings.getVariables())

def out = config['out']

class OutputClass
{
    OutputClass(out)  // Have to pass the out variable to the class
    {
    out.println ("Inside class")
    }
}

out.println("Outside class")
output = new OutputClass(out)

这篇关于在类环境中调用时,jenkins println输出中的Groovy脚本消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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