2010-07-23 3 views
8

Ant에서 macrodef를 디버그하려고합니다. 매개 변수의 내용을 요소로 표시하는 방법을 찾지 못하는 것 같습니다.Ant 매크로 정의 : 요소 매개 변수의 내용을 가져 오는 방법이 있습니까?

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

그래서 어떻게 든 (일부 확장 macrodef 구현하는 것이있을 수 있습니다) 내가 속성에 <elem />의 내용을 얻을 수있는 방법 중 하나를 필요로 추측, 또는 나는 필요합니다

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

예 실행 <element-echo><elem /></element-echo> 일종의 내부 XML 트리를 인쇄 할 수 있습니다. 누구든지이 중 하나의 구현을 알고 있습니까? 데이터를 가져 오는 세 번째 예기치 않은 방법은 물론 환영합니다.

답변

9

echoxml 작업은 어떻습니까? 귀하의 예제 빌드 파일에서

는 아마도 XML 선언하지만 원하지 않는

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

<echoxml><elem /></echoxml> 

결과를 줄

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

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

+0

보너스로, ''은 추가 처리 없이도 XML 헤더를 추가 할 수 있습니다 (개미 1.8.4에서 테스트 됨). –