Monday, October 29, 2012

Ant with maven dependency support - using maven-ant-tasks

Got hold of a nice book recently - Harnessing Hibernate from Oreilly.  While reading that, came to know about this ant task.  It would of immense help when you have an existing ant build and are in the process of migrating to Maven.  Using this task we can continue to use our build script and have all dependencies taken care by Maven.

Official page for Maven-Ant-Tasks

One can use the get task of Ant to dynamically download the jar and then include it in the build:

<get src="http://www.apache.org/dyn/closer.cgi/maven/binaries/maven-ant-tasks-2.1.3.jar"
  dest="${lib}"
  verbose="true"
  usetimestamp="true" />



A straight copy from above site:


<project ... xmlns:artifact="antlib:org.apache.maven.artifact.ant">
  ...
  <path id="maven-ant-tasks.classpath" path="lib/maven-ant-tasks-2.1.3.jar" />
  <typedef resource="org/apache/maven/artifact/ant/antlib.xml"
           uri="antlib:org.apache.maven.artifact.ant"
           classpathref="maven-ant-tasks.classpath" />
  ...
</project>


Once setup, declaring dependencies is very easy.  Look at the usage page to know how to see how to configure repositories, reference your settings.xml from local maven install.  But note that to use this task you do not need maven to be installed at all!!  It downloads the artifacts to user.home/.m2 folder.