简体   繁体   English

使用Jenkins脚本控制台并查看Groovy输出

[英]Using Jenkins script console and seeing the Groovy output

When I run simple statements in the script console I can see the output printed, eg 当我在脚本控制台中运行简单的语句时,我可以看到打印的输出,例如

println "hello"

However with this code I see no output printed when run in Jenkins script console. 但是,使用此代码,我在Jenkins脚本控制台中运行时看不到任何输出。 Do you know why ? 你知道为什么吗 ? The code prints just fine when run from computer command line. 从计算机命令行运行时,代码可以正常打印。

class Product{

    private String name
    private def price
    def vendor

    public Product(){
    }

    Product(name, price, String vendor){
        println "Constructor";
        this.name = name
        this.price = price
        this.vendor = vendor
    }

    public String getName(){
        return name
    }

    def setName(name){
        this.name = name
    }

    public String getPrice(){
        return price
    }

    def setPrice(price = 100.00){
        this.price = price
    }

    def String toString(){
        return "Name = $name, Price = $price, Vendor = $vendor";
    }

    static main(arguments){

        def p1 = new Product("Mobile", "10000", "Nokia")
        println(p1.toString())
        println "Hello"
    }
}

AFAIK the script you're writing in the Jenkins console is actually the main function of a wrapper class. AFAIK您在Jenkins控制台中编写的脚本实际上是包装类的主要功能。 The one that brings all the pre-imported Jenkins classes. 它带来了所有预先导入的詹金斯类。 That's why the main you're defining isn't compiled into Groovy run method as it is done when you're executing the script from computer command line. 这就是为什么您要定义的main未编译为Groovy run方法的原因,因为您是从计算机命令行执行脚本时完成的。

If you want your main to be executed just put it outside of the class definition like this: 如果要执行您的main,只需将其放在类定义之外,如下所示:

class Product {
...
}

 def p1 = new Product("Mobile", "10000", "Nokia")
 println(p1.toString())
 println "Hello"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM