execute Ant task if TWO conditions are met

oHo picture oHo · Aug 7, 2013 · Viewed 17.1k times · Source

The above script implements if dir_is_empty then git-clone else git-fetch using Ant-1.7.1 core statements:

<target name="update" depends="git.clone, git.fetch" />

<target name="check.dir">
  <fileset dir="${dir}" id="fileset"/>
  <pathconvert refid="fileset" property="dir.contains-files" setonempty="false"/>
</target>

<target name="git.clone" depends="check.dir" unless="dir.contains-files">
  <exec executable="git">
    <arg value="clone"/>
    <arg value="${repo}"/>
    <arg value="${dir}"/>
  </exec>
</target>

<target name="git.fetch" depends="check.dir" if="dir.contains-files" >
  <exec executable="git" dir="${dir}">
    <arg value="fetch"/>
  </exec>
</target>

(see my other post)


But how to implement a target enabled by two conditions?

if dir_does_not_exist or dir_is_empty then git-clone else git-fetch

my current attempt:

<target name="git.clone" 
        depends="chk.exist, chk.empty" 
        unless="!dir.exist || dir.noempty" >
  [...]
</target>

<target name="chk.exist">
  <condition property="dir.exist">
    <available file="${dir}/.git" type="dir"/>
  </condition>
</target>

[...]

I would prefer Ant-1.7.1 core statements. But I am open about other possibilities as Ant contrib, or embedded script... Feel free to post your ideas...

(See also question Execute ANT task just if a condition is met)

Answer

Rebse picture Rebse · Oct 9, 2013

Even when bound to Ant 1.7.1 you may combine your 3 chk targets into one, see the condition part in the snippet. Since Ant 1.9.1 (better use Ant 1.9.3 because of bugs in Ant 1.9.1 see this answer for details) it is possible to add if and unless attributes on all tasks and nested elements, so no extra target needed, f.e. :

<project xmlns:if="ant:if" xmlns:unless="ant:unless">

  <condition property="cloned" else="false">
    <and>
      <available file="${dir}/.git" type="dir" />
      <resourcecount when="gt" count="0">
        <fileset dir="${dir}/.git" />
      </resourcecount>
    </and>
  </condition>

  <exec executable="git" unless:true="${cloned}">
    <arg value="clone" />
    <arg value="${repo}" />
    <arg value="${dir}" />
  </exec>

  <exec executable="git" dir="${dir}" if:true="${cloned}">
    <arg value="fetch" />
  </exec>

</project>