Configuring IntelliJ to use the Groovy Compiler instead of the Java Compiler

In my maven project, I am currently mixing my Java code with some Groovy code. I use Groovy mainly to create beans at this point. Some of my Java code uses Groovy beans directly.

I configured the Maven Compiler plugin as follows: -

<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 test files using mvn test , it works fine.

However, when I run the test files directly from IntelliJ, by right-clicking the test file and running it, I get "can not find character" errors on Groovy beans. When I read the error log, IntelliJ uses the Java compiler to compile my project before running the test ... so the tests fail.

I can't figure out how to instruct IntelliJ to always use the Groovy compiler instead of the Java compiler.

What should I change in the SDK to use the Groovy compiler? I tried to add Groovy related JAR files, but I got other errors.

enter image description here

UPDATE 1: As suggested by @Seagull

I added the Groovy JAR in the Global Libraries section: -

enter image description here

When I executed the test file directly from IntelliJ, I get Groovy warnings and I still get the same error: -

enter image description here

Thanks.

+8
java intellij-idea maven groovy
source share
2 answers

This is the answer of the IntelliJ support team on January 2, 2014 on this issue: -

IDEA uses groovyc to create Java boxes for Groovy classes to allow seamless interaction. Unfortunately, the stub generation code does not trigger AST transformations (for example, immutable), and therefore the methods generated by these transformations do not turn into Java stubs, so the Java compiler does not see them.

Unfortunately, I do not see workarounds that do not require changing your project. One could put Groovy files in a separate module. Another option is to change the call locations to Groovy. The third would be to replace @Immutable with @Canonical and generate the constructor so that it is actually in the code (and the stubs will contain it).

You can also vote / watch http://youtrack.jetbrains.com/issue/IDEA-52379 to support the Eclipse Groovy compiler.

In the end, I removed both @Immutable and @Canonical and created my own constructors for two reasons: -

  • This allows me to run my test case directly from IntelliJ.
  • It clears the JaCoCo code coverage report, which is significantly caused by unused constructors provided for free by @Immutable and @Canonical .
+3
source share

I had this problem in the latest version of Intellij ideaIC-15.0.3-custom-jdk-bundled.dmg on 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 the main project> select "Add Support for Framework ..."> Add groovy 2.4.5 (if it has not already been added).
  • In Intellij, Settings> Build, Run, Deploy> Compiler> Resource Templates:> change the order from !?*.java;!?*.groovy to !?*.groovy;!?*.java
  • Recompile the project ( Command + Shift + F9 ), it should now compile successfully.
+5
source share

All Articles