Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuring IntelliJ to Use Groovy Compiler Instead of Java Compiler

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.

I configured the Maven Compiler Plugin like this:-

<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.

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.

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

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.

enter image description here

UPDATE 1: Per @Seagull suggestion

I added groovy JARs under "Global Libraries":-

enter image description here

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

enter image description here

Thanks.

like image 795
limc Avatar asked Dec 10 '13 00:12

limc


People also ask

How do I change the compiler in IntelliJ?

Press Ctrl+Alt+S to open the IDE settings and select Build, Execution, Deployment| Compiler. On the Compiler page, configure settings that are related to any compilation and build process. For example, you can set the automatic compilation for your project.

How do I run a Groovy script in IntelliJ?

Add Groovy script IntelliJ IDEA opens the file in the editor. Type the following code: println("Hello, Groovy!") Press Ctrl+Shift+F10 to run the script.


2 Answers

I had this problem on 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...

  1. From the terminal, I installed the latest version of groovy, using sdkman: sdk install groovy 2.4.5
  2. In Intellij, right-click on top project > select "Add Framework Support..." > Add groovy 2.4.5 (if it hasn't already been added).
  3. In Intellij, "Preferences" > "Build, Execution, Deployment" > "Compiler" > "Resource patterns:" > change the order from !?*.java;!?*.groovy to !?*.groovy;!?*.java
  4. Recompile the project (Command+Shift+F9), it should now compile successfully.
like image 55
Nick Grealy Avatar answered Oct 09 '22 15:10

Nick Grealy


This is the reply from the IntelliJ support team on January 2, 2014 regarding this problem:-

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-52379 to support Eclipse Groovy compiler.

I ended up removing both @Immutable and @Canonical and create my own constructors, for 2 reasons:-

  • 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.
like image 23
limc Avatar answered Oct 09 '22 13:10

limc