Configuring IntelliJ to Use Groovy Compiler Instead of Java Compiler

16,802

Solution 1

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.

Solution 2

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.
Share:
16,802

Related videos on Youtube

limc
Author by

limc

Uhm... yea... My Shitty Code Blog My GitHub Page

Updated on August 15, 2022

Comments

  • limc
    limc over 1 year

    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.

    • Seagull
      Seagull over 10 years
      Do you have an Groovy Framework Support for your project? Try to add it, by right-clicking on project root folder in Project view, and select an existing, or create a new Groovy sdk. Also it will add Groovy library in module dependency.