简体   繁体   中英

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

If you want your main to be executed just put it outside of the class definition like this:

class Product {
...
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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