You can just pass the arguments You need after the script invocation:
$ groovy groovyAuthDefault.groovy user pass
In the script all the parameters are accessible via args
variable. More info.
Is that what You were looking for?
UPDATE
Found solution but it has some limitations, maybe it's possible to bypass them but don't know exactly how.
As I wrote above when You invoke script from command line You can pass arguments that are kept in args
list. The problem lies in the fact that GroovyScriptEngine
doesn't invoke the external script with it's main method - there's no args
list so it fails with an MissingPropertyException
. The idea is to set fake args
.
java:
public static void main(String[] args) {
GroovyScriptEngine gse = new GroovyScriptEngine(new String[] {"/home/user/tmp"});
Binding varSet = new Binding();
varSet.setVariable("testVar", "Hello World");
varSet.setVariable("args", null); //null, empty string, whatever evaluates to false in groovy
gse.run("printHello.groovy", varSet);
}
printHello.groovy:
if(args) {
setBinding(new Binding(Eval.me(args[0])))
}
println("${testVar} !!!")
In printHello.groovy args
is checked. If it evaluates to true it means that script was invoked from command line with arguments and a new Binding
is set - evaluated from first element of arguments passed (plain groovy script extends groovy.lang.Script
. If args
evaluates to false it means that script was run with GroovyScriptEngine
.
Command line invocation:
groovy printHello.groovy [testVar:\'hi\']
Exception handling might be added with other improvements as well. Hope that helps.