Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用文件名作为参数的纯ant为文件集中的每个文件调用任务(无contrib任务,无自己的任务)_Ant - Fatal编程技术网

使用文件名作为参数的纯ant为文件集中的每个文件调用任务(无contrib任务,无自己的任务)

使用文件名作为参数的纯ant为文件集中的每个文件调用任务(无contrib任务,无自己的任务),ant,Ant,问题: 如果您的文件遵循特定的命名约定(例如,包含区域设置),并且需要通过ant脚本对每个文件进行处理,那么ant中有哪些可能性 例如: 要处理的文件: 文件_en-US.txt 文件_en-GB.txt 应为所有文件调用一个任务,并将其文件名作为参数。文件名中包含的区域设置需要由正则表达式提取,并作为参数传递给外部工具。一旦你有了文件名,解压过程就会很简单 限制: 纯Ant(1.7),不允许任何扩展,不允许像foreach这样的自定义/contrib任务。每安装一个vanilla an

问题:

如果您的文件遵循特定的命名约定(例如,包含区域设置),并且需要通过ant脚本对每个文件进行处理,那么ant中有哪些可能性

例如:

要处理的文件:

  • 文件_en-US.txt
  • 文件_en-GB.txt
应为所有文件调用一个任务,并将其文件名作为参数。文件名中包含的区域设置需要由正则表达式提取,并作为参数传递给外部工具。一旦你有了文件名,解压过程就会很简单

限制:

  • 纯Ant(1.7),不允许任何扩展,不允许像foreach这样的自定义/contrib任务。每安装一个vanilla ant(1.7)就需要运行一个

我非常确定,没有一种方法可以在不创建自己的任务或使用自己的任务的情况下为每个文件通用地调用任务。但是,有一些方法可以让ant自动检索ant contrib(或自定义jar),以便获得所需的结果。这是所使用的方法,具有类似的要求(因此任何人都很容易为开发做出贡献)

步骤1:下载ApacheIvy

ApacheIvy可用于检索依赖项,如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>
步骤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>
<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管理其他依赖项,那么您可以跳过上面的大部分内容,只需使用a下载
ant contrib
,就像上面下载Ivy一样。

它并没有完全回答我的问题,但它为我的问题提供了一个很好的解决方案/替代方案!谢谢
<target name="retrieve" description="retrieve dependancies with ivy" depends="-load-ivy">
    <ivy:retrieve />
    <ivy:artifactproperty name="[module].[artifact]" value="lib/[artifact]-[revision].[ext]" />
</target>
<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>
<target name="mytarget" depends="-load-ant-contrib">
    <for param="file">
        <fileset dir="somedir" includes="..." />
        <sequential>
            <!-- do stuff with @{file} -->
        </sequential>
    </for>
</target>