Camunda:在外部Groovy脚本中使用提供的类

时间:2018-08-08 14:45:23

标签: groovy camunda

我想用于编写外部Groovy脚本。

为了不复制大量代码,我想共享类。

我有:

- external_test.groovy
- Input.groovy

在Intellij中运行external_test.groovy

输入是一个简单的类:

package helpers

class Input {
    String serviceConfig
    String httpMethod
    String path
    LinkedHashMap headers = [:]
    String payload
    Boolean hasResponseJson
}

Camunda执行脚本时,找不到该类:

import helpers.Input
...

并引发异常:

unable to resolve class helpers.Input @ line 16, column 9. new helpers.Input(serviceConfig: "camundaService", ^ 1 error

它在“部署”中列出:

deployment listing

我想念什么吗?还是不支持?

1 个答案:

答案 0 :(得分:0)

我在Camunda论坛上找到了一条帖子,可以帮助我解决这个问题:

https://forum.camunda.org/t/groovy-files-cant-invoke-methods-in-other-groovy-files-which-are-part-of-same-deployment/7750/5

这是解决方案(并不能真正令人满意-因为它需要大量样板代码):

static def getScript(fileName, execution) {
    def processDefinitionId = execution.getProcessDefinitionId()
    def deploymentId = execution.getProcessEngineServices().getRepositoryService().getProcessDefinition(processDefinitionId).getDeploymentId()
    def resource = execution.getProcessEngineServices().getRepositoryService().getResourceAsStream(deploymentId, fileName)

    def scannerResource = new Scanner(resource, 'UTF-8')

    def resourceAsString = scannerResource.useDelimiter('\\Z').next()
    scannerResource.close()

    GroovyShell shell = new GroovyShell()
    return shell.parse(resourceAsString)
}

def helper = getScript("helpers/helper_classes.groovy", execution)
helper.myFunction("hello")