How to check if a property exists?

荒凉一梦 提交于 2020-01-22 13:18:06

问题


How do I check the existence of a property using Ant?

I am open to the use of ant-contrib, if Ant doesn't provide a similar thing.

Also, ant-contrib has an assert task, which provides exists, but the assertion is not what I need here since I would prefer a boolean return value.


回答1:


You can use the Condition task with an isset condition.

<project default="test">

  <property name="a" value="a"/>

  <target name="test">

    <condition property="a.set" else="false">
      <isset property="a"/>
    </condition>

    <condition property="b.set" else="false">
      <isset property="b"/>
    </condition>

    <echo message="a set ? ${a.set}"/>
    <echo message="b set ? ${b.set}"/>

  </target>
</project>

Output:

test:
     [echo] a set ? true
     [echo] b set ? false



回答2:


Since Ant 1.9.1 it is possible to use "if" and "unless" attributes. You can use these new attributes if you add the 2 namespaces xmlns:if="ant:if" and xmlns:unless="ant:unless" to the project.

<!DOCTYPE project>
<project xmlns:if="ant:if" xmlns:unless="ant:unless">
  <property unless:set="property" name="property.is.set" value="false"/>
  <property if:set="property" name="property.is.set" value="true"/>
  <echo>${property.is.set}</echo>
</project>

see also https://ant.apache.org/manual/ifunless.html



来源:https://stackoverflow.com/questions/8079203/how-to-check-if-a-property-exists

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!