Ant脚本 - 通过索引拆分字符串和访问

时间:2013-01-31 12:32:23

标签: arrays string ant indexing split

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

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

我需要迭代每一行,并执行一个看起来像的命令:

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>

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

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

非常感谢,

PRATIK

1 个答案:

答案 0 :(得分:2)

您的假设存在一些问题:

  1. 输入文件的格式不是标准的Java属性文件,因此无法使用ANT中的标准loadproperties task加载。
  2. ANT不是一种编程语言。您引用的“for”任务不是核心ANT的一部分(需要第三方ant-contrib.jar)
  3. 所以我建议使用嵌入式脚本来解决您的问题。

    实施例

    该项目是自我记录的:

    $ 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
    

    的build.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>