Using a groovy class in other jenkins groovy scripts -
i have 4 groovy scripts (2 dsl.groovy scripts):
jobconfig.groovy:
class jobconfig { final name jobconfig(map) { name = map['name'] } }
toplevel.groovy:
import jobconfig.* def dosmthwithjobconfig(final jobconfig config) { println(config.name); }
sublevel1.dsl.groovy:
groovyshell shell = new groovyshell() def toplevelscript = shell.parse(new file("toplevel.groovy")) def jobconfigs = [ new jobconfig(name: 'jenkinstestdsls'), new jobconfig(name: 'jenkinstestdsls2') ] jobconfigs.each { toplevelscript.dosmthwithjobconfig(it); }
sublevel2.dsl.groovy:
groovyshell shell = new groovyshell() def toplevelscript = shell.parse(new file("toplevel.groovy")) def jobconfigs = [ new jobconfig(name: 'jenkinstestdsls3'), new jobconfig(name: 'jenkinstestdsls4') ] jobconfigs.each { toplevelscript.dosmthwithjobconfig(it); }
now if locally do:
groovyc jobconfig.groovy
,i no issues running scripts locally.
but on jenkins if provide jobconfig.class @ same place these scripts are, can't running. read here don't need compiling long jobconfig.groovy on classpath. how do jenkins? or there solution?
if don't want compile groovy class(es) , add compiled classes classpath, can make use of classes within groovy scripts this:
given groovy class
class groovyclass { def groovyclass(someparameter) {} def amethod() {} }
you can use class in groovy script this
import hudson.model.* import java.io.file; import jenkins.model.jenkins; def jenkinsrootdir = build.getenvvars()["jenkins_home"]; def parent = getclass().getclassloader() def loader = new groovyclassloader(parent) def someparametervalue = "abc" def groovyclass = loader.parseclass(new file(jenkinsrootdir + "/usercontent/groovyscripts/groovyclass.groovy")).newinstance(someparametervalue) groovyclass.amethod()
Comments
Post a Comment