Cannot compile Groovy files: no Groovy library is defined for module
- 模块需要引入插件和依赖 . 如下
${project.basedir} 是pom中的常量 , 项目的路径
/src/main/java/cn/com/baidu/message/groovy 是我的groovy文件所在的包路径
<dependencies>
<dependency>
<artifactId>groovy-all</artifactId>
<groupId>org.codehaus.groovy</groupId>
<version>3.0.9</version>
<type>pom</type>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>1.6.1</version>
<configuration>
<sourceEncoding>UTF-8</sourceEncoding>
<sources>
<source>
<directory>${project.basedir}/src/main/java/cn/com/baidu/message/groovy</directory>
<includes>
<include>**/*.groovy</include>
</includes>
</source>
</sources>
<testSources>
<testSource>
<directory>${project.basedir}/src/main/test/cn/com/baidu/message/groovy</directory>
<includes>
<include>**/*.groovy</include>
</includes>
</testSource>
</testSources>
<!-- 用maven编译时需要加encoding:mvn -Dfile.encoding=UTF-8 compile -->
</configuration>
<executions>
<execution>
<goals>
<goal>addSources</goal>
<goal>addTestSources</goal>
<goal>compile</goal>
<goal>compileTests</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
- Maven的Plugins中会出现gplus的插件 , 点击compile先编译groovy文件 , 编译后在target查看是否编译成功 , 如果没有出现编译后的文件 , 那肯定是第一步的路径没有配置对
3 . 然后点击模块的compile再进行编译 , 注意:每次模块clear后 , 都需要先执行第二步再编译模块本身
如果没有compileGroovy或者没有compile模块 , 则会出现
Cannot compile Groovy files: no Groovy library is defined for module 模块名 的错误.
下面是我写的groovy工具类 , 用于执行页面传过来的JavaScript函数
package cn.com.baidu.groovy
import com.alibaba.fastjson.JSONObject
import jdk.nashorn.api.scripting.ScriptObjectMirror
import javax.script.*
/**
* Groovy动态条件封装请求参数
*/
class APIStartJavaScript{
// 查找并创建指定脚本引擎
private ScriptEngine engine = new ScriptEngineManager().getEngineByName("javascript");
Object executeScriptFunction(Object params, String script){
// 初始化Bindings
Compilable compilable = (Compilable) engine;
Bindings bindings = engine.createBindings(); //Local级别的Binding
// 绑定参数
bindings.put("request", params);
try {
// 解析编译脚本函数
CompiledScript JSFunction = compilable.compile(script);
// 获取执行结果
Object result = JSFunction.eval(bindings);
if(result == null) {
JSONObject jsonObject = new JSONObject();
return jsonObject;
}
if (result instanceof ScriptObjectMirror) {
// 遍历所有值
ScriptObjectMirror scriptObjec = (ScriptObjectMirror) result;
// 递归处理查询结果的数组
recursionEencapsulation(scriptObjec);
return scriptObjec;
}else {
return result;
}
} catch (Exception e) {
if (e instanceof javax.script.ScriptException || e instanceof NoSuchMethodException) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("scriptException", e.getMessage());
return jsonObject;
}else{
throw e;
}
}
return null;
}
// 递归封装数组
public void recursionEencapsulation(ScriptObjectMirror scriptObject) {
for(String key : scriptObject.keySet()){
Object mirror = scriptObject.get(key);
if (mirror instanceof ScriptObjectMirror) {
recursionEencapsulation(mirror);
Boolean isArray = isArray(mirror);
if(isArray) {
List<Object> list = parseArray(mirror);
scriptObject.put(key, list);
}
}
}
}
// 判断是否为数组
public Boolean isArray(ScriptObjectMirror scriptObject) {
boolean isArray = true;
int index = 0;
for(String key : scriptObject.keySet()){
if(!key.equals(index+"")) {
isArray = false;
}
index++;
}
return isArray;
}
// 将数组转为ArrayList
public List<Object> parseArray(ScriptObjectMirror scriptObject) {
List<Object> arrayList = new ArrayList<Object>();
for(String key : scriptObject.keySet()){
Object obj = scriptObject.get(key);
arrayList.add(obj);
}
return arrayList;
}
}
版权归原作者 会飞的哈士奇 所有, 如有侵权,请联系我们删除。