【问题标题】:Can I conditionally stop an Ant script based on file last modified time?我可以根据文件上次修改时间有条件地停止 Ant 脚本吗?
【发布时间】:2023-03-04 21:16:01
【问题描述】:
我一直在寻找整个互联网,但在任何地方都找不到答案。我有一个用 ANT 脚本编码的 MQFTE 作业,如果文件没有今天的日期,我在移动文件的过程中遇到了困难。我想做的是有条件的停止,比如在执行过程中的 return true 值,所以作业不会通过进一步的例程,如果文件被识别就结束跳过。
这在 ANT 中可能吗?还是必须遍历脚本中的每个<target>?
【问题讨论】:
标签:
ant
conditional
last-modified
websphere-mq-fte
【解决方案1】:
Ant fail 任务可用于有条件地停止 Ant 脚本。下面的示例将属性TODAY 初始化为当前日期,然后使用带有嵌套<date> 元素的fileset 仅选择在今天日期之前修改的文件。然后,pathconvert 任务仅在fileset 中至少有一个文件在今天之前修改时才设置属性files-not-empty。如果没有要复制的文件,则使用fail 任务来停止 Ant 脚本。
<target name="copy-if-not-modified-today">
<property name="copy-from.dir" value="${basedir}" />
<property name="copy-to.dir" value="${basedir}/build/copied_files" />
<mkdir dir="${copy-to.dir}" />
<tstamp>
<format property="TODAY" pattern="MM/dd/yyyy" />
</tstamp>
<fileset id="files" dir="${copy-from.dir}" includes="*">
<date datetime="${TODAY} 12:00 AM" when="before"/>
</fileset>
<pathconvert property="files-not-empty" setonempty="false" refid="files" />
<!--
Stop the Ant script if there are no files to copy that were modified prior
to today's date.
-->
<fail unless="files-not-empty" />
<copy todir="${copy-to.dir}" preservelastmodified="true">
<fileset refid="files" />
</copy>
</target>