使用简单的ant属性很棘手&不允许轻松设置所需的值(属性是不可变的)。使用ant-conrib的var任务允许设置和取消设置属性。
制作蚂蚁财产背后的任何种族或充分理由是为了以如此复杂的方式运作?
<property name="some.ant.prop" value=""/>
<if>
<isset property="some.ant.prop"/>
<then>
<echo message="immutable ant prop - not good, defined and just even set to null string : ${some.ant.prop}"/>
<property name="some.ant.prop" value="no-effect-value"/>
<echo message="no-effect on changing already defined prop : ${some.ant.prop}"/>
<var name="some.ant.prop" unset="true"/>
<property name="some.ant.prop" value="any-value-accepted"/>
<echo message="Overwritten prop value: ${some.ant.prop}"/>
</then>
</if>
不是为了讨论或争论,而是对更可行的替代方案有所了解。感谢。
答案 0 :(得分:3)
Ant不是一种编程语言!
一旦设定的属性在设计中是不可变的。
优点和缺点已经经常讨论(我也不会详细讨论)。
克服这些限制的几种可能性:
在过去,人们为此目的使用了antcall--有其所有缺点,搜索'antcall与macrodef'以获取详细信息。 Ant 1.6引入了macrodef
,Ant 1.8带来了新的local
任务
如果macrodef和local不够,您可以使用内置javascript引擎(自JDK 1.6起)或Groovy的脚本任务来访问ant api。
还有像f.e.这样的Ant插件。 antcontrib或Flaka。如果antcontrib var / unset对你来说太笨拙了,那么
Flaka的let
任务为覆盖属性提供了更直接的方法:
<!-- set a new property -->
<fl:let>foo := 'bar'</fl:let>
<!-- overwrite an existing property or userproperty
(those properties defined on the commandline via -Dfoo=bar ..)
notice the double '::' in foo ::= 'baz' -->
<fl:let>foo ::= 'baz'</fl:let>
最后:
要么习惯蚂蚁及其限制(但不要使用antcall!)或者使用Ant插件
use ant from groovy
或切换到Gradle。