【问题标题】:How do I use the condition element in ant to set another property?如何使用 ant 中的条件元素来设置另一个属性?
【发布时间】:2011-08-03 13:55:52
【问题描述】:

我有一个与 ant 一起使用的 build.xml,我正在尝试在目标中放置一个条件:

首先我在这里设置了可以正常工作的属性:

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

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

【问题讨论】:

  • 这看起来与 ant 文档中的语法一模一样。你确定你没有在条件任务中创建一个 元素(你已经写了“在这里做事”)?。
  • 啊...我正在那里创建另一个属性(下一行是 这是不行吗?

标签: java ant


【解决方案1】:

condition是用来定义一个属性的,而不是根据一个属性的值来执行一些动作。

使用target with if or unless attribute根据属性值执行一些任务。

【讨论】:

  • 我最初确实有目标 + 一个 if,但目标名称是固定的(-post-jar),所以我无法复制它,因此试图在目标本身中放置一些条件。谢谢JB。
  • 只要让你的 -post-jar 目标依赖于另一个具有 if 属性的目标。
  • 感谢 JB 尼泽特。通过重新安排任务,将 添加到新目标,然后在这个新目标上放置 if="isWindows" 来解决。我认为嵌套错误是指在我的原始代码中的 标记。
【解决方案2】:

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>

【讨论】:

    猜你喜欢
    • 2015-01-11
    • 1970-01-01
    • 1970-01-01
    • 2018-10-18
    • 2013-05-13
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多