Ant脚本 - 分割字符串并通过索引访问

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>

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

如果有人能帮助我或指向相关文档,我将不胜感激。

非常感谢,

Pratik

1个回答

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

因此,我建议使用嵌入式脚本来解决您的问题。

示例

该项目是自文档化的:

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

    This is a demo project answering the following stackoverflow question:
    https://dev59.com/7m3Xa4cB1Zd3GeqPj-Zj

    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:
    https://dev59.com/7m3Xa4cB1Zd3GeqPj-Zj

    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>

在我们的项目中,我们使用BeanShell,它看起来更像原始的Java。对于Groovy的使用给予加分。 - Dante WWWW
嘿,谢谢回答。如你所建议的,我在属性文件中进行了更改并安装了Groovy,现在正在使用它。您提供的解决方法很好用,但它会将每一行进行标记化。我尝试了各种分号分割方法,但是都没成功。有什么建议吗? - Pratik Pandey
@PratikPandey 答案已经修订并附带测试。我认为您需要提供更多关于您的错误具体信息。 - Mark O'Connor

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接