Loading...

October 19, 2016

Grails Goodness: Skip Bootstrap Code

Grails normally will run any *Bootstrap classes at startup. A Bootstrap class has a init and destroy closure. The init closure is invoked during startup and destroy when the application stops. The class name must end with Bootstrap and be placed in the grails-app/init folder. Since Grails 3.2 we can skip the execution of Bootstrap classes by setting the Java system property grails.bootstrap.skip with the value true.

In the following example Bootstrap class we simply add a println to see the effect of using the system property grails.bootstrap.skip:

// File: grails-app/init/mrhaki/Bootstrap.groovy
package mrhaki

class Bootstrap {
    def init = { servletContext ->
        println "Run Bootstrap"
    }
    
    def destroy = {
    }
}

First we build the application and than start it from the generated WAR file:

$ gradle build
...
:build

BUILD SUCCESSFUL

Total time: 22.235 secs
$ java -jar build/libs/sample-app-0.1.war
Run Bootstrap
Grails application running at http://localhost:8080 in environment: production

Next we use the Java system property grails.bootstrap.skip:

$ java -Dgrails.bootstrap.skip=true -jar build/libs/sample-app-0.1.war
Grails application running at http://localhost:8080 in environment: production

Notice the println statement from Bootstrap.groovy is not invoked anymore.

Written with Grails 3.2.1