title: Groovy: how to call closure in top scope from another closure
body: <p>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:</p>
  
  
  
  <pre class="lang-groovy prettyprint-override"><code>freeStyleJob() {
      //generic stuff
      name "something"
      description "something else"
  
      //custom stuff
      scm {
         svn {
             //etc....
         }
      }
  }
  </code></pre>
  
  <p>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:</p>
  
  <pre class="lang-groovy prettyprint-override"><code>def jobCommonItems() {
      return {
          //generic stuff
          name "something"
          description "something else"
      }
  }
  
  
  freeStyleJob() {
      jobCommonItems().call()
  
      //custom stuff
      scm {
         svn {
             //etc....
         }
      }
  }
  </code></pre>
  
  <p>(Perhaps with a closure.delegate = this somewhere)</p>
  
  <p>However, that's not working for closures.  It is working for methods, as illustrated here: <a href="https://dzone.com/articles/groovy-closures-owner-delegate" rel="nofollow">https://dzone.com/articles/groovy-closures-owner-delegate</a></p>
  
  <p>To illustrate, here is a test that shows three combinations of possible syntax:</p>
  
  <pre class="lang-groovy prettyprint-override"><code>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}"
  }
  </code></pre>
  
  <p>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</p>
  
  <p>I've tried changing delegate to "this", I tried changing the lookup strategy, etc.   I'm probably missing something fundamental.  </p>