【问题标题】:How to check two properties in an if condition of an Ant task?如何在 Ant 任务的 if 条件下检查两个属性?
【发布时间】:2014-01-24 18:44:06
【问题描述】:

可以通过指定ifunless 子句有条件地执行Ant 目标。据我所知,这个条款只接受一个属性。如何检查两个属性?

这是一个例子:

<project default="test">
  <property name="a" value="true"/>
  <property name="b" value="true"/>
  <target name="test-a" if="a">
    <echo>a</echo>
  </target>
  <target name="test-b" if="b">
    <echo>b</echo>
  </target>
  <target name="test-ab" if="a,b">
    <echo>a and b</echo>
  </target>
  <target name="test" depends="test-a,test-b,test-ab"/>
</project>

如果我运行它,test-ab 目标不会产生任何输出:

$ ant -f 目标-if.xml 构建文件:target-if.xml 测试-a: [回声] 一个 测试-b: [回声] b 测试抗体: 测试: 构建成功 总时间:0秒

如何为这两个属性指定一个and表达式?

【问题讨论】:

    标签: ant


    【解决方案1】:

    很遗憾,没有。 From the ant Targets manual:

    if/unless 子句中只能指定一个属性名。如果你 想要检查多个条件,可以使用依赖目标 计算检查结果:

    <target name="myTarget" depends="myTarget.check" if="myTarget.run">
        <echo>Files foo.txt and bar.txt are present.</echo>
    </target>
    
    <target name="myTarget.check">
        <condition property="myTarget.run">
            <and>
                <available file="foo.txt"/>
                <available file="bar.txt"/>
            </and>
        </condition>
    </target>
    

    【讨论】:

    • 这并不检查是否设置了ab这两个属性,而是检查文件foo.txtbar.txt是否存在。这不是问题中示例if="a, b" 的直接替代品。
    【解决方案2】:

    这是我使用条件元素的示例:

    <project default="test">
      <property name="a" value="true"/>
      <property name="b" value="true"/>
      <target name="test-a" if="a">
        <echo>a</echo>
      </target>
      <target name="test-b" if="b">
        <echo>b</echo>
      </target>
      <condition property="a-and-b">
        <and>
          <equals arg1="${a}" arg2="true"/>
          <equals arg1="${b}" arg2="true"/>
        </and>
      </condition>
      <target name="test-ab" if="a-and-b">
        <echo>a and b</echo>
      </target>
      <target name="test" depends="test-a,test-b,test-ab"/>
    </project>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-18
      • 1970-01-01
      • 2010-12-09
      • 2023-03-13
      • 2013-08-08
      • 1970-01-01
      • 1970-01-01
      • 2013-01-15
      相关资源
      最近更新 更多