Calling a task for each file in a set of files with a simple ant with the file name as a parameter (without tabs, without own tasks)

Problem:

If you have files that match a specific naming convention (e.g. locale) and need to be processed based on each file using an ant script, what are the options in ant?

Example:

Files for processing:

  • file_en-US.txt
  • file_en-GB.txt
  • ...

The task should be called for all files with their file name as a parameter. The locale contained in the file name must be extracted by the regular expression and passed to the external tool as a parameter. The selection will be direct as soon as you have the file name.

Limitations:

  • Ant (1.7), custom/contrib, foreach. Ant (1.7).
+5
1

, , ant-contrib. ant, ant -contrib ( ), . , TIBant, ( ),

1: Apache Ivy

Apache Ivy , ant -contrib. ant Ivy

<property name="ivy.install.version" value="2.2.0" />
<property name="ivy.jar.dir" location="${user.home}/.ivy2/jars" />
<property name="ivy.jar.file" location="${ivy.jar.dir}/ivy-${ivy.install.version}.jar" />

<target name="-download-ivy" unless="ivy.downloaded">
    <mkdir dir="${ivy.jar.dir}" />
    <!-- download Ivy from web site so that it can be used even without any special installation -->
    <echo message="installing ivy..." />
    <get src="http://repo1.maven.org/maven2/org/apache/ivy/ivy/${ivy.install.version}/ivy-${ivy.install.version}.jar"
         dest="${ivy.jar.file}"
         usetimestamp="true"
         verbose="true" />
</target>

<target name="-check-ivy-downloaded">
    <condition property="ivy.downloaded">
        <and>
            <available file="${ivy.jar.file}" />
            <available file="${ivy.jar.dir}/jsch-0.1.44-1.jar" />
        </and>
    </condition>
</target>

<target name="-load-ivy" depends="-check-ivy-downloaded,-download-ivy" unless="ivy.loaded">
    <path id="ivy.lib.path">
        <fileset dir="${ivy.jar.dir}" includes="*.jar" />
    </path>
    <taskdef resource="org/apache/ivy/ant/antlib.xml" uri="antlib:org.apache.ivy.ant" classpathref="ivy.lib.path" />
    <property name="ivy.loaded" value="true" />
</target>

2: ant -contrib

Ivy " " . ant -contrib

    <dependency org="ant-contrib" name="ant-contrib" rev="1.0b3" transitive="false"/>

<?xml version="1.0" encoding="ISO-8859-1"?>
<ivy-module version="2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ant.apache.org/ivy/schemas/ivy.xsd">
    <info
    organisation="org.my"
    module="mymodule"
    status="release"/>

    <dependencies>
        <dependency org="ant-contrib" name="ant-contrib" rev="1.0b3" transitive="false"/>
    </dependencies>
</ivy-module>

ant script echoxml

3.

Ivy ant-contrib ( , )

<target name="retrieve" description="retrieve dependancies with ivy" depends="-load-ivy">
    <ivy:retrieve />
    <ivy:artifactproperty name="[module].[artifact]" value="lib/[artifact]-[revision].[ext]" />
</target>

4: ant-contrib

<target name="-load-ant-contrib" depends="retrieve" unless="ant.contrib.loaded">
    <taskdef resource="net/sf/antcontrib/antlib.xml">
        <classpath>
            <pathelement location="${ant-contrib.ant-contrib}" />
        </classpath>
    </taskdef>
    <property name="ant.contrib.loaded" value="true" />
</target>

5. , for

<target name="mytarget" depends="-load-ant-contrib">
    <for param="file">
        <fileset dir="somedir" includes="..." />
        <sequential>
            <!-- do stuff with @{file} -->
        </sequential>
    </for>
</target>

ant -contrib Ivy , get, ant-contrib , , Ivy.

+4

All Articles