【问题标题】:Ant Condition taskAnt 条件任务
【发布时间】:2013-01-15 13:26:10
【问题描述】:

我无法找到这个问题的答案,正如您将看到的,理解我试图逆向工程的 build.xml 是如何工作的并不重要。不过我确实认为这个问题有一定的道理。

在这个 build.xml 中,我有以下代码段:

<condition property="tests.complete">
    <isset property="no.tests" />
</condition>
<condition property="tests.complete">
    <and>
        <uptodate>
            ...
        </uptodate>
        <uptodate>
            ...
        </uptodate>
        <uptodate>
            ...
        </uptodate>
        <not>
            <available ... />
        </not>
        <not>
            <isset ... />
        </not>
    </and>
</condition>

我明白,如果在遇到这段代码之前设置了属性 no.tests,那么属性 tests.complete 将在第一个条件下设置为 true,无论在第二个条件任务中发生什么,这个属性离开代码段时将保持设置为 true。我的问题是,既然属性 tests.complete 是由第一个条件设置的,那么第二组条件测试是否会被评估?

【问题讨论】:

    标签: ant conditional-statements short-circuiting


    【解决方案1】:

    只能设置干净(未定义)的属性。如果您的属性已经设置,则什么都不做。

    所以,不,不评估第二组条件。您可以使用以下代码对其进行测试:

    <target name="run">
        <property name="no.tests" value="true"/>
        <condition property="tests.complete">
            <isset property="no.tests" />
        </condition>
        <echo message="${tests.complete}"/> <!-- prints true -->
    
        <condition property="tests.complete" else="false">
            <isset property="whatever" /> <!-- property whatever is not set -->
        </condition>
        <echo message="${tests.complete}"/> <!-- prints true as well! -->
    </target>
    

    您也可以使用相反的方法对其进行测试:

    <target name="run">
        <property name="whatever" value="true"/>
        <condition property="tests.complete" else="false">
            <isset property="no.tests" /> <!-- no.tests isn't defined -->
        </condition>
        <echo message="${tests.complete}"/> <!-- prints false -->
    
        <condition property="tests.complete" else="false">
            <isset property="whatever" /> <!-- the property whatever is defined -->
        </condition>
        <echo message="${tests.complete}"/> <!-- prints false as well! -->
    </target>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-17
      • 2013-01-11
      • 2023-03-13
      • 2017-07-10
      • 2013-08-08
      • 1970-01-01
      • 2013-06-29
      • 1970-01-01
      相关资源
      最近更新 更多