我尝试构建html格式的字符串,以通过jenkins管道将其作为电子邮件发送。我有以下代码:
import groovy.xml.MarkupBuilder
def writer = new StringWriter()
def html = new MarkupBuilder( writer )
html.table( class: "squishSummary" ) {
style( type: "text/css" ) {
mkp.yield( getTableStyle() )
}
...
}
def getTableStyle() {
....
}
不幸的是,这在执行管道时遇到错误:java.lang.NoSuchMethodError: No such DSL method 'style' found among steps [...]
如果我正确地理解它,那么jenkins服务器尝试将样式命令作为jenkins构建步骤来执行,那么合理的失败。我该如何解决?
答案 0 :(得分:0)
以下代码可以正常工作:
import groovy.xml.MarkupBuilder
def writer = new StringWriter()
def html = new MarkupBuilder( writer )
html.table( class: "squishSummary" ) {
style( type: "text/css" ) {
mkp.yield("123")
}
}
println writer
但是,如果您声明名为style
的变量,则会得到与您类似的错误
所以此代码将失败,并显示groovy.lang.MissingMethodException: No signature of method ...
import groovy.xml.MarkupBuilder
def writer = new StringWriter()
def html = new MarkupBuilder( writer )
def style = "my style1" //just add this line and code fails
html.table( class: "squishSummary" ) {
style( type: "text/css" ) {
mkp.yield("123")
}
}
println writer
要解决问题,请使用delegate
或GString而不是简单的方法名称:
import groovy.xml.MarkupBuilder
def writer = new StringWriter()
def html = new MarkupBuilder( writer )
def style = "style1"
html.table( class: "squishSummary" ) {
delegate.style( type: "text/css" ) {
mkp.yield("123")
}
//or like this:
"${'style'}"( type: "text/css" ) {
mkp.yield("123")
}
}
println writer