Maven 2 and Groovy

February 28th, 2007 Mario Hochreiter

I am currently trying to get unit tests written in groovy running for a maven project. First i thought this would be easy going but it is definitely not. First i tried the groovy-maven-plugin version 1.0-alpha-1-SNAPSHOT from the 27th of February.
As i want to compile my groovy unit test to java byte code I just inserted following XML snippet to my parent pom.xml:

XML:
  1. <plugin>
  2.     <groupId>org.codehaus.mojo</groupId>
  3.     <artifactId>groovy-maven-plugin</artifactId>
  4.     <version>1.0-alpha-1-SNAPSHOT</version>
  5.     <executions>
  6.         <execution>
  7.             <phase>test-compile</phase>
  8.             <goals>
  9.                 <goal>compile</goal>
  10.             </goals>
  11.         </execution>
  12.         <configuration>
  13.           <sourceDirectories>
  14.             ${project.testSourceRoots}
  15.           </sourceDirectories>
  16.        </configuration>
  17.     </executions>
  18. </plugin>

This approach did not work. If i tried to run mvn test the plugin did not get executed and if i tried mvn groovy:compile maven said that it could not find the correct version for plugin org.apache.maven.plugins:groovy-maven-plugin. Huuh? Why is maven not considering the configured group id? After playing around for a while with this problem and without finding any solution I decided to try another approach.
After a short internet research i found the following guide about compiling groovy with maven 2.

XML:
  1. <plugin>
  2.   <artifactId>maven-antrun-plugin</artifactId>
  3.   <executions>
  4.     <execution>
  5.       <id>test-compile</id>
  6.       <phase>test-compile</phase>
  7.       <configuration>
  8.         <tasks>
  9.           <taskdef name="groovyc" classname="org.codehaus.groovy.ant.Groovyc">
  10.             <classpath refid="maven.compile.classpath"/>
  11.           </taskdef>
  12.           <mkdir dir="${project.build.testOutputDirectory}"/>
  13.           <groovyc destdir="${project.build.testOutputDirectory}"
  14.             srcdir="${basedir}/src/test/java/" listfiles="true">
  15.             <classpath refid="maven.test.classpath"/>
  16.           </groovyc>
  17.         </tasks>
  18.       </configuration>
  19.       <goals>
  20.         <goal>run</goal>
  21.       </goals>
  22.     </execution>
  23.   </executions>
  24. </plugin>

This approach simply runs an ant task which executes the groovy compiler on all groovy files. For a simple project setup this approach works without problems but i run into some as I already have a maven-antrun-plugin in my parent pom.xml in the profile section. But profiles are not inherited to childs. This leads to some strange inheritance problems which i do not fully understand at the moment. Why they appear in the way the are appearing. As soon I've found a solution or at least found out the reason for the problems I run into I'll tell you. So stay tuned ;-)

Entry Filed under: Maven

Leave a Comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


Calendar

February 2007
M T W T F S S
« Jan   Mar »
 1234
567891011
12131415161718
19202122232425
262728  

Most Recent Posts