搭建IntelliJ环境运行Grails 2.4.2旧代码遇Groovy编译错误
针对Grails 2.4.2 + IntelliJ IDEA 2022.2.5编译失败的解决步骤
1. 匹配Groovy版本
Grails 2.4.2固定依赖Groovy 2.3.7,检查项目的Grails用户库或依赖配置,确保引入的是该版本的Groovy包,避免高版本Groovy(如2.4+)引发语法兼容问题。
2. 配置Groovy编译器
- 打开
Settings -> Build, Execution, Deployment -> Compiler -> Groovy Compiler - 将Groovy SDK指定为Grails 2.4.2自带的Groovy包(路径一般为
~/.grails/2.4.2/projects/[你的项目名]/lib/org.codehaus.groovy/groovy-all/jars/groovy-all-2.3.7.jar) - 把字节码版本设置为1.7,和Java编译器保持一致
3. 修正依赖作用域
- 将
corretto-1.8的作用域改为Compile,Grails 2.4.2编译阶段需要明确依赖JDK 8 - 检查Grails用户库,确保
grails-core、grails-web等核心包完整,没有缺失
4. 关闭增量编译
Grails 2.x与IDEA增量编译存在兼容性问题:
- 打开
Settings -> Build, Execution, Deployment -> Compiler - 取消勾选Build project automatically
- 编译时使用
Build -> Rebuild Project全量构建,不要用增量构建
5. 配置Grails运行环境
- 打开
Run/Debug Configurations,创建或修改Grails运行配置 - Grails home指定本地安装的Grails 2.4.2路径,不要依赖IDEA自动检测
- JRE选择
corretto-1.8
6. 清理缓存并同步依赖
- 执行
File -> Invalidate Caches...,选择Invalidate and Restart - 重启后通过
Grails Tools -> Refresh Dependencies重新同步项目依赖
内容的提问来源于stack exchange,提问作者Salvador Valencia
相关产品推荐
相关产品推荐

