我需要编写一个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
答案 0 :(得分:2)
您的假设存在一些问题:
所以我建议使用嵌入式脚本来解决您的问题。
该项目是自我记录的:
$ 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
<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>