I have done quite a bit of digging to find the best way to reuse groovy scripts on the BPMN. Both on the forum as well as in the Camunda documentation. As well as experimentation with Camunda.
This seems like a simple problem but it has been surprisingly difficult to solve.
As an example use-case, I would like to execute the same groovy script on a service task that I execute on a Signal Event start task, to recalculate some business logic. Or I may want that exact same groovy script business logic re-calculation to occur on two different service tasks on two different areas of the BPMN.
I have found one solution based on Scripting | docs.camunda.org is to set a output parameter sourceCode with type Text and put a normal multi-line groovy script as the text. This can then be referenced on another task as ${sourceCode}. While this works, it has a 4,000 character limit due to database column constraint and one of the scripts we use is about 4,200 characters long.
I’ve heard of an approach using spin from How to handle large process variables, but have not been able to get an approach like this to work with a normal multi-line groovy script. The premise attempted was to store the script as a script of:
execution.setVariable("var", S('{"sourceCode":"full
mult-line
script
here"}'))
Then later reference this as:
execution.getVariable("var").prop('sourceCode')
This gets around the 4,000 character limit, but seems not to work due to natural line breaks in a normal groovy script and increasingly feels like a hack.
I am currently using the approach where a groovy script is used as an External Resource using classpath:// and using .groovy extension files that are stored in src/main/resources and deployed with the jar file. This works and is the best solution I have found so far, but comes with a concern that has both a pro and a con: the concern being that multiple process definitions can be impacted by the external groovy script file change. This could be a good thing if a small bug fix were made in the groovy script. But in the case that major new non backward-compatible script changes were needed, this would risk impacting previously deployed process definitions. In our prod environment, we have potentially long-running user tasks, so we can conceivably have a case where some old process definitions still have running instances. We have not had the need yet to migrate these.
We are using Camunda 7 in a SpringBoot configuration and are currently deploying BPMNs through the standard built in Camunda annotation @EnableProcessApplication with no special processes.xml configuration.
I have seen mention of deploying script with BPMN with either REST API or Java code, but it sounds like that would require us to refactor our deployment process and there is no documentation I can find on this. I’m assuming this means the script would stay with the deployment and be referenced with the deployment:// prefix.
Surely I am missing something basic and there is a simple way to reuse groovy script code in Camunda?