Один из способов сделать это - установить параметр out
в привязке при вызове первого сценария:
Итак, с учетом сценария s1.groovy
:
//Print the letters of 'tim_yates', one per line
'tim_yates'.each this.&println
Мы можем сделать (в s2.groovy
)
// Create a StringWriter that will capture output
String output = new StringWriter().with { sw ->
// And make a Binding for our script
new Binding().with { b ->
// Set 'out' in the Binding to be our StringWriter
b[ 'out' ] = sw
// evaluate the file with the GroovyShell (using the binding)
new GroovyShell( b ).evaluate( new File( 's1.groovy' ) )
}
// And return the String captured in our writer
sw.toString()
}
println output
А затем запустите его с groovy s2.groovy
Редактировать
Я думаю, что это вариант № 1 в ответе Дейва ...