2010-07-23 28 views
8

Tôi đang cố gỡ lỗi macrodef trong Ant. Tôi dường như không thể tìm cách hiển thị nội dung của tham số được gửi dưới dạng phần tử.Macrodef kiến: Có cách nào để lấy nội dung của tham số phần tử không?

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

Ví dụ chạy:

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

Vì vậy, tôi đoán tôi cần hoặc là một cách để có được nội dung của <elem /> thành một tài sản nào đó (một số thực hiện macrodef mở rộng có thể có đó), hoặc tôi cần một loại <element-echo><elem /></element-echo> có thể in ra bất kỳ cây XML nào bạn đặt bên trong. Có ai biết về việc thực hiện một trong những điều này? Bất kỳ cách thức thứ ba, bất ngờ nhận dữ liệu nào cũng được hoan nghênh.

Trả lời

9

Làm thế nào về nhiệm vụ echoxml?

Trong ví dụ xây dựng tập tin của bạn thay thế các dòng

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

với

<echoxml><elem /></echoxml> 

kết quả trong

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

lẽ khai báo XML không muốn mặc dù. Bạn có thể sử dụng thuộc tính echoxml file để đưa đầu ra vào tệp tạm thời, sau đó đọc tệp đó và xóa khai báo hoặc định dạng lại thông tin như bạn thấy phù hợp.

chỉnh sửa

On phản ánh, có lẽ bạn có thể nhận được gần với những gì bạn mô tả, ví dụ cơ thể tuần tự này macrodef bạn

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

cho

$ 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

Lệnh ' ' yếu tố chính xác là những gì tôi đang tìm kiếm, cảm ơn bạn! – clacke

+0

Là phần thưởng, '' hoạt động ngay cả khi không có xử lý bổ sung để thêm tiêu đề XML (được thử nghiệm trên kiến ​​1.8.4). –

Các vấn đề liên quan