简体   繁体   中英

how to create xml document with special node names with groovy markupbuilder

I am building an ant script with groovy markupbuilder. Unfortunately markupbuilder doesn't allow me to create nodes with name 'target' (no problem with targetee), becauase it throws me

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:

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?

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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