0

From this answer, I learnt that, every Groovy script compiles to a class that extends groovy.lang.Script class

Below is a test groovy script written for Jenkins pipeline in Jenkins editor.

node('worker_node'){
    print "***1. DRY principle***"

    def list1 = [1,2,3,4]
    def list2 = [10,20,30,40]

    def factor = 2

    def applyFactor =  {e -> e * factor}

    print(list1.each(applyFactor))
    print(list2.each(applyFactor))



    print "***2. Higher order function***"

    def foo = { value, f ->  f(value *2) }

    foo(3, {print "Value is $it"})

    foo(3){
        print "Value is $it"
    }
}

How to compile this groovy script to see the class generated(source code)?

overexchange
  • 15,768
  • 30
  • 152
  • 347

2 Answers2

1

The class generated is bytecode, not source code. The source code is the Groovy script.

If you want to see something similar to what the equivalent Java source code would look like, use groovyc to compile the script as usual, and then use a Java decompiler to produce Java source (this question's answers lists a few).

That's subject to the usual caveats on decompiled code, of course. High-level information is lost in the process of compiling. Decompilers have to guess a bit to figure out the best way to represent what might have been in the original source. For instance, what was a for loop in the original code may end up being decompiled as a while loop instead.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
1

groovy in jenkins pipeline is a Domain Specific Language. It's not a plain groovy.

However if you remove node(){ } then it seems to be groovy in your case.

and you can run it in groovyconsole or compile to class with groovyc

just download a stable groovy binary and extract it.

if you have java7 or java8 on your computer - you can run groovyconsole and try your code there.

with Ctrl+T you can see the actual class code generated for your script.

daggett
  • 26,404
  • 3
  • 40
  • 56