The best way is to place the Ant -Contrib jarfile project in the project. For example, suppose build.xml is at the root of your project. Create a directory in your project named ant.lib\ant-contrib , then put ant-contrib*.jar in this folder. You can use this method for other optional Ant tasks that you may need (for example, Ivy, Findbugs, Cobrrtura, etc.).
Then in your build.xml you can do this:
<taskdef resource="net/sf/antcontrib/antlib.xml"> <classpath> <fileset dir="${basedir}/ant.lib/ant-contrib"/> </classpath> </taskdef>
I like to do it this way because additional options with tasks are included in the project. If you check everything in your version control system, someone can check your code and build without downloading Ant -Contrib and installing it yourself.
You can define XML namespaces. This gives your Ant -Contrib tasks a prefix to avoid task name conflicts if you use other optional Ant tasks that have the same task name. In addition, it warns users that this is not a standard Ant task.
If you are using an XML namespace, you need to place the XMLNS declaration in the <project> header. This will contain a URI that will connect your Ant Contrib tasks to an XML namespace. For example, the ac: namespace is for all Ant Contrib tasks:
<project name="my.project" default="package" basedir="." xmlns:ac="http://ant-contrib.sourceforge.net"> <taskdef resource="net/sf/antcontrib/antlib.xml" uri="http://ant-contrib.sourceforge.net"> <classpath> <fileset dir="${basedir}/ant.lib/ant-contrib"/> </classpath> </taskdef>
This means that the XML namespace (xmlns) ac matches the URI of http://ant-contrib.sourceforge.net . URI can be anything. For instance:
<project name="my.project" default="package" basedir="." xmlns:ac="hamburger:with-fries"> <taskdef resource="net/sf/antcontrib/antlib.xml" uri="hamburger:with-fries"> <classpath> <fileset dir="${basedir}/ant.lib/ant-contrib"/> </classpath> </taskdef>
The standard should use something like antlib:net.sf.antcontrib :
<project name="my.project" default="package" basedir="." xmlns:ac="antlib:net.sf.antcontrib"> <taskdef resource="net/sf/antcontrib/antlib.xml" uri="antlib:net.sf.antcontrib"> <classpath> <fileset dir="${basedir}/ant.lib/ant-contrib"/> </classpath> </taskdef>
However, I like to use the project url. Thus, if someone wants documentation on Ant -Contrib tasks, they know the URL where the Ant -Contrib project lives.
In all three cases described above, I defined the XML namespace using ac . Therefore, you must prefix all Ant -Contrib task names with ac: You can use antcontrib or whatever. In the ac: namespace ac: your Ant -contrib tasks will look like this:
<ac:if> <istrue value="${include.debug.code}"/> <ac:then> [...] </ac:then> <ac:else> [...] </ac:else> <ac:if>
If you miss the whole thing of the namespace, you can simply use Ant -Contrib tasks as described:
<if> <istrue value="${include.debug.code}"/> <then> [...] </then> <else> [...] </else>