在类环境中调用时,jenkins println 输出中的 Groovy 脚本消失

发布于 2024-12-09 11:42:28 字数 773 浏览 2 评论 0原文

类函数内 println 的输出丢失。

示例脚本(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()

我使用 Jenkins CLI 执行 groovy 脚本

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

它只输出:

课外

似乎该类隐式使用了 System.out.println 中的 println,并且 System.out 被定向到日志文件,但类外部的 println 使用了其他内容,该内容在脚本控制台中输出。以下代码显示了该行为。

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

输出:

第二

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

温暖的光 2024-12-16 11:42:28

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

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

下面的脚本显示了完整的解决方案。

import hudson.model.*

// Get the out variable
def out = getBinding().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)

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 full solution.

import hudson.model.*

// Get the out variable
def out = getBinding().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)
空气里的味道 2024-12-16 11:42:28

如果您使用 skript 作为构建后步骤(我不确定它是否适用于提到的 CLI),您可以使用记录器中的构建:

manager.listener.logger.println("some output")

所以在您的情况下,类似这样的内容可能会有所帮助:

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

output = new OutputClass(manager.listener.logger)

另请参阅 Groovy 插件文档中的示例 10

If you use the skript as a post build step (I'm not shure whether it works with the mentioned CLI) you can use the build in logger:

manager.listener.logger.println("some output")

So in your case something like this may be helpful:

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

output = new OutputClass(manager.listener.logger)

See also Example 10 in Groovy Plugin Doc

沐歌 2024-12-16 11:42:28

邮件列表帖子 帮忙?

输出被发送到标准输出,所以如果你检查日志文件,你
可能会看到这样的内容:INFO [STDOUT] Hello World

如果你坚持使用系统脚本,你必须将变量传递给
你的类,因为绑定在类中不可见(所以它是
传递到标准输出)。你应该使用这样的东西

public class Hello {
  static void say(out) {
    out << "Hello World "
  }
}
println "Started ..."
Hello.say(out)

Does this mailing list post help?

the output is sent to standard output, so if you check your log file, you
will probably see something like this: INFO [STDOUT] Hello World

if you insist on using system script, you have to pass out variable to
your class, as the binding is not visible inside the class (so it's
passed to standard output). You should use something like this

public class Hello {
  static void say(out) {
    out << "Hello World "
  }
}
println "Started ..."
Hello.say(out)
乄_柒ぐ汐 2024-12-16 11:42:28

对我来说效果很好的一个简单解决方案是在每个脚本的顶部添加这一行。这使得可以在整个代码(类内部和外部)中使用传统的 println 命令,从而使代码直观。

import hudson.model.*
System.out = getBinding().out;

这使得能够创建如下日志条目:

println("Outside class");

class OutputClass {
    OutputClass() {
        println ("Inside class")
    }
}

new OutputClass();

它将 System.out 中的默认打印流替换为 Jenkins 通过绑定移交的打印流。

A simple solution that worked well for me was to add this line on top of each script. This enables usage of traditional println commands all over the code (inside and outside of classes) leaving the code intuitive.

import hudson.model.*
System.out = getBinding().out;

This enables to create log entries like this:

println("Outside class");

class OutputClass {
    OutputClass() {
        println ("Inside class")
    }
}

new OutputClass();

It replaces the default print stream in System.out with the one handed over from Jenkins via bindings.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文