Boostrapping Maven's Antlib

by 3/20/2007 08:24:00 AM 0 comments

Boostrapping Maven's Antlib

Maven repositories can be used from Ant build scripts using Maven's antlib. To do this you must have
both Maven and
Ant installed. When using non-standard Ant extensions it's much easier on your fellow developers if you have your build script bootstrap the use of the libraries so that they don't have to manually install the Ant extensions. Below is an example of how to bootstrap Maven's antlib and fetch all the dependencies, including transitive ones, declared in a Project Object Model (i.e. pom.xml) and copy them to the projects lib directory.
For this example, in addition to your build.xml file, you will need to create a simple pom.xml that defines your dependencies. Here's an example pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
    <name>EITS Database Tools</name>
To bootstrap antlib into your projects (so users can build without having to install antlib) modify your build.xml file like so:
<?xml version="1.0" ?>
<!-- $Id: build.xml,v 1.21 2007/01/05 01:04:43 brian Exp $ -->

    <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
         Install the Maven artifact and configure project properties

    <property name="artifact.jar.dir" value="${basedir}/.ant" />
    <property name="artifact.jar.file" value="${artifact.jar.dir}/maven.artifact-ant-2.0.2-dep.jar" />
    <mkdir dir="${artifact.jar.dir}"/>
       download maven antlib from web site so that it can be used even without any
       special installation
    <available file="${artifact.jar.file}" property="artifact.exists"/>
    <echo message="Installing Maven Antlib..."/>
    <get src=""
         dest="${artifact.jar.file}" usetimestamp="true" ignoreerrors="${artifact.exists}"/>

    <typedef resource="org/apache/maven/artifact/ant/antlib.xml" uri="antlib:org.apache.maven.artifact.ant">
            <pathelement location="${artifact.jar.file}" />
        Configure properties based on pom.xml and typical Maven build structure.
        Modify this for your own project structure
    <artifact:pom id="pom" file="pom.xml" />
    <property name="version" value="${pom.version}" />
    <property name="project" value="${pom.artifactId}" />
    <property name="build" value="${}" />
    <property name="lib" value="${build}/lib" />

    <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        Sets up the project for build

    <target name="init" description="Create the build directory structure">
        <!-- Create the timestamp -->
        <!-- Create the directory build structure used by compile -->
        <mkdir dir="${build}"/>
        <mkdir dir="${lib}"/>
        <!-- Copy dependencies to target/lib -->
        <echo>Finding dependencies of ${pom.artifactId}-${version}</echo>
        <artifact:dependencies filesetId="dependency.fileset" useScope="runtime">
            <pom refid="pom"/>
        <copy todir="${lib}" verbose="true">
            <fileset refid="dependency.fileset" />
            <mapper type="flatten" />

Brian Schlining


Cras justo odio, dapibus ac facilisis in, egestas eget quam. Curabitur blandit tempus porttitor. Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor.