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

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.

Related

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

Is it possible to specify a different JVM to run Eclipse with tycho-surefire-plugin

We have a project which is built with Tycho 0.15.0.
When running the tests (i.e. UI tests), maven executes
cmd.exe /X /C ""C:\Program Files (x86)\Java\jre7\bin\java.exe" -Dosgi.noShutdown=false -Dosgi.os=win32 [...]"
This works so far.
But now, we want to have the test instance run with a different JVM (located e.g., in c:\my_custom_jvm\jre\bin).
Is this possible to achieve? I have searched for possibilities and found the jvm option for the Maven Surefire plug-in, but this does not seem to be supported by tycho-surefire ...
For reference, here's the complete snippet of the pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-surefire-plugin</artifactId>
<version>0.15.0</version>
<configuration>
<testSuite>my.tests</testSuite>
<testClass>my.tests.AllTests</testClass>
<product>my.product</product>
<bundleStartLevel>
<bundle>
<id>org.eclipse.equinox.event</id>
<level>4</level>
<autoStart>true</autoStart>
</bundle>
</bundleStartLevel>
<dependencies>
<dependency>
<type>p2-installable-unit</type>
<artifactId>my.product</artifactId>
<version>0.0.0</version>
</dependency>
</dependencies>
<argLine>-Xmx768m -XX:PermSize=128m -Xss1m -Dosgi.framework.extensions=org.eclipse.equinox.weaving.hook -Dequinox.ds.block_timeout=60000 -Dequinox.use.ds=true</argLine>
</configuration>
</plugin>
</plugins>
</build>
tycho surefire has support for maven toolchains [1]
[1] http://maven.apache.org/guides/mini/guide-using-toolchains.html
(based on answer by jsievers)
The toolchain plugin does exactly what I need.
I added the following lines to my pom.xml (inside the tag):
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-toolchains-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>toolchain</goal>
</goals>
</execution>
</executions>
<configuration>
<toolchains>
<jdk>
<version>1.4</version>
<vendor>sun</vendor>
</jdk>
</toolchains>
</configuration>
</plugin>
And I have created a toolchain.xml file in C:\Users\itsame\.m2 (if you want it to place elsewhere, maybe this helps) with these contents:
<?xml version="1.0" encoding="UTF8"?>
<toolchains>
<toolchain>
<type>jdk</type>
<provides>
<version>1.4</version>
<vendor>sun</vendor>
<id>CustomJRE</id>
</provides>
<configuration>
<jdkHome>c:\my_custom_jvm\jre</jdkHome>
</configuration>
</toolchain>
</toolchains>
Note that even though it is a JRE (not a JDK), this works to run the tests.

gwt-maven-plugin how to add the source jar in another module pom.xml

my application has 2 module, one is jar and the other is gwt war. in the jar module (non-gwt) pom.xml, I add
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.1.2</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
and als-admin-viewer-core-1.0.0-sources.jar is successfully created.
Then In the webapp(a gwt application) pom.xml, I want to use this jar, and in the segment, I add
<plugin>
<groupId>org.codehaus.mojo<groupId>
<artifactId>gwt-maven-plugin</artifactId>
<version>2.4.0</version>
<dependencies>
<dependency>
<groupId>hk.gov.ehr.service.tch.als</groupId>
<artifactId>als-admin-viewer-core</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>hk.gov.ehr.service.tch.als</groupId>
<artifactId>als-admin-viewer-core</artifactId>
<version>1.0.0</version>
<classifier>sources</classifier>
</dependency>
</dependencies>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>compile</goal>
</goals>
</executions>
........
but when I run maven install for this project (als-admin-viewer-webapp), error
No source code is available for type hk.gov.ehr.service.tch.als.admin.viewer.core.LogSearchCriteria; did you forget to inherit a required module?
is prompted.
what is the problem?!!
I even try to add
<compileSourcesArtifacts>
<compileSourcesArtifact>hk.gov.ehr.service.tch.als:als-admin-viewer-core</compileSourcesArtifact> <!-- groupId:artifactId -->
</compileSourcesArtifacts>
in
<configuration>
section of gwt-maven-plugin, but it still does not help!!
I think you forgot to create "gwt.xml" file in your jar module and inherit it in your main gwt.xml (inside gwt maven module).
Please look at
http://mojo.codehaus.org/gwt-maven-plugin/user-guide/library.html
for details (section "Using general purpose JARs as GWT library").
Also bear in mind: if you're using maven-source-plugin to attach sources, your sources will be distributed with the web application. And if you're using "compileSourcesArtifacts" you avoid this side-effect.

Maven generate-sources folder not being picked up by Eclipse

I am in the process of migrating my build from Ant to Maven. The Ant build used to compile a "code generator", execute this "code generator" which produced a set of Java and C code. It then took the generated Java code and compiled it along with some additional code produce a single jar.
I have replicated this this in Maven quite easily and it works well when I run from the command line but Eclipse is complaining and is giving me an error relating to the pom file
Failure to find {group.id}:{artifact.id}:pom:1.0.0-SNAPSHOT in
http://{our internal site repository}/nexus/content/groups/public was
cached in the local repository, resolution will not be reattempted
until the update interval of snapshots has elapsed or updates are
forced
where the group.id and artifact.id are the group and artifact id of my code generator plugin
and any code that references the generated code also fails to compile.
My maven build consists of
a generator project that contains just the Java code for the code generator.
a generator-plugin project that contains just the code to wrap the generator as a Maven plugin. This is dependent upon the generator project.
an xyz project that uses the plugin to generate the code. The code ends up in the target/generated-sources/xxx folder of this project. I have configured the build-helper-maven-plugin as per Maven compile with multiple src directories to include this extra source directory.
If I manually add the generated source folder to the Eclipse build path all of the errors relating to the code not being there go away on that project but not on any downstream projects and the "Failure to find..." error listed above remains.
What puzzles me a little is that it is referring to the ...:pom:1.0.0-SNAPSHOT when in fact my generator-plugin is defined as a maven-plugin.
Is this a sensible approach?
Why am I getting a "Failure to find..." error?
Why isn't Eclipse picking up my generated-sources folders?
I should also say I have the m2e plugin and the m2e connector for build-help-maven-plugin installed in my Eclipse IDE.
It looks like a problem during the download of the lib from the repository. I already had the same error message once.
Did you take a look at your local repository?
Go to the .m2 folder and look for /nexus/content/groups/public. If the folder is there, open it and see if the lib was download correctly. If not, try to delete the folder and try to run mvn install to force the download of the lib.
At Eclipse, run Right button > Maven > Update Project too.
Are you using an local repository like Artifactory, aren't you? Also look for the lib in the repo1-cache (or similar) folder. See if the jar is there.
Are you behind any firewall or proxy?
Using eclipse Indigo3.7, this was what I found that worked good using spring 3.1.1 which does have the 3.0.6 version as well in it. Once I got the plugins setup and put into the correct area of the pom and included the argline and endorseddirs to have the java sources put out into the target/generated-sources/cxf folder then maven generated the sources ok.
....
<properties>...
<dependencyManagement>
<dependencies>.....
</dependencyManagement>
<dependencies>
<dependency>....
</dependencies>
<!-- *************************** Build process ************************************* -->
<build>
<finalName>projName</finalName>
<plugins>
<!-- Force Java 6 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.4</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<!-- Deployent on AS from console
<plugin>
<groupId>org.jboss.as.plugins</groupId>
<artifactId>jboss-as-maven-plugin</artifactId>
<version>${version.jboss.as.maven.plugin}</version>
</plugin>
-->
<!-- wildbill added tomcat plugin -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.0</version>
</plugin>
<!-- Surefire plugin before 2.9 version is buggy. No need to declare here,
it's being referenced below w/ the version
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12</version>
</plugin>
-->
<!-- developer added these -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerArguments>
<endorseddirs>target/generated-sources/cxf</endorseddirs>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12</version>
<configuration>
<forkMode>once</forkMode>
<argLine>-Djava.endorsed.dirs=target/generated-sources/cxf</argLine>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerArguments>
<endorseddirs>target/generated-sources/cxf</endorseddirs>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<forkMode>once</forkMode>
<argLine>-Djava.endorsed.dirs=target/generated-sources/cxf</argLine>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<configuration>
<artifactItems>
<artifactItem>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.2</version>
</artifactItem>
<artifactItem>
<groupId>javax.xml.ws</groupId>
<artifactId>jaxws-api</artifactId>
<version>2.2</version>
</artifactItem>
</artifactItems>
<outputDirectory>target/generated-sources/cxf</outputDirectory>
</configuration>
</plugin>
</plugins>
</build>
<!-- *********************** Profiles ************************************ -->
<profiles>
<profile>
<!-- When built in OpenShift the 'openshift' profile will be
used when invoking mvn. -->
<!-- Use this profile for any OpenShift specific customization
your app will need. -->
<!-- By default that is to put the resulting archive into the
'deployments' folder. -->
<!-- http://maven.apache.org/guides/mini/guide-building-for-different-environments.html -->
<id>projName</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-codegen-plugin</artifactId>
<version>2.5.2</version>
<executions>
<execution>
<id>process-sources</id>
<phase>generate-sources</phase>
<configuration>
<fork>once</fork>
<additionalJvmArgs>-Djava.endorsed.dirs=target/generated-sources/cxf</additionalJvmArgs>
</configuration>
<goals>
<goal>wsdl2java</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-xjc</artifactId>
<version>2.2</version>
</dependency>
</dependencies>
</plugin>
<!-- Actual war created in default target dir -->
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.2</version>
</plugin>
</plugins>
</build>
</profile>
</profiles>
If your wsdl folder is in ${basedir}/src/main/resources it'll find it automatically
Hope this helps!

Maven GWT Plugin copies multiple versions of the same snapshot jars

I have this issue where I build my project (mvn clean install), some of the transitive dependencies are snapshot versions and are downloaded and copied into the target webapp directory e.g XXXUtil-1.0-20110922.172721-52.jar. Then when I run mvn gwt:run, it finds uses XXXUtil-1.0-SNAPSHOT.jar and copies it to the target webapp directory. I can't figure out why this is happening. In doesn't matter whether I run as exploded or inplace.
<plugins>
<!-- GWT Maven Plugin -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>gwt-maven-plugin</artifactId>
<version>2.3.0-1</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>i18n</goal>
<goal>generateAsync</goal>
</goals>
</execution>
</executions>
<configuration>
<runTarget>Shell.html</runTarget>
<hostedWebapp>${webappDirectory}</hostedWebapp>
<i18nMessagesBundle>com.myapp.client.Messages</i18nMessagesBundle>
</configuration>
<dependencies>
<dependency>
<groupId>com.google.gwt</groupId>
<artifactId>gwt-user</artifactId>
<version>${gwt.version}</version>
</dependency>
<dependency>
<groupId>com.google.gwt</groupId>
<artifactId>gwt-dev</artifactId>
<version>${gwt.version}</version>
</dependency>
</dependencies>
</plugin>
<!-- Copy static web files before executing gwt:run -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>exploded</goal>
</goals>
</execution>
</executions>
<configuration>
<!-- <outputFileNameMapping>#{artifactId}#-#{version}#.#{extension}#</outputFileNameMapping> -->
<webappDirectory>${webappDirectory}</webappDirectory>
</configuration>
</plugin>
</plugins>
None of the suggestions described here help:
http://www.tikalk.com/alm/forums/maven-war-plugin-picking-multiple-version-same-snapshot-jars.
If i build local snapshots of XXXUtil-1.0-SNAPSHOT.jar it works buts not when downloading snapshots from a nexus repository. Another way to look at it is like this Project A generates a WAR, and depends on B.jar, which depends on C.jar. When i build my war using mvn install, it generates the correct jars in WEB-INF/lib so we have C-1.0-20110922.172721-52.jar. Which is correct and it works if i deploy my war. If i run in hosted mode using eclipse, its fine. But when i run mvn:gwt-run, C-1.0-SNAPSHOT.jar is copied into WEB-INF/lib so i have 2 jars C-1.0-SNAPSHOT.jar and C-1.0-20110922.172721-52.jar.
The only thing I can suggest you is to try to debug maven-gwt-plugin.
Checkout it from git repository
https://github.com/gwt-maven-plugin/gwt-maven-plugin.git
I had exactly the same problem. After debugging, I removed the use of maven-war-plugin and added maven-resources-plugin (compile phase, copy-resources goal). I tried gwt:run and install after that, worked without any problems. This way, we avoid the dependencies getting copied twice.