Can I conditionally stop an Ant script based on file last modified time?

回眸只為那壹抹淺笑 提交于 2019-12-11 01:22:52

问题


I've been looking all over the Internet, but couldn't find an answer anywhere. I have an MQFTE job coded in ANT script and I'm having difficulty with a process that moves files if a file doesn't have today's date. What I would like to do is a conditional stop, something like a return true value in a middle of the execution so the job is not going to go through the further routine and just end if the file is identified as to be skipped.

Is that possible in ANT? Or does it have to go through every <target> in the script?


回答1:


The Ant fail task may be used to conditionally stop an Ant script. The example below initializes the property TODAY to the current date and then uses a fileset with a nested <date> element to only select files modified prior to today's date. The pathconvert task then sets the property files-not-empty only if there is at least one file in the fileset modified prior to today's date. The fail task is then used to stop the Ant script if there are no files to copy.

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



回答2:


Maybe Ant-Contrib Tasks can help you. To perform tasks based on conditions, the "If-then-else" is a possible solution - "Ant if"



来源:https://stackoverflow.com/questions/12111282/can-i-conditionally-stop-an-ant-script-based-on-file-last-modified-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!