Sunday, March 22, 2009

maven-antrun-plugin

Sometimes, you need to customize Maven to your need. One way is to write your own plugin. One interesting topic would be to use ant script to create your own plugin. But seems the classpath and environment settings are troublesome here. There are some better ways, for example, the maven-antrun-plugin is really useful for you to twist the build process a bit. The good thing for maven-ant-plugin is that it provides you classpath information so that you can access Maven build classpath.

Our Tellurium core build was broken for a while because GMaven has problem to generate stubs for enum and generic types and our core code has a mix of Java and Groovy code. The maven-antrun-plugin comes to rescue us this time. As a result, we compile the some Java code first using maven-antrun-plugin since some groovy files need to import them. The pom section looks as follows,

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<dependencies>
<dependency>
<groupId>sun.jdk</groupId>
<artifactId>tools</artifactId>
<version>1.5.0</version>
<scope>system</scope>
<systemPath>${java.home}/../lib/tools.jar</systemPath>
</dependency>
</dependencies>

<executions>
<execution>
<phase>generate-sources</phase>
<configuration>
<tasks>
<mkdir dir="${basedir}/target/classes"/>
<javac srcdir="${basedir}/src/main/groovy"
destdir="${basedir}/target/classes" classpathref="maven.plugin.classpath">
<include name="**/org/tellurium/exception/*.java" />
</javac>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>

Note that the javac ant task requires the tools.jar, as a result, you should add this dependency to maven-antrun-plugin, otherwise, the build will complain about cannot find the javac task.

No comments:

Post a Comment