是否有一个与beanshell source()方法等效的groovy?
题
我已经仔细研究了那些时髦的文档,并没有找到类似的东西,但那里的东西组织得有点随意。我正在从beanshell切换到groovy,并使用beanshell中的源(<!> quot; fileloc <!> quot;)方法内联 - 包含其他实用程序beanhell脚本以供重用。在groovy或最佳实践中是否有标准功能?
解决方案
您可以将脚本的所有部分组合成一个String,然后让GroovyShell对象评估您的脚本。我从Venkat Subramanium的DSL示例中选择了它。
part1 = new File("part1.groovy").text
part2 = new File("part2.groovy").text
script = """
println "starting execution"
${part1}
${part2}
println "done execution"
"""
new GroovyShell().evaluate(script)
其他提示
The reason you're not finding this is because Groovy is compiled. Your Groovy code gets compiled into Java bytecode that gets run by the JVM right along side any Java code in your app. This is why things like writing Groovified unit tests for large bodies of Java code requires zero extra effort.
The BeanShell is a Java-like interpreted language, so slurping in another got of code at run time is no big deal.
That said, you might be interested in groovysh and its load
command.
不隶属于 StackOverflow