java - 如何获取文件名并将其设置为 Ant 中的属性?

标签 java ant

我需要扫描文件夹中的文件并在 Ant 中将属性设置为文件名,以便稍后使用。 例如,在 Jenkins 文件夹下有一个 test123.tar。我需要使用 test*.tar 来匹配这个文件,然后将名为“filename”的属性设置为 test123.tar 有可能做到吗? 非常感谢!

最佳答案

你可以使用 pathconvert将文件集转换为文件列表,然后 loadresourcefilterchain从列表中提取单个所需值。

<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/

相关文章:

c++ - MinGW 生成 exe,当在不同系统上运行时生成 "not compatible with the version of Windows"

java - 使用 Ant 的 Jacoco 覆盖和报告任务

java - 使用 ant 将 list 属性添加到现有的 jar 文件

java - 如何接收非实体类作为 POST 方法的参数?

java - 使用 Apache Cayenne 向数据库表执行批量插入

java - 我应该如何在 Selenium 的一种方法中检查 5 次有效登录?

gradle - 在Gradle构建中使用Ant taskdef和loaderref

java - 堆算法。非常基本,关于数组位置 0 和 1。

c# - JAVA中Base64+XOR+UTF8加密,C#中解密

java - 如何显示 Groovy AntBuilder 类路径中包含的所有资源?