本文介绍了从 groovy 脚本加载脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
File1.groovy
File1.groovy
def method() {
println "test"
}
File2.groovy
File2.groovy
method()
我想在运行时从 File1.groovy 加载/包含函数/方法,等于 rubys/rake 的加载.它们位于两个不同的目录中.
I want to load/include the functions/methods from File1.groovy during runtime, equals to rubys/rake's load. They are in two different directories.
推荐答案
如果你不介意 file2 中的代码在 with
块中,你可以这样做:
If you don't mind the code in file2 being in a with
block, you can do:
new GroovyShell().parse( new File( 'file1.groovy' ) ).with {
method()
}
另一种可能的方法是将 file1.groovy
更改为:
class File1 {
def method() {
println "test"
}
}
然后在 file2.groovy
中你可以使用 mixin
添加来自 file1
And then in file2.groovy
you can use mixin
to add the methods from file1
def script = new GroovyScriptEngine( '.' ).with {
loadScriptByName( 'file1.groovy' )
}
this.metaClass.mixin script
method()
这篇关于从 groovy 脚本加载脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!