2010-07-23 83 views
8

我想在Ant中调试macrodef。我似乎无法找到一种方式来显示作为元素发送的参数的内容。Ant macrodef:有没有办法获取元素参数的内容?

<project name='debug.macrodef'> 
    <macrodef name='def.to.debug'> 
    <attribute name='attr' /> 
    <element name='elem' /> 
    <sequential> 
     <echo>Sure, the attribute is easy to debug: @{attr}</echo> 
     <echo>The element works only in restricted cases: <elem /> </echo> 
     <!-- This works only if <elem /> doesn't contain anything but a 
      textnode, if there were any elements in there echo would 
      complain it doesn't understand them. --> 
    </sequential> 
    </macrodef> 

    <target name='works'> 
    <def.to.debug attr='contents of attribute'> 
     <elem>contents of elem</elem> 
    </def.to.debug> 
    </target> 

    <target name='does.not.work'> 
    <def.to.debug attr='contents of attribute'> 
     <elem><sub.elem>contents of sub.elem</sub.elem></elem> 
    </def.to.debug> 
    </target> 
</project> 

实例运行:

$ ant works 
... 
works: 
[echo] Sure, the attribute is easy to debug: contents of attribute 
[echo] The element works only in restricted cases: contents of elem 
... 

$ ant does.not.work 
... 
does.not.work: 
[echo] Sure, the attribute is easy to debug: contents of attribute 

BUILD FAILED 
.../build.xml:21: The following error occurred while executing this line: 
.../build.xml:7: echo doesn't support the nested "sub.elem" element. 
... 

所以我想我需要使用一个办法让<elem />的内容到属性不知何故(一些扩展macrodef实现可能有),或者我需要一个可以打印出你放入的任何XML树的<element-echo><elem /></element-echo>。有谁知道这些的实现吗?任何第三种无法预料的数据获取方式当然也是受欢迎的。

回答

9

echoxml任务如何?

在您的例子编译文件与

<echoxml><elem /></echoxml> 

结果更换线

<echo>The element works only in restricted cases: <elem /> </echo> 

$ ant does.not.work 
... 
does.not.work: 
    [echo] Sure, the attribute is easy to debug: contents of attribute 
<?xml version="1.0" encoding="UTF-8"?> 
<sub.elem>contents of sub.elem</sub.elem> 

也许XML声明不想要的,虽然。您可以使用echoxml file属性将输出放到临时文件中,然后读取该文件并删除声明,或根据需要重新格式化信息。

编辑

细想,你也许可以得到接近你的描述,例如这个连续体的macrodef什么

<sequential> 
    <echo>Sure, the attribute is easy to debug: @{attr}</echo> 
    <echoxml file="macro_elem.xml"><elem /></echoxml> 
    <loadfile property="elem" srcFile="macro_elem.xml"> 
    <filterchain> 
     <LineContainsRegexp negate="yes"> 
     <regexp pattern=".xml version=.1.0. encoding=.UTF-8..." /> 
     </LineContainsRegexp> 
    </filterchain> 
    </loadfile> 
    <echo message="${elem}" /> 
</sequential> 

$ ant does.not.work 
... 
does.not.work: 
    [echo] Sure, the attribute is easy to debug: contents of attribute 
    [echo] <sub.elem>contents of sub.elem</sub.elem> 
+0

的''元素正是我所期待的,谢谢! – clacke 2010-07-27 11:52:26

+0

作为奖励,''即使没有额外的处理来添加XML标头(在Ant 1.8.4上测试过)也可以工作。 – 2013-04-15 10:38:40

相关问题