title: Activiti: use activiti:expression to define reusable ScriptTaskListener script
body: <p>I want to define script sourcecode somewhere and reuse it from multiple ScriptTaskListeners in Activiti 5.12 or later.</p>
  
  <p>Assuming I have defined a processVariable <code>taskScript</code> in a previous groovy script task which contains groovy source code:</p>
  
  <pre><code>execution.setVariable("taskScript", 
      'def log = org.slf4j.LoggerFactory.getLogger("my.Logger");log.info("works")')
  </code></pre>
  
  <p>Now I want to let an <code>org.activiti.engine.impl.bpmn.listener.ScriptTaskListener</code> use that code by passing it as an <code>activiti:expression</code>:</p>
  
  <pre><code>&lt;activiti:taskListener event="complete" class="org.activiti.engine.impl.bpmn.listener.ScriptTaskListener" &gt;
    &lt;activiti:field name="script"&gt;
       &lt;activiti:expression&gt;&lt;![CDATA[${taskScript}]]&gt;&lt;/activiti:expression&gt;
    &lt;/activiti:field&gt;
    &lt;activiti:field name="language" stringValue="groovy" /&gt;
  &lt;/activiti:taskListener&gt;
  </code></pre>
  
  <p>I get the Exception below:</p>
  
  <pre><code>javax.script.ScriptException: groovy.lang.MissingMethodException: 
  No signature of method: org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.$() 
  is applicable for argument types: (Script657$_run_closure1)
  </code></pre>
  
  <p>It appears that the expression <code>${taskScript}</code> does not get evaluated before Activiti passes it to the ScriptTaskListener <code>script</code> property, rather the groovy engine tries to evaluate <code>${taskScript}</code> as a groovy script.</p>
  
  <p>This however works fine:</p>
  
  <pre><code>&lt;activiti:taskListener event="complete" class="org.activiti.engine.impl.bpmn.listener.ScriptTaskListener" &gt;
    &lt;activiti:field name="script"&gt;
      &lt;activiti:string&gt;
        def log = org.slf4j.LoggerFactory.getLogger("my.Logger");
        log.info("works")
      &lt;/activiti:string&gt;
    &lt;/activiti:field&gt;
    &lt;activiti:field name="language" stringValue="groovy" /&gt;
  &lt;/activiti:taskListener&gt;
  </code></pre>
  
  <p>I am quite confused why I cannot do this. Shouldn't <code>activiti:expression</code> always be evaluated before it gets applied to an <code>activiti:field</code>?</p>
  
  <p>Is there a different way to reuse script code in ScriptTaskListeners?</p>