Memento ANT

Table des matières :

Exemple d'un fichier de configuration ANT [project - target - antcall]

<project name="test" default="all" basedir=".">
<target name="all"> <antcall target="initialise" /> <antcall target="compile" /> </target>
<target name="initialise" description="Initialise required settings."> <tstamp /> <echo message="Initialization"/> <property name="myProperty" value="toto" /> </target>
<!-- Compile the java code --> <target name="compile" description="Compile ..."> <echo message="Compilation"/> <!-- create a temp build directory --> <mkdir dir="${basedir}/build" /> <!-- compile the source --> <javac srcdir="${basedir}/src" destdir="${basedir}/build"> <classpath refid="build.classpath" /> </javac> </target> </project>

Configurer une tache externe (extern task) [taskdef]

<taskdef name="myTask" classname="my.package.MyClass" />
<taskdef resource="my/package/ant.properties" />

Transformations XSLT et ANT [task <xslt> et <style> - ant 1.5]

Utilisation des task <xslt> ou <style> pour transformer un fichier XML à l'aide d'un fichier XSL.

<xslt in="in.xml" style="style.xsl" out="out.html"/>
<style in="in.xml" style="style.xsl" out="out.html"/>
<style processor="trax" in="in.xml" style="style.xsl" out="out.html"/>
Manual for <xslt> and <style> (ant.apache.org)

Utiliser le plugin ant-contrib

Voir Ant-Contrib project.

To install ant-contrib:
  • Copy ant-contrib-x-x.jar to the lib directory of your Ant installation.
  • Copy this line to your project

<taskdef resource="net/sf/antcontrib/antcontrib.properties"/>
Or :
  • Keep ant-contrib-x-x.jar in a separate location.
  • and copy in your project

<taskdef resource="net/sf/antcontrib/antcontrib.properties"> <classpath> <pathelement location="/ant-contrib/lib/ant-contrib-x-x.jar"/> </classpath> </taskdef>

Utiliser des blocs conditionnels avec ANT [task if / else du projet ant-contrib]

Task from Ant-Contrib project.

If / else

<if> <equals arg1="${foo}" arg2="bar" /> <then> <echo message="The value of property foo is bar" /> </then> <else> <echo message="The value of property foo is not bar" /> </else> </if>

If / elseif

<if> <equals arg1="${foo}" arg2="bar" /> <then> <echo message="The value of property foo is 'bar'" /> </then> <elseif> <equals arg1="${foo}" arg2="foo" /> <then> <echo message="The value of property foo is 'foo'" /> </then> </elseif> <else> <echo message="The value of property foo is not 'foo' or 'bar'"/> </else> </if>

Utiliser For-each avec ANT [ant-contrib]

Task from Ant-Contrib project.

foreach with items

<taskdef resource="net/sf/antcontrib/antcontrib.properties"/> <target name="testForeach1"> <foreach list="toto,titi,tata" param="msg" target="myTask" /> </target>

foreach for files

<taskdef resource="net/sf/antcontrib/antcontrib.properties"/> <target name="testForeach2"> <foreach param="msg" target="myTask"> <path> <fileset dir="."> <include name="**/*.xml"/> </fileset> </path> </foreach> </target>

the target use in the foreach

<target name="myTask"> <echo message="${msg}" /> </target>

Traiter des chaines de caractères avec PropertyRegex [ant-contrib 0.4]

Task from Ant-Contrib project.

Extraire une partie d'une String

<taskdef resource="net/sf/antcontrib/antcontrib.properties"/> <target name="test"> <propertyregex property="pack.name" input="package.ABC.name" regexp="package\.([^\.]*)\.name" select="\1" casesensitive="false" /> <echo>${pack.name}</echo> </target>
Le résultat sera : ABC

Copier des fichiers [task copy]

Copy one file

<copy file="file.txt" toFile="newFile.txt" />

Copy a directory to another directory

<copy todir="../new/dir"> <fileset dir="src_dir"/> </copy>

Copy files

<copy todir="../dest/dir"> <fileset dir="src_dir"> <exclude name="**/*.java"/> </fileset> </copy>
Pour plus de détails, voir le manual Ant.

Concaténer des fichiers [task concat]

Concaténer des fichiers file-*.txt -> file.txt

<concat destfile="dest/file.txt"> <fileset dir="source" includes="file-*.txt"/> </concat>
Pour plus de détails, voir le manual Ant.

How I can pass a string from my ant task to a build.xml?

Using Ant properties:
ant -Dsome.prop=value
And in your build.xml, use ${some.prop} to get the value, for example:
<echo message="some.prop = ${some.prop}"/>

How do I access environment variables within Ant?

<property environment="env"/> provides all environment variables as Ant properties prefixed by "env.".
For example, CLASSPATH would be accessible in Ant as ${env.CLASSPATH}.

<property environment="env"/> <echo message="CLASSPATH = ${env.CLASSPATH}"/>

How do I get <exec> to work for shell commands on Windows platforms?

To <exec> a shell command on Windows, you must execute the command shell followed by the command you want.
For example: The '/c' is there to exit the spawned command shell after executing the specified command.

<exec executable="cmd"> <arg line="/c dir"/> </exec>


Article extrait du site Loribel.com.
https://loribel.com/info/memento/ant.html