2011-08-03 36 views
4

我有一个build.xml蚂蚁使用,而我试图把目标内的条件:如何在ant中使用condition元素来设置另一个属性?

首先,我在这里它设置属性工程确定:

<condition property="isWindows"> 
    <os family="windows"/> 
</condition> 

然后我试着在目标中使用它:

<target name="-post-jar"> 
    <condition property="isWindows" value="true"> 
     <!-- set this property, only if isWindows set --> 
     <property name="launch4j.dir" location="launch4j" /> 
    </condition> 

    <!-- Continue doing things, regardless of property --> 
    <move file="${dist.jar.dir}" tofile="myFile"/> 
    <!-- etc --> 
</target> 

我收到一个错误:“条件不支持嵌套的”属性“元素。 问题是:我如何正确地将条件放置在目标中,为什么错误是指“嵌套”属性?

+0

这看起来酷似蚂蚁文档的语法。你确定你没有在条件任务内创建一个元素(你写过“在这里做事情”)? –

+0

啊......我在那里创建了另一个属性(下一行是是否是否定的? – Pete855217

回答

3

condition用于定义属性,但不用于根据属性的值执行某些操作。使用target with if or unless attribute来执行一些基于属性值的任务。

+0

我原本有目标+ an if ,但目标名称是固定的(-post-jar),所以我无法复制它,因此试图将一些条件放入目标本身谢谢JB。 – Pete855217

+0

只需让你的-post-jar目标取决于另一个目标一个if属性 –

+0

谢谢JB Nizet。通过重新安排任务解决,为一个新目标添加,然后在这个新目标上添加一个if =“isWindows”。我相信嵌套错误提到了一个标记正确在我的原始代码中的 Pete855217

0

condition的标准嵌套在condition元素的内部。

指定要使用property属性设置的属性以及使用condition元素上的value属性满足条件时的值。此外,您可以为该属性设置一个值,该条件不符合else属性。

要检查属性是否被设置为标准的condition,使用isset

<condition property="isWindows"> 
    <os family="windows"/> 
</condition> 

<target name="-post-jar"> 
    <!--Only set property if isWindows --> 
    <condition property="launch4j.dir" value="launch4j"> 
     <isset property="isWindows"/> 
    </condition> 

    <!-- Continue doing things, regardless of property --> 
    <move file="${dist.jar.dir}" tofile="myFile"/> 
    <!-- etc --> 
</target> 
相关问题