我需要扫描文件夹中的文件并在 Ant 中将属性设置为文件名,以便稍后使用。 例如,在 Jenkins 文件夹下有一个 test123.tar。我需要使用 test*.tar 来匹配这个文件,然后将名为“filename”的属性设置为 test123.tar 有可能做到吗? 非常感谢!
最佳答案
你可以使用 pathconvert将文件集转换为文件列表,然后 loadresource用filterchain从列表中提取单个所需值。
<project default="test">
<target name="test">
<!-- read your fileset into a property formatted as a list of lines -->
<pathconvert property="file.list" pathsep="${line.separator}">
<map from="${basedir}${file.separator}" to=""/>
<fileset dir="${basedir}">
<include name="test*.tar"/>
</fileset>
</pathconvert>
<!-- extract a single target file from the list -->
<loadresource property="file.name">
<string value="${file.list}"/>
<filterchain>
<!-- add your own logic to deal with multiple matches -->
<headfilter lines="1"/>
</filterchain>
</loadresource>
<!-- print the result -->
<echo message="file.name: ${file.name}"/>
</target>
</project>
输出:
$ ls test*.tar
test012.tar test123.tar testabc.tar
$
$ ant
Buildfile: C:\tmp\ant\build.xml
test:
[echo] file.name: test012.tar
BUILD SUCCESSFUL
Total time: 0 seconds
详细输出:
$ ant -v
test:
[pathconvert] Set property file.list = test012.tar
[pathconvert] test123.tar
[pathconvert] testabc.tar
[loadresource] loading test012.tar
[loadresource] test123.tar
[loadresource] testabc.tar into property file.name
[loadresource] loaded 13 characters
[echo] file.name: test012.tar
BUILD SUCCESSFUL
Total time: 0 seconds
关于java - 如何获取文件名并将其设置为 Ant 中的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9886802/