问题
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