title: java.io.NotSerializableException: groovy.text.StreamingTemplateEngine
body: <p>I created pipeline job in jenkins that first, creating Template variable with StreamingTemplateEngine object and using it, and secondly, calling sh step.</p>
  
  <p>This is the code:</p>
  
  <pre><code>node('node') {
  
      stage ("Creating Template and run sh command") {
          def text = 'Dear "$firstname $lastname",\nSo nice to meet you in &lt;% print city %&gt;.\nSee you in ${month},\n${signed}'
          def binding = ["firstname":"Sam", "lastname":"Pullara", "city":"San Francisco", "month":"December", "signed":"Groovy-Dev"]
          def engine = new groovy.text.StreamingTemplateEngine()
          def template = engine.createTemplate(text).make(binding)
          def result = 'Dear "Sam Pullara",\nSo nice to meet you in San Francisco.\nSee you in December,\nGroovy-Dev'
          print result
          sh ("echo Hello World")
      }
  }
  </code></pre>
  
  <p>When I run the pipeline I get the following error:</p>
  
  <pre><code>an exception which occurred:
      in field groovy.lang.Closure.delegate
      in object groovy.tmp.templates.StreamingTemplateScript49$_getTemplate_closure1@27101ad4
      in field groovy.lang.Closure.delegate
      in object groovy.lang.Closure$WritableClosure@27101ad4
      in field groovy.lang.Closure.delegate
      in object org.codehaus.groovy.runtime.CurriedClosure@69da9c73
      in field groovy.lang.Closure.delegate
      in object groovy.lang.Closure$WritableClosure@69da9c73
      in field groovy.lang.Closure.delegate
      in object org.codehaus.groovy.runtime.CurriedClosure@3ffe8420
      in field groovy.lang.Closure.delegate
      in object groovy.lang.Closure$WritableClosure@3ffe8420
      in field com.cloudbees.groovy.cps.impl.BlockScopeEnv.locals
      in object com.cloudbees.groovy.cps.impl.BlockScopeEnv@557711d2
      in field com.cloudbees.groovy.cps.impl.CallEnv.caller
      in object com.cloudbees.groovy.cps.impl.FunctionCallEnv@42d0d4ba
      in field com.cloudbees.groovy.cps.Continuable.e
      in object org.jenkinsci.plugins.workflow.cps.SandboxContinuable@293a571b
      in field org.jenkinsci.plugins.workflow.cps.CpsThread.program
      in object org.jenkinsci.plugins.workflow.cps.CpsThread@37f6a1b8
      in field org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.threads
      in object org.jenkinsci.plugins.workflow.cps.CpsThreadGroup@3715a80
      in object org.jenkinsci.plugins.workflow.cps.CpsThreadGroup@3715a80
  **Caused: java.io.NotSerializableException: groovy.tmp.templates.StreamingTemplateScript49**
      at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:860)
      at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032)
      at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988)
      at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:967)
      at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854)
      at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032)
      at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988)
      at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:967)
      at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854)
      at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032)
      at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988)
      at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:967)
      at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854)
      at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032)
      at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988)
      at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:967)
      at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854)
      at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032)
      at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988)
      at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:967)
      at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854)
      at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032)
      at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988)
      at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:967)
      at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854)
      at org.jboss.marshalling.river.BlockMarshaller.doWriteObject(BlockMarshaller.java:65)
      at org.jboss.marshalling.river.BlockMarshaller.writeObject(BlockMarshaller.java:56)
      at org.jboss.marshalling.MarshallerObjectOutputStream.writeObjectOverride(MarshallerObjectOutputStream.java:50)
      at org.jboss.marshalling.river.RiverObjectOutputStream.writeObjectOverride(RiverObjectOutputStream.java:179)
      at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:344)
      at java.util.HashMap.internalWriteEntries(HashMap.java:1790)
      at java.util.HashMap.writeObject(HashMap.java:1363)
      at sun.reflect.GeneratedMethodAccessor87.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
      at org.jboss.marshalling.reflect.SerializableClass.callWriteObject(SerializableClass.java:273)
      at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:976)
      at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854)
      at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032)
      at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988)
      at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854)
      at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032)
      at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988)
      at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:967)
      at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854)
      at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032)
      at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988)
      at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:967)
      at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854)
      at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032)
      at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988)
      at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854)
      at org.jboss.marshalling.river.BlockMarshaller.doWriteObject(BlockMarshaller.java:65)
      at org.jboss.marshalling.river.BlockMarshaller.writeObject(BlockMarshaller.java:56)
      at org.jboss.marshalling.MarshallerObjectOutputStream.writeObjectOverride(MarshallerObjectOutputStream.java:50)
      at org.jboss.marshalling.river.RiverObjectOutputStream.writeObjectOverride(RiverObjectOutputStream.java:179)
      at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:344)
      at java.util.TreeMap.writeObject(TreeMap.java:2438)
      at sun.reflect.GeneratedMethodAccessor167.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
      at org.jboss.marshalling.reflect.SerializableClass.callWriteObject(SerializableClass.java:273)
      at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:976)
      at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854)
      at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032)
      at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988)
      at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854)
      at org.jboss.marshalling.AbstractObjectOutput.writeObject(AbstractObjectOutput.java:58)
      at org.jboss.marshalling.AbstractMarshaller.writeObject(AbstractMarshaller.java:111)
      at org.jenkinsci.plugins.workflow.support.pickles.serialization.RiverWriter.writeObject(RiverWriter.java:140)
      at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.saveProgram(CpsThreadGroup.java:467)
      at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.saveProgram(CpsThreadGroup.java:443)
      at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.saveProgramIfPossible(CpsThreadGroup.java:430)
      at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:367)
      at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$200(CpsThreadGroup.java:82)
      at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:243)
      at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:231)
      at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:64)
      at java.util.concurrent.FutureTask.run(FutureTask.java:266)
      at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:112)
      at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
      at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
      at java.util.concurrent.FutureTask.run(FutureTask.java:266)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
      at java.lang.Thread.run(Thread.java:748)
  </code></pre>
  
  <p>I looked at other answers on questions about Not Serializable problems that suggested to put the Not Serializable objects inside function and annotate the function with @NonCPS, but I still get the same error.</p>
  
  <p>This is the code with @NonCPS annotation:</p>
  
  <pre><code>node('node') {
  
      stage ("Creating Template") {
          def text = 'Dear "$firstname $lastname",\nSo nice to meet you in &lt;% print city %&gt;.\nSee you in ${month},\n${signed}'
          def binding = ["firstname":"Sam", "lastname":"Pullara", "city":"San Francisco", "month":"December", "signed":"Groovy-Dev"]
          def engine = MakeTemplate()
          def template = engine.createTemplate(text).make(binding)
          def result = 'Dear "Sam Pullara",\nSo nice to meet you in San Francisco.\nSee you in December,\nGroovy-Dev'
          print result
      }
  
      stage ("Run sh command") {
          sh ("echo Hello World")
      }
  }
  
  @NonCPS
  def MakeTemplate() {
      def Template = new groovy.text.StreamingTemplateEngine()
      return Template
  }
  </code></pre>
  
  <p>When I separate the Template variable and the sh step to two stages, the job runs without any problem.</p>
  
  <p>This is the code when I separate the Template variable and the sh step to two different stages:</p>
  
  <pre><code>node('node') {
  
      stage ("Creating Template") {
          def text = 'Dear "$firstname $lastname",\nSo nice to meet you in &lt;% print city %&gt;.\nSee you in ${month},\n${signed}'
          def binding = ["firstname":"Sam", "lastname":"Pullara", "city":"San Francisco", "month":"December", "signed":"Groovy-Dev"]
          def engine = new groovy.text.StreamingTemplateEngine()
          def template = engine.createTemplate(text).make(binding)
          def result = 'Dear "Sam Pullara",\nSo nice to meet you in San Francisco.\nSee you in December,\nGroovy-Dev'
          print result
      }
  
      stage ("Run sh command") {
          sh ("echo Hello World")
      }
  }
  </code></pre>
  
  <p>Someone has any suggestions what I can do?
  thanks!</p>