Groovy是基于jvm的脚本语言。
maven使用加上依赖
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy</artifactId>
</dependency>
随便建个脚本文件
println 'I am ' + name;
println 'My age is ' + age;
System.out.println(dd);
然后跑main方法
public static void main(String[] args) throws Exception {
Binding binding = new Binding();
GroovyShell shell = new GroovyShell(binding);
binding.setVariable("name", "bo");
binding.setVariable("age", "23");
binding.setVariable("dd", 11);
//1. 执行包含脚本的文件
File file = new File("D:\\testGro.sh");
shell.evaluate(file);
//2. 执行脚本
binding.setVariable("strs", new String[]{"A", "B", "C"});
String join = (String) shell.evaluate("def call(){return strs.join('-')}; call()");
System.out.println(join);
//3. 执行java代码
shell.evaluate("String str = \"one str\";\n" +
" System.out.println(str);");
shell = null;
binding = null;
}
执行完
I am bo
My age is 23
11
A-B-C
one str
一些业务流程使用脚本,有奇效。