gpt4 book ai didi

ant - 如何使用 FileSet 在 ant 中选择子目录?

转载 作者:行者123 更新时间:2023-12-04 14:31:44 25 4
gpt4 key购买 nike

我正在使用 ant 1.6.2 并尝试设置一个任务,该任务将比较源目录和目标目录,识别源目录中存在的所有子目录并删除目标目录中喜欢的命名子目录。

因此,假设源目录中有子目录 sub1、sub2 和 sub3,而目标目录中有 sub1、sub2、sub3 和 sub4,那么我想从目标目录中删除 sub1、sub2 和 sub3。

我想我可以通过使用 FileSelector 来识别源中存在于目标中的所有目录来做到这一点。但是,我无法让 文件选择器返回目录的匹配项。

最终,我想我会做这样的事情:

<fileset id="dirSelector" dir="${install.dir}">
<type type="dir"/>
<present targetdir="${dist.dir}"/>
</fileset>

我首先尝试列出源目录中存在的目录并将它们打印出来:
<fileset id="dirSelector" dir="${install.dir}">
<type type="dir"/>
</fileset>
<property name="selected" refid="dirSelector" />
<echo>Selected: ${selected}</echo>

但是,我从来没有在类型选择器设置为目录的情况下打印任何内容。如果我将类型更改为文件,我会打印出文件。

有没有更好的方法来完成我想要做的事情?我在使用类型选择器时遗漏了什么吗?

最佳答案

如果不编写自定义 Ant 任务,这会有点困惑。如果您乐于使用 ant-contrib,以下应该可以解决问题。图书馆。这有点黑客(尤其是它使用属性的方式),但它似乎工作正常。

<project name="stackoverflow" default="delete_target_dirs">

<taskdef resource="net/sf/antcontrib/antlib.xml">
<classpath>
<pathelement location="ant-contrib-1.0b3.jar"/>
</classpath>
</taskdef>

<property name="src.dir" value="src"/>
<property name="target.dir" value="target"/>

<target name="delete_target_dirs">

<for param="file">
<path>
<dirset dir="${src.dir}">
<include name="**"/>
</dirset>
</path>

<sequential>
<basename property="@{file}_basename" file="@{file}" />
<available property="@{file}_available" file="${@{file}_basename}" filepath="${target.dir}" />
<if>
<equals arg1="${@{file}_available}" arg2="true"/>
<then>
<delete verbose="true">
<dirset dir="${target.dir}" includes="${@{file}_basename}"/>
</delete>
</then>
</if>
</sequential>
</for>

</target>

</project>

关于ant - 如何使用 FileSet 在 ant 中选择子目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2142479/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com