How to load Groovy classes into the Groovy script engine of Activiti?

99封情书 提交于 2019-12-08 10:24:36

问题


I am loading a Groovy project with the following structure in Activiti :

└───src
    └───main
        └───groovy
            ├───classes
            │       Foo.groovy
            │
            └───scripts
                    script.groovy

script.groovy

package scripts

import classes.Foo

Foo.groovy

package classes

class Foo {
  Foo(){
  }
}

My problem is that the import statement of my script: import classes.Foo is not resolved by Activiti.

I am running Activiti inside a Spring container using org.activiti:activiti-spring:5.21.0. My scripts are evaluated using the groovy scripting engine in org.activiti-engine:5.21.0.

How to make Activiti aware of my Foo.groovy class ?

I have been looking for a way to specify the classpath but no luck so far.

I tried to manually resolve the import statements from the files and evaluate them but I am not satisfied with this solution.

I see that Activiti uses the scripting engine GroovyScriptEngineImpl to evaluate my script.


回答1:


I managed to do that using GroovyClassLoader#parseClass() method.

final GroovyScriptEngineImpl engine = (GroovyScriptEngineImpl) this.scriptingEngines.getEngineByName("groovy");
GroovyClassLoader classLoader = engine.getClassLoader();
Files.walk(...)
    .filter(Files::isRegularFile)
    .forEach(path -> {
        classLoader.parseClass(new GroovyCodeSource(path.toUri()));
    });
engine.setClassLoader(classLoader);


来源:https://stackoverflow.com/questions/55160173/how-to-load-groovy-classes-into-the-groovy-script-engine-of-activiti

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!