Search code examples
javaantant-contrib

Ant using antlib with namespace


I am using the following demonstration script:

<?xml version="1.0" encoding="UTF-8"?>
<project name="test" basedir="." xmlns:deploy="antlib:net.sf.antcontrib">
    <target name="default">
        <taskdef resource="net/sf/antcontrib/antlib.xml">
            <classpath>
                <pathelement location="lib/ant-contrib-1.0b3.jar"/>
            </classpath>
        </taskdef>
        <deploy:if>
            <isset property="defaultprop"/>
            <then>
                <echo message="it's set!"/>
            </then>
        </deploy:if>
    </target>
</project>

When I run this build script (with target default), the error is

build.xml:9: Problem: failed to create task or type antlib:net.sf.antcontrib:if

The pathelement lib/ant-contrib-1.0b3.jar exists, and ant is picking it up. I'm thinking the problem is how I'm using the xmlns. I'm taking this from another example that I have that also doesn't work for me (it works on a particular server, though!), and trying to figure out what the magic sauce is.


Solution

  • Your taskdef where you're adding ant-contrib needs to declare a URI the same as the namespace you're defining and prefixing in the project. Similar to how the taskdef over here works.

    <project name="test" basedir="." xmlns:deploy="antlib:net.sf.antcontrib">
        <target name="default">
            <taskdef uri="antlib:net.sf.antcontrib" resource="net/sf/antcontrib/antlib.xml">
                <classpath>
                    <pathelement location="lib/ant-contrib-1.0b3.jar"/>
                </classpath>
            </taskdef>
            <deploy:if>
                <isset property="defaultprop"/>
                <then>
                    <echo message="it's set!"/>
                </then>
            </deploy:if>
        </target>
    </project>