Is there a groovy equivalent to the beanshell source () method?

I looked through the groovy doc and found no analogue, but things are a bit messy there. I am switching from beanshell to groovy and used the source ("fileloc") method in beanshell to inline-include other beanshell utility scripts for reuse. Is there a standard function for this in groovy or best practice?

+1


source to share


2 answers


You can collect all parts of your scripts into a String, and then the GroovyShell object evaluates your script. I picked this from the DSL examples from Venkat Subramanium.



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)

      

+5


source


The reason you don't find this is because Groovy is compiled. Your Groovy code is compiled to Java bytecode, which the JVM runs right next to any Java code in your application. This is why things like writing Groovified unit tests for large bodies of Java code take extra effort.

BeanShell is an interpreted Java-like language, so breaking in other resulting code at runtime doesn't really matter.



So you might be interested in groovysh and his team load

.

+1


source







All Articles