IntelliJ + Groovy + Spock: no Groovy library is defined for module - maven

I have been trying to create a Groovy project with Spock testing in IntelliJ IDEA.
Here are steps that I followed:
Created Groovy project and added Maven support.
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
Due to conflicting Groovy dependency I removed Groovy 2.2 library from the Module Settings->Libraries. This allowed me to run tests.
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.

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

There were two steps to fixing the (broken) project:
Update the groovy-all dependency to version 2.2.1, which I had installed on my machine.
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.

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

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.

Related

Groovy Maven Compilation fails with error: Unable to determine Groovy version. Is Groovy declared as a dependency?

I have a fairly vanilla groovy project that builds using maven. It utilises the gmavenplus plugin, with the pom lifted straight from the gmaven plus web page
pom looks something like this:
<project>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>1.8.0</version>
<executions>
<execution>
<goals>
<goal>addSources</goal>
<goal>addTestSources</goal>
<goal>compile</goal>
<goal>compileTests</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<!-- any version of Groovy \>= 1.5.0 should work here -->
<version>2.5.8</version>
<type>pom</type>
</dependency>
</dependencies>
</project>
When I build, I get the following error on the compile stage.
[INFO] --- gmavenplus-plugin:1.8:compile (default) # myapp---
[INFO] Unable to get Groovy version from GroovySystem, trying InvokerHelper.
[WARNING] Unable to get Groovy version from InvokerHelper or GroovySystem, trying jar name.
[ERROR] Unable to determine Groovy version. Is Groovy declared as a dependency?
And the final maven error results in this:
[ERROR] Failed to execute goal org.codehaus.gmavenplus:gmavenplus-plugin:1.8:compile (default) on project myapp: Execution default of goal org.codehaus.gmavenplus:gmavenplus-plugin:1.8:compile failed. NullPointerException -> [Help 1]
I have tried all manner of tricks, hacks and cajoling to get it to compile. I actually got it working in Intellij by adding the groovy runtime as a direct module dependency (rather than a maven managed dependency), but this doesn't work from the command line.
I had previously had a similar setup working on gmaven plus 1.6 and groovy 2.4.7 on the same laptop, no idea why this is broken now.
So I eventually got it working properly by deleting the entire codehaus dir in the .m2 repo. Re-downloading everything seemed to sort it out and now it all works correctly without any hacks.
This github issue is somewhat related - https://github.com/groovy/GMavenPlus/issues/84 - and the final comment led me to zapping the repo.

maven/apt generated classes in eclipse

Using following configuration in master pom, some classes (metamodel FYI) are generated for all child projects having jpa entities under target/generated-sources, as expected.
<plugin>
<groupId>org.bsc.maven</groupId>
<artifactId>maven-processor-plugin</artifactId>
<version>2.2.4</version>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>4.3.10.Final</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>process</id>
<goals>
<goal>process</goal>
</goals>
<phase>generate-sources</phase>
<configuration>
<processors>
<processor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</processor>
</processors>
</configuration>
</execution>
</executions>
</plugin>
This works perfectly from Maven and Netbeans.
Some team members still use Eclipse. There, generated classes aren't found automatically. They can be opened if added manually to the "build path" (whatever it means as this is redundant to pom.xml). However, that's not stable and will be reset whenever they "update the project" (shouldn't even be needed but...) to reflect project's maven configuration.
My question is, how to configure Eclipse to use this project's configuration automatically? I don't want to change the project's pom.xml too much, as they are perfectly legal and work well outside of Eclipse, which I just want to be taught to behave correctly.
UPDATE: M2Eclipse is installed and doesn't solve this, which is basically our problem.
There ist a project/Eclipse-Plugin called M2Eclipse (link).
The plugin includes the following feature:
Dependency management for Eclipse build path based on Maven's pom.xml

Add external library .jar to Spring boot .jar internal /lib

I have an external .jar that cannot be imported from public repositories using pom.xml, it's sqljdbc41.jar.
I can run the project locally from my IDE, and everything will work. I referenced the library after downloading it like so:
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc41</artifactId>
<version>4.1</version>
<scope>system</scope>
<systemPath>${basedir}/lib/sqljdbc41.jar</systemPath>
</dependency>
When I run mvn clean package to create my .jar file and try to run the created .jar, a mistake will pop up, which mentions the SQL Server references are not valid. I then extracted my .jar file and true enough, everything that is referenced in the pom.xml file properly gets downloaded and added, however, my SQL Server does not.
I can, in a very hacky way* just manually add the sqljdbc41.jar to my /lib folder after it's been compiled as a .jar, and it'll work, however that seems highly unoptimal. What would be a better approach?
*Opening the .jar file with Winrar, going to the /lib folder, manually selecting my sqljdbc41.jar file, then make sure to select the No Compression option bottom left where Winrar gives you compression options, in case you find this by Google and no one answered.
you can set 'includeSystemScope' to true.
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
You could install the sqljdbc41.jar in your local repository :
mvn install:install-file -Dfile=path/to/sqljdbc41.jar -DgroupId=com.microsoft.sqlserver -DartifactId=sqljdbc41 -Dversion=4.1 -Dpackaging=jar
And then declare the dependency as a standard dependency :
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc41</artifactId>
<version>4.1</version>
</dependency>
If you use a remote artifact repository (nexus, archiva...) you also need to deploy the artifact on this repository. You can find more here : https://maven.apache.org/guides/mini/guide-3rd-party-jars-remote.html
Another way, you can put it into the resources folder, such as resources/lib/xxx.jar, then config the pom.xml like this:
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc41</artifactId>
<version>4.1</version>
<scope>system</scope>
<systemPath>${basedir}/src/main/resources/lib/sqljdbc41.jar</systemPath>
</dependency>
In Spring Boot: I also faced similar issue and below code helped me.
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.5.7.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
</plugins>
</build>
It works for me:
project {root folder}/libs/ojdbc-11.2.0.3.jar
pom.xml:
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc</artifactId>
<version>11.2.0.3</version>
<scope>system</scope>
<systemPath>${basedir}/libs/ojdbc-11.2.0.3.jar</systemPath>
</dependency>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
In my case, the fault was providing a version number without "dot" in tag:
<dependency>
<groupId>jdk.tools</groupId>
<artifactId>jdk.tools</artifactId>
<scope>system</scope>
<version>1</version>
<systemPath>${basedir}/src/main/resources/lib/tools.jar</systemPath>
</dependency>
This one works:
<dependency>
<groupId>jdk.tools</groupId>
<artifactId>jdk.tools</artifactId>
<scope>system</scope>
<version>1.8</version>
<systemPath>${basedir}/src/main/resources/lib/tools.jar</systemPath>
</dependency>
When Spring-Boot projects are used with maven or gradle plugins they packaged the applicaiton by default as executable jars.
These executable jars cannot be used as dependency in any another Spring-Boot project because the executable jar add classes in BOOT-INF/classes folder. This means that they cannot be found when the executable jar is used as a dependency because the dependency jar will also have the same class path structure as shown below.
If we want to use project-A as a maven dependency in project-B then we must have two artifacts. To produce the two artifacts, one that can be used as a dependency and one that is executable, a classifier must be specified. This classifier is applied to the name of the executable archive, leaving the default archive for use as a dependency.
To configure a classifier of exec in Maven, you can use the following configuration:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<classifier>exec</classifier>
</configuration>
</plugin>
</plugins>
</build>
So the MAJIC WORD here is <classifier>exec</classifier> this will create a jar structure as below and then it could easily be conusmed by spring-boot project as maven dependency jar on class path.
The above plugin need to be add in project-A pom that is going to be used as dependency in project-B. Same is explained in spring documentation section 16.5. as well.
In order to work through the local repository, the target .jar file that we will work with must be in the s2 folder. Several methods can be used for this:
The file can be taken manually and put in the relevant place (not
preferred). The same process can be done by installing it via the
console.
Relevant Remote URL is written in the .pom file dependencies and
automatically places it in the s2 folder when Intellij is refreshed
(validate) in the IDE used.
The same process can be done by addressing the .pom file dependencies via the centeral repository.
Attention: ComponentScan should not be forgotten for the related jar work on SpringBot.

JUnit tests fail with Java error when using IntelliJ within maven module after adding Hibernate Metamodel classes

On my project we've used Hibernate's (JPA) Metamodel Generator to make our Criteria queries type safe. It all works great within our app, however, when we run the JUnit tests within that Maven module using our IDE they now fail with the following error:-
Error:java: Annotation processor 'org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor' not found
Which I guess is due to the following in our generated classes:-
#Generated(value = "org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor")
#StaticMetamodel(MyEntity.class)
When Maven runs the tests as part of our build process then they run with no problems at all.
I suspect I'm missing something within the set up of my IDE, which is IntelliJ IDEA 14. Any ideas what this might be? Or have I done something wrong within Maven? :-
<plugin>
<groupId>org.bsc.maven</groupId>
<artifactId>maven-processor-plugin</artifactId>
<version>2.1.0</version>
<executions>
<execution>
<id>process</id>
<goals>
<goal>process</goal>
</goals>
<phase>generate-sources</phase>
<configuration>
<processors>
<processor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</processor>
</processors>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>4.3.4.Final</version>
<optional>true</optional>
</dependency>
</dependencies>
</plugin>
I had a similar problem after I upgraded to IntelliJ IDEA 14.1.2. For me the following action resolved the problem:
Go to Settings > Build, Execution, Deployment > Compiler > Annotation Processors.
On the left of this configuration panel I have an Annotation profile for every maven module in my project. I did not set up these profiles myself: maybe they where inferred by the IDE. I don't know, but in some of these annotation profiles, the enable annotation processing flag was enabled. Moreover, in some cases, the JPAMetaModelEntityProcessor was explicitly listed here as an annotation processor. After removing the annotation processor from the profile and disabling the checkbox, the error disappeared and my test ran successfully.
The answer by #Jeroen Noels disables annotation processing in IDEA.
To keep it enabled I've added
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>4.3.4.Final</version>
<scope>provided</scope>
</dependency>
to the Maven dependencies, i.e. to the classpath. Note that the scope is provided!

Groovy file does not compile in Intellij IDEA

I have maven project wit java and groovy tests.
In command line maven compilation all tests are running, but in my IDEA project (which is created automatically, by "AutoImport maven projects", IDEA copies groovy files to /target/test-classes without compiling them.
My gmaven plugin looks like
<plugin>
<groupId>org.codehaus.gmaven</groupId>
<artifactId>gmaven-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<goals>
<goal>generateStubs</goal>
<goal>compile</goal>
<goal>generateTestStubs</goal>
<goal>testCompile</goal>
</goals>
<configuration>
<providerSelection>1.7</providerSelection>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>${groovy.version}</version>
</dependency>
</dependencies>
</plugin>
I had the same issue and had to change in Idea the following setting:
Settings->Compiler->Resource patterns
It was !?*.java
I changed it into
!?.java;!?.form;!?.class;!?.groovy;!?.scala;!?.flex;!?.kt;!?.clj
It would be better to be able to specify it into the pom file though but haven't found a way yet.
GMaven plugin is only intended for maven compilation. Idea uses the Groovy compiler included in groovy-all jar. For Idea to get a hold of that add a project dependency, e.g.:
...
<groupId>yourproject</groupId>
<artifactId>yourproject</artifactId>
<version>1.0.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>${groovy.version}</version>
</dependency>
</dependencies>
...
Solved by removing and creating from scratch IDEA project (ipr file)
Please change folder type of groovy files as Source Root. Step for same is Right Click on folder -> Select option - 'Make Directory as' -> Source Root
As suggested above
I changed it into
!?.java;!?.form;!?.class;!?.groovy;!?.scala;!?.flex;!?.kt;!?.clj
This setting was already available in my intellij.
Still the code was not generating Fixed it by Setting->Compiler
and check compile independent module in parallel and after that it got generated
This is similar to #Opal's answer, but is relevant for tests, as asked in the question:
In the IDE go to Files->Project Structure->Project Settings->Modules. In the Project files tree select the src->test->groovy directory. Hit Alt+T to make the groovy directory the test source root
Please check Settings | Compiler | Resource patterns. It appears that *.groovy somehow suits there and therefore it's copied into output instead of being compiled.
You have to change folder type of groovy files to make directory as source Root.

Resources