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

Arrays Ant脚本--通过索引拆分字符串和访问,arrays,string,ant,indexing,split,Arrays,String,Ant,Indexing,Split,我需要编写一个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 我已经能够想出如何循环: <fo

我需要编写一个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>

在我看来,这是一项相当简单的任务。我试着寻找,但没有成功

如果有人能帮我解决这个问题,或者给我指一份相关的文件,我将不胜感激

非常感谢,


Pratik

您的假设存在两个问题:

  • 输入文件的格式不是标准的Java属性文件,因此无法使用ANT中的标准加载它
  • ANT不是一种编程语言。您引用的“for”任务不是核心ANT的一部分(需要第三方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
    
    编译文件
    
    这是一个演示项目,回答了以下问题:
    http://stackoverflow.com/questions/14625896
    首先安装第三方依赖项并生成测试文件
    ant引导生成测试文件
    然后运行构建
    蚂蚁
    预期以下输出
    解析数据文件:
    [exec]build/myCommand-param1 path/to/file1a-param2 path/to/file1b
    [exec]build/myCommand-param1 path/to/file2a-param2 path/to/file2b
    路径/to/file1a;路径/to/file1b,
    路径/to/file2a;路径/to/file2b,
    #!/bin/bash
    回声0美元$*
    新文件(“build/input.txt”).eachLine{line->
    def fields=line.split(/[;,]/)
    exec(可执行文件:“build/myCommand”){
    参数(值:“-param1”)
    参数(值:字段[0])
    参数(值:“-param2”)
    参数(值:字段[1])
    }
    }
    
    +1用于groovy用法。。我们在我们的项目中使用BeanShell,它看起来更像原始的Java.hey thanx 4 ans。正如你所建议的,我在属性文件中做了更改,n安装groovy和nw使用它..d sol u提供的工作非常好,bt将其标记化每一行..我尽了最大努力2将其拆分;我所有的努力都白费了suggestions@PratikPandey答案是通过测试修改的。我认为你需要提供更多关于你的错误的细节。
    <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>