Can I conditionally stop an Ant script based on fi

2019-07-19 08:19发布

问题:

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"