I am trying to build a simple webpage using HTML markup but I get the error: java.lang.NoSuchMethodError: No such DSL method 'body' found among steps
Code:
@NonCPS
def parseJsonForXml(inputJson) {
try {
def writer = new StringWriter()
def markup = new MarkupBuilder(writer)
markup.html {
body {
div {
h1 "Test Page"
}
}
}
Jenkins does lots of Groovy magic like CPS transformation, custom method interceptots, etc. In short, the nested closers are not called on the markup object as they are supposed to be in groovy, instead Jenkins changes the receiver object and calls them on the main script, so it checks if the body DSL step is defined in an internal map of all DSL method defined in Jenkins and plugin. To make it work change make the caller object explicit
import groovy.xml.MarkupBuilder
@NonCPS
def parseJsonForXml() {
def writer = new StringWriter()
def markup = new MarkupBuilder(writer)
markup.with {
html {
body {
div {
h1 "Test Page"
}
}
}
}
return writer.toString()
}
println(parseJsonForXml())
It does exactly what you expect it to do
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] echo
<html>
<body>
<div>
<h1>Test Page</h1>
</div>
</body>
</html>
[Pipeline] End of Pipeline
Finished: SUCCESS