Inject Groovy into existing Maven Java Project - eclipse

I have been maintaining a maven java project for year.
Recently, I learned Ruby and asked why haven't these nice features (of Ruby) existed in Java, and I am so happy to find Groovy the answer. It's already out there for more than 6 years and what a shame I didn't know about it sooner.
Now come to the story:
I have a lot of java code written already, organized in folder structures follow maven default convention (src/main/java for logic & src/test/java for test)
Now, I want to write some new stuff in Groovy, so I guess I should create src/main/groovy for groovy logic and src/test/groovy for test. However, both mvn eclipse:eclipse and the latest m2eclipse only understand and include src/main/groovy as source code folder of the generated eclipse project, and don't not recognize src/test/groovy at all.
Is this the correct behavior? Or am I missed any thing?
By the way, here is the gmaven plugin configured inside my POM:
<build>
...
<plugins>
...
<plugin>
<groupId>org.codehaus.gmaven</groupId>
<artifactId>gmaven-plugin</artifactId>
<version>1.2</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.gmaven.runtime</groupId>
<artifactId>gmaven-runtime-1.7</artifactId>
<version>1.2</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>groovy-all</artifactId>
<groupId>org.codehaus.groovy</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>1.7.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
<configuration>
<providerSelection>1.7</providerSelection>
</configuration>
</plugin>
...
</plugins>
...
</build>

You may find that you have better luck with new version of m2eclipse instead of eclipse:eclipse. Either way, once your project is in eclipse, navigate your src/{main,test}/ folder, right-click and choose Build Path and "User as Source Folder".

Using eclipse can be more comfortable :
Import your project into eclipse as "Maven project"
1) go to Eclipse Marketplace & install groovy compiler
2)right-click on project and configure it as groovy project
3)result :

Related

org.osgi import cannot be resolved

I am trying to get started with OSGI and create a basic bundle.
I created a bundle with activator in Eclipse (2019-06) and selected Liberty as the target runtime (the end goal is to create a Liberty extension)
It works fine, but when I convert it to Maven, Eclipse complains the org.osgi package cannot be resolved
I see this dependency is defined:
<dependency>
<groupId>net.wasdev.maven.tools.targets</groupId>
<artifactId>liberty-target</artifactId>
<version>19.0.0.9</version>
<type>pom</type>
<scope>provided</scope>
</dependency>
I tried adding this to the felix plugin with no luck.
<Import-Package>
org.osgi.framework
</Import-Package>
After trying thing for a while I am ready to give up. Any help would be appreciated.
Add these maven dependencies for the OSGi specs.
<dependency>
<groupId>org.osgi</groupId>
<artifactId>osgi.core</artifactId>
<version>6.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>osgi.cmpn</artifactId>
<version>6.0.0</version>
<scope>provided</scope>
</dependency>
You also have to configure the bnd-maven-plugin. So your imports are computed automatically. Alternatively you can use the maven-bundle-plugin. (You might already use this).
<plugin>
<groupId>biz.aQute.bnd</groupId>
<artifactId>bnd-maven-plugin</artifactId>
<version>4.2.0</version>
<executions>
<execution>
<goals>
<goal>bnd-process</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
</archive>
</configuration>
</plugin>
This covers the compile time resolution of your packages.
In addition you need to provide the correct bundles at runtime. This highly depends on how you define your runtime.
Some org.osgi package come from the OSGi framework itself. Others have to be installed as bundles. Be aware that you normally do not install the specs as bundles. Instead you install implementations that also bring the specs.

JPA Static Metamodel not recognized by IntelliJ

I generated the application with JHipster with Gradle as the build tool.
When I created entity I added filtering support, which generated JPA static metamodel. But IntelliJ doesn't recognize the metamodels.
I have enabled the annotation processor settings on IntelliJ but it doesn't seem to work.
What settings do I have to change for IntelliJ to recognize the JPA static metamodels?
By default, the metamodel classes get generated into the /target/generated-sources/annotations folder. It seems like that folder isn't registered as a source folder.
You can either change that manually in your IDE or if you're using a Maven build, you can do that automatically by adding the following plugin to your build configuration:
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>target/generated-sources/annotations</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
...
</project>
I explained that in more details in one of my Hibernate Tips.
I'm not allowed to comment but I wanted to add to Thorben Janssen's answer.
Besides the plugin config I also had to add this to the dependencies of the project:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>5.4.21.Final</version>
</dependency>
This is what generates the sources in the target/generated-sources/annotations path.
So the pom ended up like this:
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>5.4.21.Final</version>
</dependency>
...
</dependencies>
...
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>add-source</id>
<goals>
<goal>add-source</goal>
</goals>
<phase>generate-sources</phase>
<configuration>
<sources>
<source>target/generated-sources/annotations</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
...
</build>
To get IntelliJ IDEA to recognize the generated classes, I had to add this line on build.gradle
sourceSets {
main.java.srcDirs += 'build/generated/source/apt/main'
}
Update
Better solution is to modify IntelliJ Plugin
idea {
module {
sourceDirs += file("build/generated/source/apt/main")
generatedSourceDirs += file("build/generated/source/apt/main")
}
}
Intellij's build recognize all processors listed in this file:
META-INF/services/javax.annotation.processing.Processor
.
Case you use Eclipse Link, include this line inside the file:
org.eclipse.persistence.internal.jpa.modelgen.CanonicalModelProcessor
Case Hibernate:
org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor
Ensure that you have all dependencys: I will describe using maven just for example:
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.jpa.modelgen.processor</artifactId>
<version>2.7.0</version>
<scope>provided</scope>
</dependency>
OR
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>5.2.12.Final</version>
<scope>provided</scope>
</dependency>
For me it wasn't a problem of the configuration files (none of the above mentioned solutions worked), but I simply had to reload all Maven project files.
For this in IntelliJ Idea:
Go to the Maven tab on the right side of the IDE (you might have to make it visible under View -> Tool Windows)
Open the project and compile
On the top left corner of the tab press Reload all Maven Projects
Now, the meta classes (e.g. SampleClass_) should be importable and recognized by IntelliJ

Import soapui tests to eclipse

How do I take imported SoapUI project and tie it to my current Maven Eclipse project, and be able to see this SoapUI project when I am in the Java Perspective view?
I have a SoapUI project that I would like to import into my Eclipse Maven project.
I can import this SoapUI project to eclipse when I go to the soapUI Perspective view and do a 'Import Project.' This works great.
I want to have my maven plugin to execute the projects/testCases/testSuites in my current soapUI project file.
It is probably simple, I am just not seeing how to make this final connection between the soapUI and eclipse projects to run all projects and get reports.
enter link description hereYou are probably looking for maven-soapui-plugin - configure it in your pom.xml build - block:
<plugin>
<groupId>eviware</groupId>
<artifactId>maven-soapui-plugin</artifactId>
<executions>
<execution>
<id>soap-integration-test</id>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<projectFile>${basedir}/target/path/to/your/soapUIprojectFile.xml</projectFile>
<!-- move reports directly to surefire-reports-->
<outputFolder>${basedir}/target/surefire-reports</outputFolder>
<junitReport>true</junitReport>
<printReport>true</printReport>
<!--move soapui-logs into target dir-->
<soapuiProperties>
<property>
<name>soapui.logroot</name>
<value>${project.build.directory}/soapui/logs/${soapui.testsuite.version}/</value>
</property>
</soapuiProperties>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.junit_version}</version>
<scope>runtime</scope>
</dependency>
...
</dependencies>
</plugin>
Look for more info here

Groovy Maven Plugins

I am trying to run Spock tests and perhaps some Groovy scripts, currently for a Groovy app, but in future for Java projects as well.
I am really confused about which plugins should be used and how to best configure them, aka least amount of code.
I found numerous articles showing Maven XML settings for gmaven-plugin, gmaven-runtime and groovy-eclipse plugin, which seems like it works under IDEA as well.
I have been using Eclipse for a while and now am trying out IDEA Community Edition.
One of the confusions stems from the fact that I was able to add following dependency to my POM, in addition to having Gmaven in section.
<dependency>
<groupId>org.codehaus.gmaven</groupId>
<artifactId>gmaven-plugin</artifactId>
<version>1.5</version>
</dependency>
I understand that the question is a bit vague... but so is documentation on the matter :)
For fully Maven-Groovy projects read this post Building your Groovy 2.0 projects with Maven
I have sample project with Maven, Java and Spock tests. There is one requirement here. Spock tests needs to have names like FooTest.groovy.
My configuration looks like this
<plugin>
<groupId>org.codehaus.gmaven</groupId>
<artifactId>gmaven-plugin</artifactId>
<version>1.5</version>
<configuration>
<providerSelection>2.0</providerSelection>
<source/>
</configuration>
<executions>
<execution>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.codehaus.gmaven.runtime</groupId>
<artifactId>gmaven-runtime-2.0</artifactId>
<version>1.5</version>
<exclusions>
<exclusion>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.2.1</version>
</dependency>
</dependencies>
</plugin>
Fully working project is here: https://github.com/mariuszs/java-spock-test-sample

Getting groovy, maven, and eclipse to play nice together?

So we have some unit tests written in groovy. We have the Groovy Eclipse plugin going, we have gmaven going, but the problem is that the maven eclipse plugin doesn't automatically add the src/test/groovy directory as a source directory. So, I enlisted the build-helper plugin to add a source directory, but then the problem becomes the source directory - in eclipse, the filters will include **/*.java and exclude everything else, which leads to the groovy eclipse plugin being confused. I've managed to jury-rig the problem by using the build helper to add-test-resource with the right .groovy file filter. Obviously the problem here is that is not usable if we decided to use groovy classes in the projects - the .groovy classes would be included in the .jar files.
How do I fix this?
I dumped gmaven in favor of the groovy-compiler-plugin, which does the groovy compiler weaving for you. With gmaven I wound up with too many weird compiler errors where stubs were missing, etc. You still need the builder-helper, and the Groovy Eclipse plugin helps in linking the source to the compiled classes, but this has worked flawlessly between working within eclipse and at the command line.
<properties>
<groovy.version>1.8.0</groovy.version>
<groovy.provider>1.7</groovy.provider>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.5</version>
<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>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<compilerId>groovy-eclipse-compiler</compilerId>
<verbose>true</verbose>
<source>1.6</source>
<target>1.6</target>
</configuration>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-batch</artifactId>
<version>1.8.0-03</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-compiler</artifactId>
<version>2.5.1</version>
<exclusions>
<exclusion>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-batch</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</plugin>
</plugins>
</pluginManagement>
</build>
We have created m2eclipse integration for Groovy-Eclipse. First, you must install m2eclipse:
http://m2eclipse.sonatype.org/sites/m2e
Then you can install the Groovy-Eclipse integration, which you can get here:
http://dist.codehaus.org/groovy/distributions/greclipse/snapshot/e3.6/
or here for Galileo:
http://dist.codehaus.org/groovy/distributions/greclipse/snapshot/e3.5/
Note that the m2eclipse integration is still beta and we appreciate feedback from users to see how well it works for them.
I happened to check out the maven eclipse plugin page and it turns out this type of problem is already solved:
http://maven.apache.org/plugins/maven-eclipse-plugin/examples/specifying-source-path-inclusions-and-exclusions.html
I ended up just using the build-helper-plugin to specify additional sources and added .groovy files to the source includes for the eclipse plugin.