2013-11-20 26 views
6

在Ant 1.9.1中,您可以在大多數任務上使用if and unless attributesAnt if:true。將屬性設置爲true不會導致任務執行

我有我定義在那裏我試圖運行這些任務的宏:

<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設置爲true。如果我刪除該行,我的test.template宏將工作。

在用戶定義的宏中使用if:true有問題嗎?解決此問題的最佳方法是什麼?

+0

我注意到你有一個目標,財產和宏的所有的所謂的「test.templates」 –

+0

充滿bug報告問題仍然存在後從2013年12月29日發佈新的ant版本 - 詳情請參閱我的更新回答編輯(2)。 – Rebse

+0

找到解決方案 - 查看我更新的答案編輯(3)! – Rebse

回答

7

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> 
+0

感謝您投入的所有工作!我們使用的是antcontrib的'',但我認爲這將是一種更加優雅的處理問題的方式,但當它不起作用時會感到驚訝。 –

+0

@Rebse:如果您編輯此答案,將解決方案放在頂部而不是底部(在幾個非解決方案下),那就太好了。如果不是你的「我解決了它!」對這個問題發表評論,在結束之前我會放棄你的回答。 –

相關問題