Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Environment-specific web.xml in grails?

What's the best way to build environment-specific web.xml entries in grails?

I need to make certain modifications for production only, as they break running locally.

Any thoughts?

like image 419
Stefan Kendall Avatar asked May 04 '11 19:05

Stefan Kendall


2 Answers

You can create scripts/_Events.groovy with an event handler for the 'WebXmlEnd' event which is fired once Grails and the plugins have finished making their changes. Update the XML with plain search/replace or via DOM methods by parsing the XML and write out the updated file:

import grails.util.Environment

eventWebXmlEnd = { String filename ->

   if (Environment.current != Environment.PRODUCTION) {
      return
   }

   String content = webXmlFile.text

   // update the XML
   content = ...

   webXmlFile.withWriter { file -> file << content }
}
like image 158
Burt Beckwith Avatar answered Sep 21 '22 08:09

Burt Beckwith


Here's the solution that's i'm using, from the guy over at death-head.ch

first install the templates

grails install-templates

then customize the web.xml you'll find in src/templates/war/web.xml. I chose to make a web_dev.xml and a web_prod.xml and delete the web.xml. I wanted web_prod.xml to contain a security-constraint block. anyway...

Place the following in BuildConfig.groovy:

// #########################################################
// ## Can't use environment switching block because BuildConfig doesn't support it.
// ##    @url http://jira.grails.org/browse/GRAILS-4260
// ## So use this workaround:
// ##   @url http://death-head.ch/blog/2010/09/finally-solved-the-base-authentication-in-grails/
// #########################################################

switch ("${System.getProperty('grails.env')}") {
  case "development":
    if (new File("/${basedir}/src/templates/war/web_dev.xml").exists()) {
        grails.config.base.webXml = "file:${basedir}/src/templates/war/web_dev.xml"
    }
    break;
  default:
    if (new File("/${basedir}/src/templates/war/web_prod.xml").exists()) {
        grails.config.base.webXml = "file:${basedir}/src/templates/war/web_prod.xml"
    }
    break;
}

Good luck!

like image 30
Tak Avatar answered Sep 23 '22 08:09

Tak