在我的 Maven 项目中,我目前正在将我的 Java 代码与一些 Groovy 代码混合在一起。此时我主要使用 Groovy 来构建 bean。我的一些 Java 代码直接使用 Groovy bean。
我这样配置 Maven 编译器插件:-
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<compilerId>groovy-eclipse-compiler</compilerId>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
</configuration>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-compiler</artifactId>
<version>2.8.0-01</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-batch</artifactId>
<version>2.1.5-03</version>
</dependency>
</dependencies>
</plugin>
当我使用 mvn test
运行我的测试用例时,它工作得很好。
但是,当我通过右键单击测试文件并运行它直接从 IntelliJ 运行测试用例时,我在 Groovy bean 上收到“找不到符号”错误。当我阅读错误日志时,IntelliJ 在运行测试之前使用 Java 编译器编译我的项目...因此,测试失败。
我似乎无法弄清楚如何指示 IntelliJ 始终使用 Groovy 编译器而不是 Java 编译器。
我应该在 SDK 下更改什么以便使用 Groovy 编译器?我尝试添加与 Groovy 相关的 JAR 文件,但出现了其他错误。
更新 1:根据@Seagull 的建议
我在“全局库”下添加了 groovy JAR:-
当我直接从 IntelliJ 执行测试文件时,我收到了一些 Groovy 警告,但我仍然收到相同的错误:-
谢谢。
最佳答案
我在最新版本的 Intellij ideaIC-15.0.3-custom-jdk-bundled.dmg MAC 10.10.5, JDK 上遇到了这个问题1.8.0_60。
包括后代的所有步骤...
- 我从终端安装了最新版本的
groovy
,使用 sdkman :sdk 安装 groovy 2.4.5
- 在 Intellij 中,右键单击顶级项目 > 选择“添加框架支持...”> 添加
groovy 2.4.5
(如果尚未添加)。 - 在 Intellij 中,“首选项”>“构建、执行、部署”>“编译器”>“资源模式:”> 更改顺序
!?*.java;!?*.groovy
到!?*.groovy;!?*.java
- 重新编译项目(Command+Shift+F9),现在应该可以成功编译。
关于java - 配置 IntelliJ 以使用 Groovy 编译器而不是 Java 编译器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20484034/