I had a very similar problem recently and came up with a solution (see below). It's maybe not the simplest one. Putting code into real classes might be better, but then you have to compile the classes before you can start the DSL script. My solution is purely script based.
In the script that gets directly called by the DSL plugin (and which hence has access to the DSL API and to the workspace where all the scripts are checked out), I define a method 'loadScript'. This method reads in the specified file and evaluates it. But before calling 'evaluate', it creates a binding and puts some object there. Among others, the reference to the DSL API and the reference to the 'loadScript' method. Then, when the script being evaluated is performed, it magically can use the DSL API and the loadScript method.
It's important that the last statement in the loaded script is 'return this'.
Then you can write e.g. the following:
rootScript.groovy:
def firstPipelineCreator = loadScript("firstPipelineCreator")
firstPipelineCreator.createPipeline()
The magics is in the script which is directly referenced in the 'Process DSL' step:
rootDir = "" + SEED_JOB.workspace
jobDsl = this
def loadScript(String scriptName) {
// Create the binding and put there varaibles/methods that will be available
// in every script that has been loaded via loadScript
scriptBindings = new Binding(this.binding.variables)
scriptBindings.setVariable("jobDsl", jobDsl)
scriptBindings.setVariable("rootDir", rootDir)
scriptBindings.setVariable("loadScript", this.&loadScript)
scriptBindings.setVariable("logInfo", this.&logInfo)
scriptBindings.setVariable("logDebug", this.&logDebug)
shell = new GroovyShell(scriptBindings)
logDebug "Loading script '" + scriptName + ".groovy" + "'"
script = shell.parse(new File(rootDir, scriptName + ".groovy"))
return script.run() // The script should have 'return this' as the last statement
}
def logInfo(text) {
println "[INFO ] " + text
}
def logDebug(text) {
println "[DEBUG] " + text
}