在类环境中调用时,jenkins println 输出中的 Groovy 脚本消失
类函数内 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我自己在这里找到了解决方案 http://mriet.wordpress。 com。
当 Groovy 插件启动时,会将两个绑定传递给脚本。从绑定中我们可以获得out变量。获取它并使用 out.println 输出到脚本控制台,而不是普通的 println。
下面的脚本显示了完整的解决方案。
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.
如果您使用 skript 作为构建后步骤(我不确定它是否适用于提到的 CLI),您可以使用记录器中的构建:
所以在您的情况下,类似这样的内容可能会有所帮助:
另请参阅 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:
So in your case something like this may be helpful:
See also Example 10 in Groovy Plugin Doc
此邮件列表帖子 帮忙?
Does this mailing list post help?
对我来说效果很好的一个简单解决方案是在每个脚本的顶部添加这一行。这使得可以在整个代码(类内部和外部)中使用传统的 println 命令,从而使代码直观。
这使得能够创建如下日志条目:
它将 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.
This enables to create log entries like this:
It replaces the default print stream in
System.out
with the one handed over from Jenkins via bindings.