Using Precompiled GSP Pages and Removing Sources

If you’re builing closed-source application in Grails you’ll probably want to remove GSP sources from your war. Fortunately, war script in Grails 1.2 calls GSP pre-compiler that as a result produces Java class files. However, GSP pages remain in WEB-INF/grails-app/views directory of application’s war file, although they’re not needed any more when application is packaged for production environment (because GroovyPagesTemplateEngine runs with disabled reloadEnabled option).

So, when building a war for production you can safely remove the content of your GSP files (but don’t delete them). You can do that automatically using the following script in scripts/_Events.groovy:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
eventCreateWarStart = { warName, stagingDir ->
    if(grailsSettings.grailsEnv == 'production') {
        println "Delete content of GSP files"
        def viewDirs = [new File("${stagingDir}/WEB-INF/grails-app/views")]
        while(viewDirs) {
            def dirs = viewDirs
            viewDirs = []
            dirs.each { dir ->
                dir.listFiles().findAll {
                    if(it.isDirectory()) {
                        viewDirs << it
                    }
                    else if(it.name.endsWith(".gsp")) {
                        it.text = "This GSP file has been pre-compiled"
                    }
                }
            }
        }
    }
}

Comments