上面的if脚本使用Ant-1.7.1核心语句实现了then then git-clone else git-fetch:
<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_exist dir_is_empty if or 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任务)
发布于 2013-10-09 12:29:50
即使绑定到Ant1.7.1,您也可以将3个chk目标组合成一个,请参阅片段中的条件部分。自从Ant1.9.1(更好地使用Ant1.9.3,因为Ant1.9.1详情见此答案。中有bug),就可以在所有任务和嵌套元素上添加如果和除非属性,因此不需要额外的目标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>发布于 2013-10-09 09:19:58
来自蚂蚁关于目标的文档
在
if/unless子句中只能指定一个属性名。如果要检查多个条件,可以使用依赖目标来计算检查的结果: 存在foo.txt和bar.txt文件。
此外,还讨论了dev@ant.apache.org和user@ant.apache.org邮件列表:
例如,下面的target组合了两个属性(dir.exist和dir.noempty)来使用操作符<and>和<istrue>创建另一个属性(许多其他操作人员已记录在案为<or>、<xor>、<not>、<isfalse>、<equals>、D29)。
<target name="chk" depends="chk.exist, chk.empty" >
<condition property="cloned">
<and>
<istrue value="dir.exist" />
<istrue value="dir.noempty" />
</and>
</condition>
</target>上述property "cloned"由目标git.clone和git.fetch使用,如下所示:
<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>https://stackoverflow.com/questions/18097555
复制相似问题