配置 IntelliJ 以使用 Groovy 编译器而不是 Java 编译器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20484034/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Configuring IntelliJ to Use Groovy Compiler Instead of Java Compiler
提问by limc
In my maven project, I'm currently mixing my Java code with some Groovy code. I'm using Groovy mostly to construct the beans at this point. Some of my Java code uses the Groovy beans directly.
在我的 maven 项目中,我目前正在将我的 Java 代码与一些 Groovy 代码混合在一起。在这一点上,我主要使用 Groovy 来构建 bean。我的一些 Java 代码直接使用 Groovy bean。
I configured the Maven Compiler Plugin like this:-
我像这样配置了 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>
When I run my testcases using mvn test
, it works just fine.
当我使用 运行我的测试用例时mvn test
,它工作得很好。
However, when I run the testcases directly from IntelliJ by right clicking the test file and run it, I'm getting "cannot find symbol" errors on the Groovy beans. When I read the error log, IntelliJ uses Java compiler to compile my project before running the test... thus, the tests fail.
但是,当我通过右键单击测试文件并运行它直接从 IntelliJ 运行测试用例时,我在 Groovy bean 上收到“找不到符号”错误。当我阅读错误日志时,IntelliJ 在运行测试之前使用 Java 编译器编译我的项目......因此,测试失败。
I can't seem to figure out how to instruct IntelliJ to always use the Groovy compiler instead of Java compiler.
我似乎无法弄清楚如何指示 IntelliJ 始终使用 Groovy 编译器而不是 Java 编译器。
What should I change under SDK so that Groovy compiler will be used? I tried adding Groovy related JAR files, but I got other errors.
我应该在 SDK 下更改什么才能使用 Groovy 编译器?我尝试添加与 Groovy 相关的 JAR 文件,但出现其他错误。
UPDATE 1: Per @Seagull suggestion
更新 1:根据 @Seagull 建议
I added groovy JARs under "Global Libraries":-
我在“全局库”下添加了 groovy JAR:-
When I executed the test file directly from IntelliJ, I'm getting some Groovy warnings and I still get the same error:-
当我直接从 IntelliJ 执行测试文件时,我收到了一些 Groovy 警告,但仍然出现相同的错误:-
Thanks.
谢谢。
采纳答案by limc
This is the reply from the IntelliJ support team on January 2, 2014 regarding this problem:-
这是 IntelliJ 支持团队在 2014 年 1 月 2 日关于此问题的回复:-
IDEA uses groovyc to generate Java stubs for Groovy classes to allow for seamless interop. Unfortunately stub generation code doesn't launch AST transformations (e.g. Immutable) and so the methods generated by those transformations don't make it into Java stubs, hence Java compiler doesn't see them.
Unfortunately I see no workarounds that don't require modifying your project. One would be to place Groovy files into a separate module. Another would be to change the call places into Groovy. The third one would be to replace @Immutable with @Canonical and generate the constructor so that it's actually in the code (and the stubs will contain it).
You may also vote/watch http://youtrack.jetbrains.com/issue/IDEA-52379to support Eclipse Groovy compiler.
IDEA 使用 groovyc 为 Groovy 类生成 Java 存根以实现无缝互操作。不幸的是,存根生成代码不会启动 AST 转换(例如 Immutable),因此这些转换生成的方法不会进入 Java 存根,因此 Java 编译器看不到它们。
不幸的是,我没有看到不需要修改项目的解决方法。一种方法是将 Groovy 文件放入一个单独的模块中。另一种方法是将调用位置更改为 Groovy。第三个是用@Canonical 替换@Immutable 并生成构造函数,以便它实际上在代码中(并且存根将包含它)。
您还可以投票/观看http://youtrack.jetbrains.com/issue/IDEA-52379以支持 Eclipse Groovy 编译器。
I ended up removing both @Immutable
and @Canonical
and create my own constructors, for 2 reasons:-
我最终删除了@Immutable
and@Canonical
并创建了自己的构造函数,原因有两个:-
- It allows me to run my test case directly from IntelliJ.
- It cleans up JaCoCo code coverage report significantly caused by the unused constructors provided for free by
@Immutable
and@Canonical
.
- 它允许我直接从 IntelliJ 运行我的测试用例。
- 它清除了由
@Immutable
和免费提供的未使用的构造函数引起的 JaCoCo 代码覆盖率报告@Canonical
。
回答by Nick Grealy
I had this problem on the latest version of Intellij ideaIC-15.0.3-custom-jdk-bundled.dmgon MAC 10.10.5, JDK 1.8.0_60.
我对的IntelliJ的最新版本,这个问题ideaIC-15.0.3定制-JDK-bundled.dmg的MAC 10.10.5,JDK 1.8.0_60。
Including all steps for posterity...
包括子孙后代的所有步骤......
- From the terminal, I installed the latest version of
groovy
, using sdkman:sdk install groovy 2.4.5
- In Intellij, right-click on top project > select "Add Framework Support..." > Add
groovy 2.4.5
(if it hasn't already been added). - In Intellij, "Preferences" > "Build, Execution, Deployment" > "Compiler" > "Resource patterns:" > change the order from
!?*.java;!?*.groovy
to!?*.groovy;!?*.java
- Recompile the project (Command+Shift+F9), it should now compile successfully.
- 在终端中,我
groovy
使用sdkman安装了最新版本的:sdk install groovy 2.4.5
- 在 Intellij 中,右键单击顶部项目 > 选择“添加框架支持...” > 添加
groovy 2.4.5
(如果尚未添加)。 - 在 Intellij 中,“首选项”>“构建、执行、部署”>“编译器”>“资源模式:”> 将顺序从更改
!?*.java;!?*.groovy
为!?*.groovy;!?*.java
- 重新编译该项目(Command+ Shift+ F9),它现在应该编译成功。