4

I have a project in IntelliJ IDEA that consists of both Java and Groovy classes. These classes are contained in folders "groovy" and "java" that I've marked as source folders. I have many Java classes that import classes from the "groovy" source folder, but when I try running them, I consistently get the error "java: package foo does not exist". Package "foo" exists directly under the "groovy" folder, so this should be working. I included a visual below. (I'm trying to avoid any specific details. I may or may not be working on a top secret Area 51 project.)

Structure visual:

project-folder
|
-src
 |
 -main
  |
  -groovy (marked as source)
  ||
  |-foo
  | |
  | -bar.groovy
  -java (marked as source)
   |
   - java class that imports "foo.bar"

Error: java: package foo does not exist

Things that don't work:

  • Taking everything under "framework" and placing them directly under "groovy" folder. Results in "Cannot resolve symbol bar"

  • Unmavenizing project and rebuilding

Ben Dohrmann
  • 51
  • 1
  • 1
  • 5

3 Answers3

5

You should not have to "unmavenize" your project. (Although I understand the troubleshooting reasoning for suggesting you do such.) I suspect the issue is a corrupted cache or index. Go to File > Invalidate Cache. Select to invalidate the cache and then restart IDEA. Let IDEA re-index the project. Things should be fine. If not, check that 1) you are using the latest version of IDEA (12.1.5) and 2) the latest version of the Groovy plug-in (File > Settings > [IDE Settings] > Plugins).

When you do use maven, you will need to identify the "groovy" directory as an additional source directory in your POM. If you do not, when IDEA re-imports the project (i.e. re-syncs to the POM), it will drop the groovy directory as a source since by default maven does not consider it a source. How you do this depends on what plugin you use. Since GMaven is no longer maintained, I've been using the groovy-eclipse-compiler plugin. If you use that plug-in, the plug-in will automatically include src/main/groovy as a source (as long as there is at least one java or groovy file in src/main/java). However, IDEA does not pick that directory up and include it as a source as well. That means if you manually (or IDEA automatically) runs a maven re-import, your src/main/groovy directory will get unmarked as a source, and IDEA will show compile errors. You need to specify the additional directory. You can use the build-helper-maven-plugin to do this as the groovy-eclipse-compiler documentation recommends.

Here's the meat & potatoes of a POM for a working Java/Groovy project:

<properties>
    <groovy.version>2.1.5</groovy.version>
    <groovy-eclipse-compiler.version>2.8.0-01</groovy-eclipse-compiler.version>
</properties>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <compilerId>groovy-eclipse-compiler</compilerId>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.codehaus.groovy</groupId>
                    <artifactId>groovy-eclipse-compiler</artifactId>
                    <version>${groovy-eclipse-compiler.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.codehaus.groovy</groupId>
                    <artifactId>groovy-eclipse-batch</artifactId>
                    <version>2.1.5-03</version>
                    <scope>compile</scope>
                </dependency>
            </dependencies>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <executions>
                <execution>
                    <id>add-source</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>add-source</goal>
                    </goals>
                    <configuration>
                        <sources>
                            <source>src/main/groovy</source>
                        </sources>
                    </configuration>
                </execution>
                <execution>
                    <id>add-test-source</id>
                    <phase>generate-test-sources</phase>
                    <goals>
                        <goal>add-test-source</goal>
                    </goals>
                    <configuration>
                        <sources>
                            <source>src/test/groovy</source>
                        </sources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

<dependencies>
    <dependency>
        <groupId>org.codehaus.groovy</groupId>
        <artifactId>groovy-all</artifactId>
        <version>${groovy.version}</version>
    </dependency>
</dependencies>
Javaru
  • 30,412
  • 11
  • 93
  • 70
  • Thanks for the detailed answer, Mark, but I'm still having trouble. Invalidating the cache and re-indexing the project did not work. My version of IntelliJ IDEA is 12.1.5, and the Groovy plugin is up to date. I tried adapting your POM code into my own, but the line `${groovy-eclipse-compiler.version}` is getting the error message `Dependency org.codehaus.groovy:groovy-eclipse-compiler:2.8.0-01" not found`. (It works fine in Eclipse.) – Ben Dohrmann Oct 08 '13 at 16:22
  • Just FYI, I fixed the POM where I added your code (still not sure what was wrong), but some new errors were introduced by a coworker, so I'm looking into that before continuing this. – Ben Dohrmann Oct 08 '13 at 19:06
2

Restart IntelliJ :-) Dumb, but that's what worked for me. No idea what was causing the issue, but I'm glad it's fixed. Hopefully that helps someone else too.

Snekse
  • 15,474
  • 10
  • 62
  • 77
0

I had the similar problem. In my case, groovy compilation logged RuntimeException as warning. This is because of No suitable classloader found for grab. After I fixed this issue, groovy sources were successfully compiled and Java classes were able to see them on the classpath.

Community
  • 1
  • 1
suman j
  • 6,710
  • 11
  • 58
  • 109