How to avoid outer-scope values messing with using the MarkupBuilder?
I need an element named file in my xml, however the gradle project have a property named file which is called instead. What is the best way around this?
import groovy.xml.MarkupBuilder
task test {
def writer = new StringWriter()
def xml = new Marku开发者_运维知识库pBuilder(writer)
xml.elem1(test: 'fest') {
elem2(a: 'b')
file(c: 'd')
elem4(e: 'f')
}
println(writer)
}
You should be able to do:
xml.file( c: 'd' )
You should be able to use the 'delegate' variable, which is automatically made available within each XML element's code block.
For example:
import groovy.xml.MarkupBuilder
task test {
def writer = new StringWriter()
def xml = new MarkupBuilder(writer)
xml.elem1(test: 'fest') {
elem2(a: 'b')
delegate.file(c: 'd')
elem4(e: 'f')
}
println(writer)
}
精彩评论