我有一个groovy脚本,它需要在外部groovy脚本的类中运行方法。我知道如何在外部groovy脚本中运行方法:
new GroovyShell().parse( new File( 'foo.groovy' ) ).with {
method()
}但是,如果该方法在一个类中呢?我试过了,但它给了我一个错误。
new GroovyShell().parse( new File( 'foo.groovy' ) ).with {
theclass.method()
}发布于 2014-06-07 15:51:21
您可以使用Java反射来创建位于另一个脚本中的Class的新实例:
File sourceFile = new File("D:\\anoutherScript.groovy")
//here you have to update your classloader with external script
getClass().getClassLoader().addURL(sourceFile.toURI().toURL())
GroovyObject obj = Class.forName("ClassInAnotherObject").newInstance()
obj.doSth()外部文件中的脚本将如下所示:
class ClassInAnotherObject{
def doSth(){
}
}但是在脚本文件中可以有更多的类,也可以有更多的指令和方法调用。就像普通的groovy脚本一样。
https://stackoverflow.com/questions/24090660
复制相似问题