Antlib ファイルは、ルート要素が antlib である xml ファイルです。Antlib の要素は Apache Ant 定義タスク(Taskdef など)または org.apache.tools.ant.taskdefs.AntlibDefinition を拡張する任意の Ant タスクです。
このタスクを実行する Ant にバンドルされている現在の宣言セットは次のとおりです。
タスクと型のグループは、Antlib ファイル内でまとめて定義できます。たとえば、ファイル sample.xml には以下が含まれています。
<?xml version="1.0"?>
<antlib>
<typedef name="if" classname="org.acme.ant.If"/>
<typedef name="scriptpathmapper"
classname="org.acme.ant.ScriptPathMapper"
onerror="ignore"/>
<macrodef name="print">
<attribute name="file"/>
<sequential>
<concat taskname="print">
<fileset dir="." includes="@{file}"/>
</concat>
</sequential>
</macrodef>
</antlib>
2 つのタイプまたはタスクである if と scriptpathmapper を定義します。この antlib ファイルは、ビルドスクリプトで次のように使用できます。
<typedef file="sample.xml"/>
<typedef> の他の属性も使用できます。たとえば、sample.xml がクラスも格納した jar ファイル sample.jar 内にあると仮定すると、次のビルドフラグメントは if と scriptpathmapper タスク/型を定義し、それらを名前空間 URI samples:/acme.org に配置します。
<typedef resource="org/acme/ant/sample.xml"
uri="samples:/acme.org"/>
この定義は、次のように使用できます。
<sample:if valuetrue="${props}" xmlns:sample="samples:/acme.org">
<sample:scriptpathmapper language="beanshell">
some bean shell
</sample:scriptpathmapper>
</sample:if>
パターン antlib:java.package を持つ名前空間 URI は特別扱いされます。
Ant は、このパターンを持つ名前空間 URI を持つ要素が発生すると、デフォルトのクラスパスのパッケージディレクトリ内に antlib.xml という名前のリソースがあるかどうかを確認します。
たとえば、ファイル antcontrib.jar がディレクトリ ${ant.home}/lib に配置されており、すべての Antcontrib の定義が定義されているリソース net/sf/antcontrib/antlib.xml が含まれていると仮定すると、次のビルドファイルは自動的に antcontrib の定義を場所 HERE に読み込みます。
<project default="deletetest" xmlns:antcontrib="antlib:net.sf.antcontrib">
<macrodef name="showdir">
<attribute name="dir"/>
<sequential>
<antcontrib:shellscript shell="bash"> <!-- HERE -->
ls -Rl @{dir}
</antcontrib:shellscript>
</sequential>
</macrodef>
<target name="deletetest">
<delete dir="a" quiet="yes"/>
<mkdir dir="a/b"/>
<touch file="a/a.txt"/>
<touch file="a/b/b.txt"/>
<delete>
<fileset dir="a"/>
</delete>
<showdir dir="a"/>
</target>
</project>
将来のバージョンの Ant では、リソースがデフォルトのクラスパスにあるという要件は削除される可能性があります。
Antlib をローカルの Ant インストールから分離したい場合(たとえば、その jar をプロジェクトの SCM システムに保持する場合など)、Ant がその jar を見つけられるようにクラスパスを指定する必要があります。最善の解決策は、<taskdef> で antlib を読み込むことです。
<project xmlns:antcontrib="antlib:net.sf.antcontrib"> <taskdef uri="antlib:net.sf.antcontrib" resource="net/sf/antcontrib/antlib.xml" classpath="path/to/ant-contrib.jar"/> <target name="iterate"> <antcontrib:for param="file"> <fileset dir="."/> <sequential> <echo message="- @{file}"/> </sequential> </antcontrib:for> </target> </project>
Antlib 内で定義された定義は、Antlib 内で使用できます。ただし、定義が配置される名前空間は、Antlib を使用する <typedef> に依存します。この問題に対処するため、定義は、antlib 実行中は名前空間 URI ant:current に配置されます。たとえば、次の antlib はタスク <if>、タイプ <isallowed>、およびそのタスクとタイプを利用したマクロ <ifallowed> を定義します。
<antlib xmlns:current="ant:current">
<taskdef name="if" classname="org.acme.ant.If"/>
<typedef name="isallowed" classname="org.acme.ant.Isallowed"/>
<macrodef name="ifallowed">
<attribute name="action"/>
<element name="do"/>
<sequential>
<current:if>
<current:isallowed test="@{action}"/>
<current:then>
<do/>
</current:then>
</current:if>
</sequential>
</macrodef>
</antlib>
Antlib は他の Antlib を使用できます。
Antlib で定義された名前は、呼び出し元 <typedef> または自動要素解決によって指定された名前空間 URI にあるため、呼び出し側が名前空間 URI を使用する場合、コア Ant タイプとタスクの名前を再利用できます。たとえば、次の antlib はさまざまなタスクのデフォルトを定義するために使用できます。
<antlib xmlns:antcontrib="antlib:net.sf.antcontrib">
<presetdef name="javac">
<javac deprecation="${deprecation}"
debug="${debug}"/>
</presetdef>
<presetdef name="delete">
<delete quiet="yes"/>
</presetdef>
<presetdef name="shellscript">
<antcontrib:shellscript shell="bash"/>
</presetdef>
</antlib>
これは次のように使用できます。
<project xmlns:local="localpresets">
<typedef file="localpresets.xml" uri="localpresets"/>
<local:shellscript>
echo "hello world"
</local:shellscript>
</project>