蚂蚁如果:真.将属性设置为 true 不会导致任务执行 [英] Ant if:true. Setting property to true won't cause the task to executed

查看:22
本文介绍了蚂蚁如果:真.将属性设置为 true 不会导致任务执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Ant 1.9.1 中,您可以在大多数任务中使用if 和unless 属性.

我定义了一个宏,我在其中尝试运行这些任务:

....<目标名称="test.templates"description="测试自动配置模板和答案"><test.templatesif:true="test.templates"template.root.dir="${main.dir}"answer.dir="${main.config.dir}"/></目标>

然而,这不会运行我的宏——即使属性 test.templates 设置为 true.如果我删除该行,我的 test.template 宏将起作用.

在用户定义的宏中使用 if:true 是否有问题?解决此问题的最佳方法是什么?

解决方案

来自 ant 手册 :

<块引用>

从 Ant 1.9.1 开始,可以在所有对象上添加 if 和unless 属性使用特殊命名空间的任务和嵌套元素.

直到现在才在宏定义中使用新的 if 和除非属性,但以下代码段有效:

<property name="foo" value="true"/><宏定义名称=foobar"><属性名称=bla"/><属性名称=whentrue"/><顺序><echo if:true="${@{whentrue}}">@{bla}</echo></顺序></宏定义><echo>${ant.version}</echo><foobar whentrue="foo" bla="yada,yada"/></项目>

注意=>属性语法<echo if:true="${@{whentrue}}">,仅在使用@{whentrue}时不起作用.>

输出:

 [echo] Apache Ant(TM) version 1.9.1 2013 年 5 月 15 日编译[回声] 雅达,雅达

我的另一个尝试:

<属性名称=bla"/><顺序><echo>@{bla}</echo></顺序></宏定义><echo>${ant.version}</echo><foobar bla="yada,yada"/>

没用:

... 问题:无法创建任务或输入 foobar原因:名称未定义.行动:检查拼写.行动:检查是否已声明任何自定义任务/类型.行动:检查任何<presetdef>/<macrodef>已经进行了声明.

还假设 <foobar bla="yada,yada" if:true="foo"/> 之类的东西会起作用:

<property name="foo" value="true"/><宏定义名称=foobar"><属性名称=bla"/><顺序><echo>@{bla}</echo></顺序></宏定义><echo>${ant.version}</echo><foobar bla="yada,yada" if:true="foo"/></项目>

输出,没有错误但macrodef没有被执行:

[echo] Apache Ant(TM) version 1.9.1 2013 年 5 月 15 日编译建造成功

似乎该区域仍然存在一些不一致之处,因为此功能是全新的.
也许我们应该提交一个错误!?
-- 编辑 (1) --
刚刚找到了 Peter Reilly 2007 年的评论(他已经实现了 if/unless 功能) 在提供带有宏定义的片段的 ant bug 数据库中.

-- 编辑(2)--
尽管新的 Ant 版本 1.9.3 从 2013 年 12 月 29 日开始(请参阅此处的发行说明) 修复了与新的 if: 和除非: 属性相关的错误 (https://issues.apache.org/bugzilla/show_bug.cgi?id=55885) 我们的问题仍然存在.因此,我打开了一个错误报告,请参阅 ant 错误数据库 bugid 55971.

-- 编辑(3)--
终于找到了解决办法.除了 Bugid 55885 的错误修正之外,Ant 1.9.3 版还为新的 if: 和除非:attributes => Bugid 55359 显示 if:true必须使用 ="${propertyname}" 而不是 if:true="propertyname".
所以你的宏应该在 Ant 1.9.3 上升级后像这样工作:

....<目标名称="test.templates"description="测试自动配置模板和答案"><test.templatesif:true="${test.templates}"template.root.dir="${main.dir}"answer.dir="${main.config.dir}"/></目标>

In Ant 1.9.1, you have the ability to use if and unless attributes on most tasks.

I have a macro I've defined where I'm trying to run these tasks:

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

However, this won't run my Macro -- even though the property test.templates is set to true. If I remove that line, my test.template macro will work.

Is there a problem using if:true in a user defined Macro? What's the best way to get around this issue?

解决方案

From ant manual :

Since Ant 1.9.1 it is possible to add if and unless attributes on all tasks and nested elements using special namespaces.

Didn't use the new if and unless attributes in a macrodef until now, but the following snippet works :

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

Notice => the property syntax <echo if:true="${@{whentrue}}">, it doesn't work when using @{whentrue} only.

Output :

 [echo] Apache Ant(TM) version 1.9.1 compiled on May 15 2013
 [echo] yada,yada

My other try :

<macrodef name="foobar" if:true="foo">
 <attribute name="bla"/>
 <sequential>
   <echo>@{bla}</echo>
 </sequential>
</macrodef>

 <echo>${ant.version}</echo>
 <foobar bla="yada,yada"/>

didn't work :

... 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.

Also assumed that something like <foobar bla="yada,yada" if:true="foo"/> would work :

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

output, no error but macrodef doesn't get executed :

[echo] Apache Ant(TM) version 1.9.1 compiled on May 15 2013
BUILD SUCCESSFUL

Seems like there are still some inconsistencies in that area, as this feature is spanking new.
Maybe we should file a bug !?
-- EDIT (1) --
Just found a comment from 2007 by Peter Reilly (he has implemented the if / unless feature) in the ant bug database providing a snippet with a macrodef.

-- EDIT (2) --
Although the new Ant release 1.9.3 from December 29, 2013 (see releasenotes here) fixed a bug related to the new if: and unless: attributes (https://issues.apache.org/bugzilla/show_bug.cgi?id=55885) our problem still remains. Thus i opened a bug report, see ant bug database bugid 55971.

-- EDIT (3) --
Finally the solution is found. Beside the bugfix for Bugid 55885 the Ant Release 1.9.3 provides also a bugfix for the documentation of the new if: and unless: attributes => Bugid 55359 showing that if:true="${propertyname}" instead of if:true="propertyname" has to be used.
So your macro should work after upgrade on Ant 1.9.3 like that :

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

这篇关于蚂蚁如果:真.将属性设置为 true 不会导致任务执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆