Can a Groovy source file in Jenkins' Shared Library access a method from a global variable?
my Shared Library structure is:
(root)
+- src # Groovy source files
| +- org
| +- foo
| +- CallMethodFromVars.groovy # for org.foo.CallMethodFromGlobalVars.groovy class
+- vars
| +- foo.groovy # for global 'foo' variable
In my class src.org.foo.CallMethodFromVars
I would like to access a method call(arg)
in vars.foo.groovy
src.org.foo.CallMethodFromVars:
class CallMethodFromVars {
String run(){
//TODO call method vars.foo.groovy.call() here
}
}
vars.foo.groovy:
def call(arg) {
doSomething()
}
My research and attempts at solving this so far have failed. Some of the things I tried:
import var.*
;foo.groovy.call(arg)
def method = this.class.classLoader.loadClass('vars.foo'); method.call(arg)
def engine = new GroovyScriptEngine("/path-to-library/vars/foo.groovy"); engine.run('call', [arg])
def method = library.vars.foo; method.call(arg)
def method libraryContext.getVariable("foo"); method.call(arg)
method = System.getenv('foo'); method.call(arg)
I tried all the above with
method()
instead ofmethod.call(arg)