【问题标题】:Ant macrodef: Is there a way to get the contents of an element parameter?Ant macrodef:有没有办法获取元素参数的内容?
【发布时间】:2011-03-20 02:24:15
【问题描述】:

我正在尝试在 Ant 中调试宏定义。我似乎找不到显示作为元素发送的参数内容的方法。

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

所以我想我需要一种方法以某种方式将&lt;elem /&gt; 的内容放入属性中(某些扩展的宏定义实现可能有),或者我需要一种可以打印出任何 XML 树的&lt;element-echo&gt;&lt;elem /&gt;&lt;/element-echo&gt;你放进去。有谁知道其中任何一个的实现?当然也欢迎任何第三种未预料到的获取数据的方式。

【问题讨论】:

    标签: ant element macrodef


    【解决方案1】:

    echoxml 任务怎么样?

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

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

    <echoxml><elem /></echoxml>
    

    结果

    $ 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 属性将输出放到一个临时文件中,然后读取该文件并删除声明,或者重新格式化您认为合适的信息。

    编辑

    经过反思,您可能会接近您所描述的内容,例如您的宏定义的顺序主体

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

    【讨论】:

    • &lt;echoxml /&gt; 元素正是我想要的,谢谢!
    • 另外,“”即使没有额外的处理来添加 XML 标头也可以工作(在 ant 1.8.4 上测试)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-30
    • 2015-01-06
    • 2012-06-02
    • 1970-01-01
    • 2018-10-31
    • 1970-01-01
    相关资源
    最近更新 更多