Need Groovy syntax help for generating a Closure from a String

后端 未结 4 721
醉梦人生
醉梦人生 2020-12-21 18:03

I\'m trying to generate a closure from a string. The code inside the closure references a DSL function build(). The errors I\'m getting imply that Groovy is trying to exec

4条回答
  •  离开以前
    2020-12-21 18:30

    In addition to Michael Easter's answer, you could also pass the script's binding through to the GroovyShell

    def build = { ->
      "BUILD $it"
    }
    
    def shell = new GroovyShell( this.binding )
    def c = shell.evaluate( "{ -> build( 'tim_yates' ) }" )
    
    c()
    

提交回复
热议问题