從ant manual:
由於螞蟻1.9.1,可以使用特殊的命名空間的所有 任務和嵌套元素添加if和unless屬性。
沒有如果,除非在macrodef屬性,直至現在使用新的,但下面的代碼片斷工作:
<project xmlns:if="ant:if" xmlns:unless="ant:unless">
<property name="foo" value="true"/>
<macrodef name="foobar">
<attribute name="bla"/>
<attribute name="whentrue"/>
<sequential>
<echo if:true="${@{whentrue}}">@{bla}</echo>
</sequential>
</macrodef>
<echo>${ant.version}</echo>
<foobar whentrue="foo" bla="yada,yada"/>
</project>
公告=>屬性語法<echo if:true="${@{whentrue}}">
,它不使用時工作僅限於@ {whentrue}。
輸出:
[echo] Apache Ant(TM) version 1.9.1 compiled on May 15 2013
[echo] yada,yada
我的另一個嘗試:
<macrodef name="foobar" if:true="foo">
<attribute name="bla"/>
<sequential>
<echo>@{bla}</echo>
</sequential>
</macrodef>
<echo>${ant.version}</echo>
<foobar bla="yada,yada"/>
沒有工作:
... Problem: failed to create task or type foobar
Cause: The name is undefined.
Action: Check the spelling.
Action: Check that any custom tasks/types have been declared.
Action: Check that any <presetdef>/<macrodef> declarations have taken place.
還假定像<foobar bla="yada,yada" if:true="foo"/>
會的工作:
<project xmlns:if="ant:if" xmlns:unless="ant:unless">
<property name="foo" value="true"/>
<macrodef name="foobar">
<attribute name="bla"/>
<sequential>
<echo>@{bla}</echo>
</sequential>
</macrodef>
<echo>${ant.version}</echo>
<foobar bla="yada,yada" if:true="foo"/>
</project>
輸出,沒有錯誤,但macrodef沒有得到執行:
[echo] Apache Ant(TM) version 1.9.1 compiled on May 15 2013
BUILD SUCCESSFUL
好像還有在該地區一些不一致的地方,因爲這個功能是嶄新的。
也許我們應該提交一個bug!?
- 編輯(1) -
剛剛發現一個comment from 2007 by Peter Reilly(他實施了if/unless功能)在螞蟻錯誤數據庫中,提供了一個帶有macrodef的片段。
- EDIT(2) -
雖然從2013年12月29日(see releasenotes here)的新的Ant版本1.9.3固定與新的,如果一個錯誤:除非:屬性(https://issues.apache.org/bugzilla/show_bug.cgi?id=55885)我們的問題仍然存在。因此,我打開了一個錯誤報告,請參閱螞蟻錯誤數據庫bugid 55971。
- 編輯(3) -
最後找到解決方案。除了版本1.9的Bugid 55885的bug修正之外。3還爲新的if:文件提供了錯誤修正:除非:attributes =>Bugid 55359顯示必須使用if:true="${propertyname}"
而不是if:true="propertyname"
。
所以你的宏應該升級後的工作螞蟻1.9.3這樣的:
<property name="test.templates" value="true"/>
....
<target name="test.templates"
description="Test the autoconfiguration templates and answers">
<test.templates
if:true="${test.templates}"
template.root.dir="${main.dir}"
answers.dir="${main.config.dir}"/>
</target>
我注意到你有一個目標,財產和宏的所有的所謂的「test.templates」 –
充滿bug報告問題仍然存在後從2013年12月29日發佈新的ant版本 - 詳情請參閱我的更新回答編輯(2)。 – Rebse
找到解決方案 - 查看我更新的答案編輯(3)! – Rebse