如何使用groovy markupbuilder创建具有特殊节点名的xml文档

时间:2010-11-04 11:50:10

标签: groovy markupbuilder

我正在使用groovy markupbuilder构建一个ant脚本。不幸的是,markupbuilder不允许我创建名为'target'的节点(对于targetstee没有问题), 因为它抛出了我

Caught: groovy.lang.MissingMethodException: No signature of method: java.lang.String.call() is applicable for argument types: (java.util.LinkedHashMap, BuildGen$_main_closure1_closure5) values: [[name:clean], BuildGen$_main_closure1_closure5@18efaea]
Possible solutions: wait(), any(), trim(), split(), dump(), next()

所以在我的markupbuilder里面这个代码片段可以工作:

targete(name: 'clean') {
  delete(dir: rootProj.compilerOutput)
}

但我想用'目标'节点实现相同的目标..

我设法以这种方式创建一个空的'目标'节点:

builder.invokeMethod('target', [name: 'clean'])

但我怎样才能继续在这个'目标'节点内放置更多节点?


工作代码示例:

 def writer = new StringWriter()
 def builder = new groovy.xml.MarkupBuilder(writer)

 builder.project(name: projectName, basedir:'.') {

   // works with 'target2' but not with 'target'

   'target2'(name: 'build-subprojects') {
    rootProj.getAllDependentProjects().each { p->
    echo(message: "Compiling project: ${p.projectName}")
    // some real stuff
  }
}

1 个答案:

答案 0 :(得分:3)

如果我猜对了,你的问题是你想创建名为Groovy关键字的节点吗?

如果是这样,那么您可以简单地将名称放在引号中,如下所示:

def writer = new StringWriter()
def builder = new groovy.xml.MarkupBuilder( writer )

builder.project {
  'for'(name: 'clean') {
    delete(dir: '.')
  }
}

println writer

该代码片段将打印出来:

<project>
  <for name='clean'>
    <delete dir='.' />
  </for>
</project>

对我而言,这有效:

def projects = [ 'pro_one', 'pro_two' ]
def writer = new StringWriter()
def builder = new groovy.xml.MarkupBuilder(writer)

builder.project( name: 'test', basedir:'.' ) {
  'target'( name: 'build-subprojects' ) {
    projects.each { p ->
      echo( message: "Compiling project: ${p}" )
    }
  }
}

println writer.toString()

在调用此代码之前,您是否已将target设置为代码中的任何内容?

你可以尝试:

  builder.target( name: 'build-subprojects' ) {

这可能会更好吗?

我已经尝试过Groovy 1.7.5和1.8 beta 2并且无法让它失败: - /