如果在macrodef中,则为Ant条件

时间:2013-08-13 10:01:10

标签: ant

在ant中,我有一个macrodef。

假设我必须使用这个macrodef,并且如果属性special.property存在并且为true,我想在所述macrodef中运行一个项目,我该怎么办?

我目前有

<macrodef name="someName">
    <sequential>
        <someMacroDefThatSetsTheProerty  />
        <some:thingHereThatDependsOn if="special.property" />
    <sequential>
</macrodef>

哪个不起作用 - some:thingHereThatDependsOn没有“if”属性,我无法添加一个。

antcontrib不可用。

使用目标我可以给目标一个“if”,我可以用macrodef做什么?

2 个答案:

答案 0 :(得分:16)

在Ant 1.9.1及更高版本中,现在有ifunless attributes的新实现。这可能就是你想到的。

首先,您需要将它们放入命名空间。将它们添加到您的<project>标题中:

<project name="myproject" basedir="." default="package"
    xmlns:if="ant:if"
    xmlns:unless="ant:unless">

现在,您可以将它们添加到几乎任何Ant任务或子实体中:

<!-- Copy over files from special directory, but only if it exists -->
<available property="special.dir.available"
    file="${special.dir} type="dir"/>

<copy todir="${target.dir}>
    <fileset dir="${special.dir}" if:true="special.dir.available"/>
    <fileset dir="${other.dir}"/>
</copy>

<!-- FTP files over to host, but only if it's on line-->
<condition property="ftp.available">
    <isreachable host="${ftp.host}"/>
</condition>

<ftp server="${ftp.host}" 
    userid="${userid}"
    passowrd="${password}"
    if:true="ftp.available">
    <fileset dir=".../>
</ftp>

答案 1 :(得分:7)

仅当ANT“thingHereThatDependsOn”任务支持“if”属性时,才可以执行此操作。

如上所述,ANT中的条件执行通常仅适用于目标。

<target name="doSomething" if="allowed.to.do.something">
   ..
   ..
</target>

<target name="doSomethingElse" unless="allowed.to.do.something">
   ..
   ..
</target>

<target name="go" depends="doSomething,doSomethingElse"/>