Faster Asset Compilation with Grails Asset-Pipeline!

Posted by David Estes on Feb 25, 2014

Filed under Grails

War building in Grails can be a time consuming process. With the addition of the asset-pipeline it can grow longer. This, however, is better than the alternative of slowing down your app startup time. Lets talk about what the asset-pipeline does during this war compiling phase.

First the build scans your current app, and all installed olugins for assets to compile. For plugins this includes the 'web-app' folder in order to facilitate easier migration. Then, the asset-pipeline scans for previously compiled assets in the target/assets folder and only recompiles the files that have actually changed. (You can wipe this cache by running grails asset-clean.)

So really, it's not that complicated of a process. There are two big factors to slowing this process down. They are, needlessly compiling assets individually, and longer compiler phases for languages like LESS, or SASS.

The asset-pipeline provides some great tricks for reducing excessive compiling that can all be configured in your apps Config.groovy file. We can use a combination of both excludes and includes patterns to restrict the files we need to process. Remember, if I have a js file that requires, for example, jquery.js, I do not need to necessarily compile jquery. that is because when my js file is processed, jquery gets bundled together with it. The same can be said for your stylesheets or your LESS imports.

To remove these files from the precompiler phase we have a few options. One trick is to prefix files you do not want to precompile with an underscore ( _ ). This works great when you might have a small mixins library that doesn't ever make sense to be compiled by itself.

Another option is to use config. We can set globally scoped excludes/includes or even control it at the plugin name level.

grails.assets.excludes = ["tiny_mce/src/.js"]
grails.assets.plugin."twitter-bootstrap".excludes = ["/.less", "/**/*.css"]
grails.assets.plugin."twitter-bootstrap".includes = ["bootstrap.less"]

That really is about all there is to it. Now you can enjoy the fastest possible WAR builds, and reduce your deployment time!

A Note For LESS Users

Recently the less-asset-pipeline plugin added support for a java native LESS compiler called less4j. Try it out by setting:

grails.assets.less.compiler='less4j'

This compiler is SIGNIFICANTLY faster.