groovy

publicclassgroovyTools{

privateUseruser;

publicUsergetUser(){

returnuser;

}

publicvoidsetUser(Useruser){

this.user=user;

}

privateStringfilename="script.txt";

publicbooleanparse(){

Filef=newFile("script.txt");

booleanss=false;

GroovyShellshell=newGroovyShell();

Scripts=null;

try{

//解析groovy脚本

s=shell.parse(f);

//给脚本中调用的java对象设置对应值

s.setProperty("user",user);

ss=(Boolean)s.run();

}catch(CompilationFailedExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}catch(IOExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}

returnss;

}

publicstaticvoidmain(String[]arg){

groovyToolstool=newgroovyTools();

tool.parse();

}

}

groovy脚本:

println"我进来了"

defname=user.getName();

println"nimaa"+name;

returntrue;

相关推荐