简体   繁体   English

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

[英]how to create xml document with special node names with groovy markupbuilder

I am building an ant script with groovy markupbuilder. 我正在用groovy markupbuilder构建一个蚂蚁脚本。 Unfortunately markupbuilder doesn't allow me to create nodes with name 'target' (no problem with targetee), becauase it throws me 不幸的是,markupbuilder不允许我创建名称为“ target”的节点(targetee没问题),因为它抛出了我

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

so inside my markupbuilder this snippet works: 因此,在我的markupbuilder中,此代码段有效:

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

but I would like to achieve the same with a 'target' node.. 但我想通过“目标”节点实现相同的目标。

I managed to create an empty 'target' node this way: 我设法通过以下方式创建了一个空的“目标”节点:

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

but how can I go on and put more nodes inside this 'target' node? 但是如何继续在这个“目标”节点中放置更多节点?


Example of working code: 工作代码示例:

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

If I guess right, your problem is you want to create nodes with names that are Groovy keywords? 如果我猜对了,那么您的问题是您要创建具有Groovy关键字名称的节点吗?

If so, then you can simply put the name in quotes, like so: 如果是这样,则只需将名称加上引号即可,如下所示:

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

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

println writer

That snippet will print out: 该代码段将打印出来:

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

For me, this works: 对我来说,这有效:

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

Have you got target set to anything in your code before calling this? 在调用此代码之前,是否已将target设置为代码中的任何内容?

You could try: 您可以尝试:

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

That might work better? 那可能更好?

I've tried Groovy 1.7.5, and 1.8 beta 2 and can't get it to fail :-/ 我已经尝试过Groovy 1.7.5和1.8 beta 2,但无法使其失败:-/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM