从groovy脚本加载脚本 [英] Load script from groovy script

查看:206
本文介绍了从groovy脚本加载脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

File1.groovy

  def method(){
printlntest
}






File2.groovy

  method()

我想在运行时加载/包含File1.groovy的函数/方法,等于rubys / rake的加载。它们位于两个不同的目录中。

解决方案

如果您不介意file2中的代码位于 with block,你可以这样做:

  new GroovyShell()。parse(new File(' file1.groovy')).with {
method()
}






另一个可能的方法是将 file1.groovy 更改为:

 class File1 {
def method(){
printlntest
}
}

然后在 file2.groovy 中可以使用 mixin 添加方法从 file1

  def脚本= new GroovyScriptEngine('。').with {
loadScriptByName('file1.groovy')
}
this.metaClass.mixin脚本

method()


File1.groovy

def method() {
   println "test"
}


File2.groovy

method()

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.

解决方案

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()
}


Another possible method would be to change file1.groovy to:

class File1 {
  def method() {
    println "test"
  }
}

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脚本加载脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆