I'm trying to break up code that makes use of the Jenkins Job DSL plugin into reusable pieces, and I suspect that my question is generic to Groovy and not Jenkins-specific. For example, I want to reuse parts of this block:
freeStyleJob() {
//generic stuff
name "something"
description "something else"
//custom stuff
scm {
svn {
//etc....
}
}
}
By placing name and description in a utility method (obviously I want to do more than just that in real life). However, I cannot find the proper syntax to create a closure for the current scope. Here is how I think it should look:
def jobCommonItems() {
return {
//generic stuff
name "something"
description "something else"
}
}
freeStyleJob() {
jobCommonItems().call()
//custom stuff
scm {
svn {
//etc....
}
}
}
(Perhaps with a closure.delegate = this somewhere)
However, that's not working for closures. It is working for methods, as illustrated here: https://dzone.com/articles/groovy-closures-owner-delegate
To illustrate, here is a test that shows three combinations of possible syntax:
String myString = "Top Level: string"
def myMethod() {
println "Top Level: Method"
}
def myClosure = { println "Top Level: Class"}
class MyClass1 {
String myString = "Class1: String"
def myMethod() {
println "Class1: Method"
}
def myClosure = { println "Class1: Closure"}
}
class MyClass2 {
String myString = "Class2: String"
def myMethod() {
println "Class2: Method"
}
def myClosure = { println "Class2: Closure"}
}
class MyClass {
def closure = {
println "In-Class generated closure begins, delegate is ${delegate}"
myMethod()
myClosure()
println myString
}
}
def closure = new MyClass().closure
closure.delegate = new MyClass1()
closure()
closure = new MyClass().closure
closure.delegate = new MyClass2()
closure()
// This fails - it can find the top level method, but not closure or string
closure.delegate = this
closure()
def methodMissing(String methodName, args) {
println "Method not found in class ${this} by name ${methodName}"
}
I get an error that the closure is not in the main class (ie test for test.groovy): Method not found in class test@60611244 by name myClosure
I've tried changing delegate to "this", I tried changing the lookup strategy, etc. I'm probably missing something fundamental.
Seems that one solution is to invert the relationship like this, and to pass "this" as the context to find DSL top-level closures.
How about something like this?
The job factory methods like
freeStyleJob
return an object that can be used to apply more configuration using thewith
method. That method expects a closure argument which has the same properties as the closure passed to thefreeStyleJob
method.The script itself is an instance of
DslFactory
which is the interface containing e.g. thefreeStyleJob
method. You can pass that object to classes or methods to use thefreeStyleJob
.And then you can use the
myJob
object to apply further configuration usingwith
.