文件1.groovy
def method() {
println "test"
}
文件2.groovy
method()
我想在运行时加载/包含 File1.groovy 中的函数/方法,相当于 rubys/rake 的加载。它们位于两个不同的目录中。
如果您不介意 file2 中的代码位于with
块,你可以这样做:
new GroovyShell().parse( new File( 'file1.groovy' ) ).with {
method()
}
另一种可能的方法是改变file1.groovy
to:
class File1 {
def method() {
println "test"
}
}
然后在file2.groovy
您可以使用mixin
添加方法file1
def script = new GroovyScriptEngine( '.' ).with {
loadScriptByName( 'file1.groovy' )
}
this.metaClass.mixin script
method()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)