开发者

How to avoid outer-scope values messing with using the MarkupBuilder?

开发者 https://www.devze.com 2023-04-05 09:22 出处:网络
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?

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)
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号