servletsgrailsgrails-3.0contextpath

Grails 3, change contextPath dynamically (servletContext?)


I have a problem changing the contextPath of an grails 3 web application dynamically. It's working if I change it in groovy.gsp (server.contextPath = "/myApp"), but i need to deploy it with different names like that:

myApp_A.war         =>  IP:Port/myApp_A
myApp_B.war         =>  IP:Port/myApp_B

In can get the direcory name (llike: "myApp_A" for myApp_A.war) and read the right properties file dynamically (myApp_A.properties). After that I'm trying to set the context path for this instance dynamically to IP:Port/myApp_A

I tried to change the grails "server.contextPath" in the bootstrap like this:

Holders.config.'server.contextPath' = '/myApp_A'

But this is not working completely. In this case I will be redirected to IP:Port/myApp_A, but then I got 404. The webservice is still listening to the static configurartion ("/myApp").

I think I have to change it earlier (in bootstrap it's too late), or change it directly in the servletContext. After changing Holders.config.'server.contextPath' to the new path, the servletContext is still containing the old static path (servletContext.getContextPath() = old path).

In:

class Application extends GrailsAutoConfiguration implements EnvironmentAware { 
...

I can read my settings and inject them before the servletContext is created. Any ideas how I can set the contextPath there?

Regards, grailsfan


Solution

  • In application.groovy, write your logic to set the context path.

    You can set server.contextPath = "path_a".

    So

    if ( myCondition == true ) {
        server.contextPath = "path_a"
    } else {
        server.contextPath = "path_b"
    }