execute Ant task if TWO conditions are met

家住魔仙堡 提交于 2019-11-29 13:37:50
Rebse

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>

From the documentation on targets:

Only one propertyname can be specified in the if/unless clause. If you want to check multiple conditions, you can use a dependend target for computing the result for the check:

<target name="myTarget" depends="myTarget.check" if="myTarget.run">
     <echo>Files foo.txt and bar.txt are present.</echo>
</target>

<target name="myTarget.check">
     <condition property="myTarget.run">
         <and>
             <available file="foo.txt"/>
             <available file="bar.txt"/>
         </and>
     </condition>
</target>

Moreover, there were some discussions on dev@ant.apache.org and user@ant.apache.org mailing-lists:


For example, the following target combines two properties (dir.exist and dir.noempty) to create another one (cloned) using operators <and> and <istrue> (many other operators are documented as <or>, <xor>, <not>, <isfalse>, <equals>, <length>).

<target name="chk" depends="chk.exist, chk.empty" >
  <condition property="cloned">
    <and>
      <istrue value="dir.exist"   />
      <istrue value="dir.noempty" />
    </and>
  </condition>
</target>

The above property "cloned" is used by targets git.clone and git.fetch as follows:

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

<target name="git.clone" depends="chk" unless="cloned" >
  <exec  executable="git" >
    <arg value="clone"   />
    <arg value="${repo}" />
    <arg value="${dir}"  />
  </exec>
</target>

<target name="git.fetch" depends="chk" if="cloned" >
  <exec executable="git" dir="${dir}">
    <arg value="fetch"/>
  </exec>
</target>

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

<target name="chk.empty" >
  <fileset dir="${dir}" id="fileset" />
  <pathconvert refid="fileset" property="dir.noempty" setonempty="false" />
</target>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!