Ant macrodef: Is there a way to get the contents of an element parameter?

早过忘川 提交于 2019-12-05 03:32:00

How about the echoxml task?

In your example build file replacing the line

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

with

<echoxml><elem /></echoxml>

results in

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

Perhaps the XML declaration is not wanted though. You might use the echoxml file attribute to put the output to a temporary file, then read that file and remove the declaration, or reformat the information as you see fit.

edit

On reflection, you can probably get close to what you describe, for example this sequential body of your 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>

gives

$ 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>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!