来自ant manual basename:"When this task executes, it will set the specified property to the value of the last path element of the specified file"
一旦设置的属性在 vanilla ant 中是不可变的,因此在 for 循环中使用 basename 任务时,属性“名称”保存第一个文件的值。
因此必须使用 unset="true" 的 antcontrib var 任务:
<target name="store">
<for param="file">
<path>
<fileset dir="." includes="*.xqm"/>
</path>
<sequential>
<var name="name" unset="true"/>
<basename file="@{file}" property="name" />
<echo message="@{file}, ${name}"/>
</sequential>
</for>
</target>
在使用 Ant 1.8.x 或更高版本时,也可以使用 local task:
<target name="store">
<for param="file">
<path>
<fileset dir="." includes="*.xqm"/>
</path>
<sequential>
<local name="name"/>
<basename file="@{file}" property="name" />
<echo message="@{file}, ${name}"/>
</sequential>
</for>
</target>
最后你可以使用 Ant Flaka 代替 antcontrib :
<project xmlns:fl="antlib:it.haefelinger.flaka">
<fl:install-property-handler />
<fileset dir="." includes="*.xqm" id="foobar"/>
<!-- create real file objects and access their properties -->
<fl:for var="f" in="split('${toString:foobar}', ';')">
<echo>
#{ format('filename %s, last modified %tD, size %s bytes', f.tofile.toabs,f.tofile.mtime,f.tofile.size) }
</echo>
</fl:for>
<!-- simple echoing the basename -->
<fl:for var="f" in="split('${toString:foobar}', ';')">
<echo>#{f}</echo>
</fl:for>
</project>