0

我正在用 groovy markupbuilder 构建一个 ant 脚本。不幸的是,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()

所以在我的标记生成器中,这个片段有效:

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

1 回答 1

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 并且不能让它失败:-/

于 2010-11-04T12:18:04.447 回答