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.
UPDATE 1: Per @Seagull suggestion
I added groovy JARs under "Global Libraries":-
When I executed the test file directly from IntelliJ, I'm getting some Groovy warnings and I still get the same error:-
Thanks.
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.
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.
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...
groovy
, using sdkman: sdk install groovy 2.4.5
groovy 2.4.5
(if it hasn't already been added).!?*.java;!?*.groovy
to !?*.groovy;!?*.java
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:-
@Immutable
and @Canonical
.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With