Use Ant-Contrib Tasks in Maven

I just messed around a while to get a simple Ant-contrib task running inside a Maven build – a simple <if>.
The examples on the net weren’t very helpful or too bloated, so i’ll post a very simple one.

The problem with Ant-contrib is that this library doesn’t come with the regular Ant-distribution. So it has to be downloaded separately.
But inside the Maven-antrun-plugin, Ant can’t access the ant-contrib.jar without explicitely referencing it with a hard-coded path (for example classpath=”C:/Dev/Ant/ant-contrib-1.0b3.jar”) – although it’s in the system classpath.

As it’s not the best practice to refer to local filesystem dependencies and rather downloading and using them within the Maven lifecycle, i’m referencing the ant-contrib.jar that Maven downloads when resolving the dependencies.

In my example i’m checking if the environment variable GLASSFISH has been set, failing the complete build if it’s not.
So my pom.xml looks like this:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>mackaz</groupId>
 <artifactId>antcontrib</artifactId>
 <version>1.0-SNAPSHOT</version>

 <dependencies>
  <!-- Ant-Contrib for Special Ant Tasks (IF) -->
  <dependency>
   <groupId>ant-contrib</groupId>
   <artifactId>ant-contrib</artifactId>
   <version>1.0b3</version>
   <exclusions>
    <exclusion>
     <groupId>ant</groupId>
     <artifactId>ant</artifactId>
    </exclusion>
   </exclusions>
  </dependency>
  <dependency>
   <groupId>ant</groupId>
   <artifactId>ant-nodeps</artifactId>
   <version>1.6.5</version>
  </dependency>
 </dependencies>

 <build>
  <plugins>
  <!-- Ant Task with Contrib -->
   <plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <executions>
     <execution>
     <phase>validate</phase>
     <configuration>
     <tasks>
       <!-- point to the dependency in the local Maven repository -->
       <taskdef resource="net/sf/antcontrib/antcontrib.properties" classpath="${settings.localRepository}/ant-contrib/ant-contrib/1.0b3/ant-contrib-1.0b3.jar" />
        <if>
         <not><isset property="env.GLASSFISH" /></not>
         <then>
          <fail>Glassfish Environment Property not set!</fail>
         </then>
         <else>
          <echo>Glassfish Environment Property is correct.</echo>
         </else>
        </if>
       </tasks>
      </configuration>
      <goals>
       <goal>run</goal>
      </goals>
     </execution>
    </executions>
   </plugin>
  </plugins>
 </build>
</project>

It’s also not perfect, but better then the examples i saw. The only downside is that when upgrading or downgrading Ant-contrib, the referenced path has to be modified.

This entry was posted in General and tagged , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

3 Comments

  1. Guillaume
    Posted 29. December 2010 at 13:40 | Permalink

    Thanks a lot, I was exactly looking for something like that.
    By the way, you can improve your plugin definition by using this:
    ${settings.localRepository} (maven built-in) instead of ${env.M2_REPO}

  2. ingo
    Posted 30. December 2010 at 15:35 | Permalink

    @Guillaume thanks! Thats nicer. Replaced the variable accordingly.

  3. Pietrino
    Posted 23. March 2011 at 03:26 | Permalink

    A slightly different approach can be found here:
    http://docs.codehaus.org/display/MAVENUSER/Using+ant-contrib+tasks+with+maven-antrun-plugin
    It puts the ant-contrib dependency inside the plugin element and then uses maven.plugin.classpath as classpath reference. It prevents you from having to provide the full path (including version) to your local Maven repository.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>