3

我需要编写一个 Ant 脚本来加载一个属性文件,从中读取一个属性。值(多行)类似于:

path/to/file1a;path/to/file1b,
path/to/file2a;path/to/file2b,
..
..

我需要遍历每一行,并执行如下所示的 shell 命令:

myCommand -param1 path/to/file1a -param2 path/to/file1b  #Command inside a single iteration

我已经能够弄清楚如何循环:

<for list="${ValueFromPropertyFile}" param="a">  
    <sequential>
        <exec executable="myCommand">
            <arg value="-param1" />
            <arg value="----  split(@{a}, ";")[0]  ----" />
            <arg value="-param2" />
            <arg value="----  split(@{a}, ";")[1]  ----" />
        </exec>
    </sequential>
</for>

在我看来,这是一项非常简单的任务。我尝试搜索,但没有任何成功。

如果有人可以帮助我解决这个问题,或者将我指向相关文件,我将不胜感激。

非常感谢,

普拉提克

4

1 回答 1

2

您的假设存在几个问题:

  1. 输入文件的格式不是标准的 Java 属性文件,因此无法使用 ANT 中的标准loadproperties 任务加载。
  2. ANT 不是一种编程语言。您引用的“for”任务不是核心 ANT 的一部分(需要第 3 方 ant-contrib.jar)

所以我建议使用嵌入式脚本来解决您的问题。

例子

该项目是自我记录的:

$ ant -p
Buildfile: /home/mark/tmp/build.xml

    This is a demo project answering the following stackoverflow question:
    http://stackoverflow.com/questions/14625896

    First install 3rd party dependencies and generate the test files

        ant bootstrap generate-test-files

    Then run the build

        ant

    Expect the following output

        parse-data-file:
            [exec] build/myCommand -param1 path/to/file1a -param2 path/to/file1b
            [exec] build/myCommand -param1 path/to/file2a -param2 path/to/file2b

构建.xml

<project name="demo" default="parse-data-file">

    <description>
    This is a demo project answering the following stackoverflow question:
    http://stackoverflow.com/questions/14625896

    First install 3rd party dependencies and generate the test files

        ant bootstrap generate-test-files

    Then run the build

        ant

    Expect the following output

        parse-data-file:
            [exec] build/myCommand -param1 path/to/file1a -param2 path/to/file1b
            [exec] build/myCommand -param1 path/to/file2a -param2 path/to/file2b

    </description>

    <target name="bootstrap" description="Install 3rd party dependencies">
        <mkdir dir="${user.home}/.ant/lib"/>
        <get dest="${user.home}/.ant/lib/groovy-all.jar" src="http://search.maven.org/remotecontent?filepath=org/codehaus/groovy/groovy-all/2.1.0/groovy-all-2.1.0.jar"/>
    </target>

    <target name="generate-test-files" description="Generate the input data and sample script">
        <echo file="build/input.txt">path/to/file1a;path/to/file1b,
path/to/file2a;path/to/file2b,</echo>

        <echo file="build/myCommand"> #!/bin/bash
echo $0 $*</echo>

        <chmod file="build/myCommand" perm="755"/>
    </target>

    <target name="parse-data-file" description="Parse data file">
        <taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy"/>

        <groovy>
            new File("build/input.txt").eachLine { line ->
                def fields = line.split(/[;,]/)

                ant.exec(executable:"build/myCommand") {
                    arg(value:"-param1")
                    arg(value:fields[0])
                    arg(value:"-param2")
                    arg(value:fields[1])
                }
            }
        </groovy>
    </target>

    <target name="clean" description="Cleanup build files">
        <delete dir="build"/>
    </target>

</project>
于 2013-01-31T21:52:33.273 回答