0

I have one common groovy file that contains few const variables and functions... and I also have more groovy files with pipelineJob that use the variables and functions from the common file what is the best way to import all the data from the common file to the other files?

1

1 Answer 1

1

I have not tested this with Jenkins, but if Jenkins executes the Groovy script as if by invoking groovy -cp .... myScript.groovy it should work:

utils.groovy:

// notice there's no "def", otherwise the def would be local only
name = 'Joe'

class MyUtils {
    static String greeting(String name) {
        "Hello $name"
    }
}

src/main.groovy

def shell = new GroovyShell(getBinding())
shell.evaluate(new File('utils.groovy'))

println MyUtils.greeting(name)

Running it:

$ groovy src/Main.groovy
Hello Joe

Because the Script base class by default also has an evaluate method, your can actually just call that instead of using a GroovyShell and the result should be identical:

src/main.groovy

evaluate(new File('utils.groovy'))

println MyUtils.greeting(name)

If it doesn't work it's because the Script base class has been changed , probably... the first approach should work in all cases.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.