views:

213

answers:

2

I've scoured the groovy doc and haven't found an analogue, but things there are organized a bit haphazardly. I'm switching from beanshell to groovy and was using the source("fileloc") method in beanshell to inline-include other, utility beanshell scripts for reuse. Is there a standard function to do this in groovy or a best practice?

+1  A: 

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.

feoh
+4  A: 

You can assemble all the parts of your scripts into a String, then have a GroovyShell object evaluate your script. I picked this up from Venkat Subramanium's DSL examples.

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)
John Flinchbaugh