Search code examples
xmlgrailsgroovyweb.xmlgrails-plugin

Changing web.xml value from doWithWebDescriptor closure in Grails Plugin


We're writing a Grails plugin and need to have the plugin modify the web.xml file. We're adding entries (that part is already done) and we also need to modify existing entries in the web.xml. That's the part we're having trouble with.

According to the documentation the parameter passed into the doWithWebDescriptor closure is supposed to be a GPathResult object. So we assumed we could do the following (using session timeout as an example):

def doWithWebDescriptor = { xml ->
  xml.'session-config'.'session-timeout'.replaceBody(60)
}

But that throws the following exception:

groovy.lang.MissingMethodException: No signature of method: groovy.xml.dom.DOMCategory$NodeListsHolder.replaceBody() is applicable for argument types ...

This exception makes it look like the parameter passed into this closure isn't actually a GPathResult object, but a DOMCategory$NodeListsHolder object, something there is virtually no documentation on.

We've also tried other approaches, like using replaceNode, etc. Nothing seems to work.


Solution

  • Try this instead.

    def doWithWebDescriptor = { xml ->
        xml.'session-config'.'session-timeout'[0].replaceNode {
            'session-timeout' 60
        }
    }