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"?>
<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>eits</groupId>
    <artifactId>eits-db</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>EITS Database Tools</name>
    <url>http://oceana.shore.mbari.org/projects/${project.name}</url>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.0.4</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.3.0</version>
            <scope>runtime</scope>
        </dependency>
    </dependencies>
</project>
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 $ -->
<project 
    name="YourProjectName" 
    default="init" 
    basedir="." 
    xmlns:artifact="antlib:org.apache.maven.artifact.ant">
    
    ... 

    <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
         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="http://www.apache.org/dist/maven/binaries/maven-artifact-ant-2.0.2-dep.jar"
         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">
        <classpath>
            <pathelement location="${artifact.jar.file}" />
        </classpath>
    </typedef>
    
    <!-- 
        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="${pom.build.directory}" />
    <property name="lib" value="${build}/lib" />

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

    -->
    <target name="init" description="Create the build directory structure">
        <!-- Create the timestamp -->
        <tstamp/>
        
        <!-- 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"/>
        </artifact:dependencies>
        <copy todir="${lib}" verbose="true">
            <fileset refid="dependency.fileset" />
            <mapper type="flatten" />
        </copy>
            
        ...
    </target>

hohonuuli

Developer

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

0 comments: