- GroovyClassLoader loader = new GroovyClassLoader(getClass().getClassLoader());
- public static Object run(final String fileName) throws Exception {
- File f = new File(fileName);
- if (!f.exists()) {
- log.error("not found groovy " + f.getPath());
- return null;
- } else {
- // #debug
- //log.info("run groovy " + f.getPath());
- }
- su.compile(fileName);
- Class groovyClass = loader.parseClass(new GroovyCodeSource(f));
- GroovyObject object = (GroovyObject) groovyClass.newInstance();
- object.setProperty("xxx", "abc");//设置的自定义参数
- object.setProperty("yyy", 322);//设置的自定义参数
- return object.invokeMethod("run", null);
- }
- //该片段来自于http://www.codesnippet.cn/detail/12122012934.html
来源: http://www.codesnippet.cn/detail/12122012934.html