Better minification (UglifyJs) , and compiling before the WAR is built

Faster application startup time

Easy extensibility

Before we get too far, it is important to note a few structural differences. The asset-pipeline, does not store it's files in your web-app directory, but rather a new directory generated in grails-app/assets. Within that directory are various subfolder a useful for organizing your assets. It is also nice to note that you can make these sub-folders with whatever names you like and their contents are all still treated as if they are in the root structure (i.e. if I have a file assets/libs/tinymce.js and a file assets/javascripts/application.js. They are still considered as being in the same relative path as each other and application can require tinymce by simply writing //=require tinymce).

File Dependencies

In asset-pipeline, your assets can define their own requirements. The idea being your manifest shouldn't be in a separate file that you have to keep jumping back to to maintain. Asset pipeline is also smart enough to detect doubly defined requirements and discern the correct order needed for your JavaScript or CSS to load correctly without double includes. First lets look at an example JavaScript application.js file:

Notice the '//=' comments at the top of the file? Those are where you would define your "manifest" or list of dependent files. Asset-pipeline (by default) supports 3 types of "require" directives.

require_self - controls the location of which this current files contents will be placed.

require - requires a file given a relative path or absolute (if not found in relative path, the root is checked amongst all plugins)

require_tree - requires all files of similar content type in the specified directory and subdirectories (does not span plugins).

And to include into your gsp file simply use the following tag:

<asset:javascript src="application.js"/>

Pretty nifty right? But what if I require my models yet a particular model needs to ensure another model is loaded first? How do we deal with that? Easy, lets say model Book requires model Author for an association:

Notice in our book.js file we require author. And in our application js we require all models with require_tree. This little require will not cause a double inclusion and will insure a proper load order. This not only works with JavaScript, but with your CSS as well.

Assets in plugins.

Assets in plugins are treated just like assets within your app, barring a few differences. The first, most important difference is that above all, your application assets take priority. If, for example, you have book.js in your assets directory and book.js in a plugins assets directory, your applications copy will take precedence and override the other file. The other difference is that the asset path for a plugin is both the grails-app/assets folder, and the web-app folder. This small difference was done to increase the usability if legacy plugins.

Development Environment

In development mode. Your assets are compiled on the fly (for the case of Coffeescript and other compiled languages as well). You may think this is slow, but recent advancements in Rhino have made this much more performant. This means you don't have to wait for the resources plugin to detect your changes and it becomes instantly available. In development mode, your assets are also automatically included into your layout file individually based on the dependency tree of the file. This preserves order and allows for easy debugging.

Production Environment

In production, the asset-pipeline comes with some grails scripts for preparing for release. It is important that BEFORE creating your War file that you run grails asset-precompile. This script will iterate over your assets and generated compiled, gripped, and cache-digested, minified versions of your files in the web-app/assets folder. This folder is always checked for an existing asset before the asset-pipeline analyzes your source (Note: after you are done generating your War, you may want to delete this folder so you can continue using on the fly development).

Once your application is deployed, your compiled assets are now automatically served by tomcat. Options also exist to automatically copy these assets out to an external path for serving via a CDN or different web server (i.e. nginx).

Faster Startup times

Because asset-pipeline doesn't preprocess your assets at startup, a significant portion of your load time goes away. It also becomes much quicker to deploy your applications and reduces the amount of downtime your server might have during the restart.

Extensibility

With asset-pipeline it becomes easy to extend and define new asset types. Already here are a list of a few of the available extension plugins: