Ant macrodef:有没有办法获取元素参数的内容?

cla*_*cke 8 ant element 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>
Run Code Online (Sandbox Code Playgroud)

示例运行:

$ 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.
...
Run Code Online (Sandbox Code Playgroud)

所以我想我需要一种方法来以某种方式获取<elem />属性的内容(一些扩展的宏定义实现可能有),或者我需要一种<element-echo><elem /></element-echo>可以打印出你放入的任何XML树的方法.有谁知道其中任何一个的实现?任何第三种,意料之外的数据获取方式当然也是受欢迎的.

mar*_*ton 9

这项echoxml任务怎么样?

在您的示例构建文件中替换该行

<echo>The element works only in restricted cases: <elem /> </echo>
Run Code Online (Sandbox Code Playgroud)

<echoxml><elem /></echoxml>
Run Code Online (Sandbox Code Playgroud)

结果是

$ 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>
Run Code Online (Sandbox Code Playgroud)

也许不希望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>
Run Code Online (Sandbox Code Playgroud)

$ 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>
Run Code Online (Sandbox Code Playgroud)