使用普通蚂蚁调用文件集中每个文件的任务,文件名作为参数(没有贡献任务,没有自己的任务)

and*_*oid 5 ant

问题:

如果您的文件遵循某种命名约定(例如包括语言环境)并且需要通过ant脚本在每个文件的基础上进行处理,那么ant中有哪些可能性?

例:

要处理的文件:

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

应该为所有文件调用一个任务,并将其文件名作为参数.文件名中包含的语言环境需要由正则表达式提取并作为参数传递给外部工具.只要有文件名,提取就会直截了当.

限制:

  • 普通的Ant(1.7),没有扩展允许没有像foreach这样的自定义/贡献任务.需要运行每个香草蚂蚁(1.7)安装.

Tom*_*ard 4

我很确定没有一种方法可以在不创建自己的自定义任务或不使用ant-contrib 的情况下为每个文件通用调用任务。然而,有一些方法可以让 ant 自动检索 ant-contrib (或自定义 jar),这样您就可以获得所需的结果。这是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>
Run Code Online (Sandbox Code Playgroud)

步骤2:添加ant-contrib依赖

ivy 使用“ivy 文件”来指定依赖项。您可以按如下方式添加 ant-contrib

    <dependency org="ant-contrib" name="ant-contrib" rev="1.0b3" transitive="false"/>
Run Code Online (Sandbox Code Playgroud)

完整的 ivy 文件可能看起来像

<?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>
Run Code Online (Sandbox Code Playgroud)

您还可以使用echoxml从 ant 脚本动态输出此文件

步骤 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>
Run Code Online (Sandbox Code Playgroud)

第四步:加载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>
Run Code Online (Sandbox Code Playgroud)

第 5 步:创建使用以下命令迭代文件的目标for

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

如果你只需要下载ant-contrib,不想使用ivy来管理其他依赖,那么你可以跳过上面的大部分内容,只需要像上面下载ivy一样使用get来下载。ant-contrib