背景:我从XML文件中读取名称,并希望将它们映射到构建任务的源和目标路径。我不是一位经验丰富的Ant用户,我正在寻找一种方法
示例xml:
<list><value>The first name<value><value>The second name</value></list>
所需的结果集:
${dir}/The first name.${ext}
${dir}/The second name.${ext}
我可以使用pathconvert
或mappedresources
构建每个文件的路径,但我还没有能够将结果映射回我可以在{{{}}中使用的文件资源集合{1}}。这个问题有一个优雅的解决方案吗?
答案 0 :(得分:2)
ANT不是一种编程语言。易于嵌入groovy。
├── build.xml
├── sample.xml
├── src
│ ├── file1.txt
│ ├── file2.txt
│ └── file3.txt
└── target
├── file1.txt
└── file2.txt
运行如下
$ ant
Buildfile: /.../build.xml
install-groovy:
build:
[copy] Copying 2 files to /.../target
[copy] Copying /.../src/file1.txt to /.../target/file1.txt
[copy] Copying /.../src/file2.txt to /.../target/file2.txt
BUILD SUCCESSFUL
<list>
<value>file1</value>
<value>file2</value>
</list>
<project name="demo" default="build">
<!--
================
Build properties
================
-->
<property name="src.dir" location="src"/>
<property name="src.ext" value="txt"/>
<property name="build.dir" location="target"/>
<available classname="org.codehaus.groovy.ant.Groovy" property="groovy.installed"/>
<!--
===========
Build targets
===========
-->
<target name="build" depends="install-groovy" description="Build the project">
<taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy"/>
<groovy>
def xmllist = new XmlSlurper().parse(new File("sample.xml"))
ant.copy(todir:properties["build.dir"], verbose:true, overwrite:true) {
fileset(dir:properties["src.dir"]) {
xmllist.value.each {
include(name:"${it}.${properties["src.ext"]}")
}
}
}
</groovy>
</target>
<target name="clean" description="Cleanup project workspace">
<delete dir="${build.dir}"/>
</target>
<target name="install-groovy" description="Install groovy" unless="groovy.installed">
<mkdir dir="${user.home}/.ant/lib"/>
<get dest="${user.home}/.ant/lib/groovy.jar" src="http://search.maven.org/remotecontent?filepath=org/codehaus/groovy/groovy-all/2.3.6/groovy-all-2.3.6.jar"/>
<fail message="Groovy has been installed. Run the build again"/>
</target>
</project>