title: Closures in JenkinsFile groovy - callbacks or delegate
body: <p>I want to be able to add a callback as a parameter in a Jenkins Groovy script. I think a closure is what I need, but I don't know how to do it. Here is the output I want:</p>
  
  <pre><code>enter
  hello
  exit
  </code></pre>
  
  <p><strong>JenkinsFile:</strong></p>
  
  <pre><code>def rootDir = pwd()
  def tools = load "${rootDir}\\patchBuildTools.groovy"
  mainMethod(tools.testCl("hello"))
  </code></pre>
  
  <p><strong>patchBuildTools.groovy</strong></p>
  
  <pre><code>def mainMethod(Closure test) {
      println "enter"
      test()
      println "exit"
  }
  
  
  def testCl(String message) {
      println message
  }
  </code></pre>
  
  <p>This gives me an out put of :</p>
  
  <pre><code>hello
  enter
  java.lang.NullPointerException: Cannot invoke method call() on null object
  </code></pre>
  
  <p>Is it possible to get the call order I want?</p>
  
  <p><strong>Update - based on the answer</strong></p>
  
  <p><strong>JenkinsFile:</strong></p>
  
  <pre><code>def rootDir = pwd()
  def tools = load "${rootDir}\\patchBuildTools.groovy"
  mainMethod("enter", "exit")
  {
    this.testCl("hello")
  }
  </code></pre>
  
  <p><strong>patchBuildTools.groovy</strong></p>
  
  <pre><code>def mainMethod(String msg1, String ms2, Closure test) {
    println msg1
    test()
    println ms2
  }
  
  
  
  def testCl(String message) {
      println message
  }
  </code></pre>