Search code examples
groovybeanshell

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


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?


Solution

  • 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)