如果满足两个条件,则执行 Ant 任务 [英] execute Ant task if TWO conditions are met

查看:37
本文介绍了如果满足两个条件,则执行 Ant 任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上面的 脚本实现了if dir_is_empty then git-clone else git-fetch 使用 Ant-1.7.1 核心语句:

The above ant 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>

(参见我的其他帖子)

但是如何实现一个由两个条件启用的target?

如果 dir_does_not_existdir_is_empty 然后 git-clone else git-fetch

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

我目前的尝试:

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

[...]

<小时>

我更喜欢 Ant-1.7.1 核心语句.但我对其他可能性持开放态度,例如 Ant contrib嵌入式脚本...请随意发表您的想法...


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

(另见问题只要满足条件就执行ANT任务)

推荐答案

即使绑定到 Ant 1.7.1,您也可以将 3 个 chk 目标合并为一个,请参阅 condition 部分在代码段中.由于 Ant 1.9.1(由于 Ant 1.9.1 中的错误,最好使用 Ant 1.9.3 有关详细信息,请参阅此答案)可以在所有任务和嵌套元素上添加 if 和unless 属性,所以没有额外的目标需要,铁:

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>

这篇关于如果满足两个条件,则执行 Ant 任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆