Need Groovy syntax help for generating a Closure from a String

后端 未结 4 725
醉梦人生
醉梦人生 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:27

    Consider the example below. The key is to specify, explicitly, a closure without parameters.

    def build = { def jobName ->
        println "executing ${jobName}"
    }
    
    // we initialize the shell to complete the example
    def sh = new GroovyShell()
    sh.setVariable("build", build)
    
    // note "->" to specify the closure
    def cl = sh.evaluate(' { -> build("my job") }')
    
    println cl.class
    cl.call()
    

提交回复
热议问题