December 14, 2009

Grails Goodness: Change Context Path of a Grails Application for Jetty

By default a Grails application context path is set to the application name. The context path is the bold part in the following URL: http://localhost:8888/appname/index. We can change the context path with properties for when we use $ grails run-app to run the application. We can run $ grails -Dapp.context=/app run-app to change the context path to /app. Or we can set the property app.context = /app in Or we can add = '/app' to our conf/Config.groovy configuration file. These properties will all affect the context path when we run $ grails run-app.

But what if we want to deploy our application to Jetty on a production server and use a custom context path? We have to add an extra file to web-app/WEB-INF with the name jetty-web.xml. Here we can configure Jetty specific settings for the application and one of the settings is the context path. If the application is deployed to Jetty or we run the application with $ grails run-app the context path is used that we set in jetty-web.xml.

<?xml version="1.0" encoding="UTF-8"?>
<!-- File: web-app/WEB-INF/jetty-web.xml -->
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "">
<Configure class="org.mortbay.jetty.webapp.WebAppContext">
    <Set name="contextPath">/app</Set>