IntelliJ + Groovy + Spock: no Groovy library is defined for module
Asked Answered
Y

4

7

I have been trying to create a Groovy project with Spock testing in IntelliJ IDEA.

Here are steps that I followed:

  1. Created Groovy project and added Maven support.
  2. Added Spock dependencies and plugin. I am using POM very similar to this one: https://github.com/mariuszs/java-spock-test-sample/blob/master/pom.xml
  3. Due to conflicting Groovy dependency I removed Groovy 2.2 library from the Module Settings->Libraries. This allowed me to run tests.
  4. I created a Groovy class in "src/main".. but I get the error when I try to run it:

Groovyc: Cannot compile Groovy files: no Groovy library is defined for module...

I am probably missing something because I am tired of trying different configurations for half of the day.

Yeld answered 27/1, 2014 at 19:28 Comment(0)
F
13

For fully groovy project try GMavenPlus

Sample project: https://github.com/mariuszs/groovy-maven-sample

Install GMavenPlus IntelliJ Plugin. IntelliJ dont recognize source directories src/main/groovy, configure this manually as shown below from Project Settings -> Modules window:.

groovy source code

Configuration

<project>
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.gmavenplus</groupId>
        <artifactId>gmavenplus-plugin</artifactId>
        <version>1.5</version>
        <executions>
          <execution>
            <goals>
              <goal>compile</goal>
              <goal>testCompile</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  <dependencies>
    <dependency>
      <groupId>org.codehaus.groovy</groupId>
      <artifactId>groovy-all</artifactId>
      <version>2.4.4</version>
    </dependency>
    <dependency>
        <groupId>org.spockframework</groupId>
        <artifactId>spock-core</artifactId>
        <version>1.0-groovy-2.4</version>
        <scope>test</scope>
    </dependency>     
  </dependencies>
</project>
Formaldehyde answered 27/1, 2014 at 20:7 Comment(2)
quick fast solutions (y) :)Pentarchy
+1 can't understand why IntelliJ made this so difficult and unintuitive; 3 hours just to get groovy spock tests working with java project! :-DChloroform
Y
2

There were two steps to fixing the (broken) project:

  1. Update the groovy-all dependency to version 2.2.1, which I had installed on my machine.
  2. In "projectName.iml" file replace

orderEntry type="library" exported="" scope="TEST" name="Maven: org.codehaus.groovy:groovy-all:2.2.1" level="project"

with this one:

<orderEntry type="library" name="groovy-2.2.1" level="application" />

This situation was caused by two factors: me being new to the IDE and the fact that things kinda-sorta work even when you mis-configure the project. I still think this Q&A might be useful to someone in the future.

Yeld answered 27/1, 2014 at 20:16 Comment(2)
Hmm, did you try right clicking on Project -> Maven -> Reimport? After this IntellIJ do this for you :) Manual editing of *.iml file is not needed and not recommended. Other simple option is to Open project again with "Delete existing configuration and reimport"Formaldehyde
Good to know. p.s. After I edited the answer the Stack does not show part of it :(Yeld
A
1

I had this "Cannot compile Groovy files: no Groovy library is defined for module" issue recently. I needed to mark my groovy test directory as a 'Test Sources Root' in IntelliJ.

Right click on dir --> Mark Directory as --> Test Sources Root

Agar answered 3/1, 2017 at 11:31 Comment(0)
T
0

For me the solution was to open a .groovy file in the specific folder/module and then idea gave a little popup showing the the Groovy SDK is not configured. Then I clicked on configure -> OK and voila.

Thebaine answered 4/12, 2017 at 14:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.