Since Grails 2.4.0 asset-pipeline
has been the default plugin recommended to manage the static resources in a Grails
application. In earlier versions of Grails the
resources plugin was an option but with
Grails 3 the only viable option is using asset-pipeline because resources is not available in Grails 3. If you want to know how to migrate from resources to asset-pipeline, this post is for you.
I recommend you start the migration analyzing your layouts because probably
that the content of your main.gsp layout is the following:
related to jQuery but all of those files are on the same directory. When doing
the migration we’re also going to reorganize the files in a better way.
stylesheets under grails-app/assets directory. Any file you copy into those
directories can be served by the plugin. The easy way would be to copy all of
the resources. For example, with the following directory structure:
You could replace the following code:
With this one:
Please notice that you don’t need to include the .js or .css extension
because asset-pipeline will add it when looking for the files in those
As I said you could just copy all those files and create a lot of entries in
If you take a look at the content of the grails-app/assets directory created
by default you can find the files application.js and aplication.css. Let’s
take a look at the content of the application.css file:
We’re defining a bundle called application (the name of the file) that will
contain bootstrap, grails, main and mobile stylesheet files. Starting
with that we’re going to add the rest of the original files we were using with
You can create arbitrary bundles of your js and css files defining files in the
root of those directories and declaring the files you want to add to that bundle.
With these two bundles defined the only thing you need to do is replace all
Now we can run the application, open the inspector and see what happens:
resources won’t be optimized and you’ll have hot-reloading of the changes. This
is the default behavior during development. For production all files will be
minified and included in one file per bundle:
One last thing about organizing the bundles is that if we want to create our
custom directories we need to create a top level directory in grails-app/assets
and inside that directory we can create our arbitrary structure. I’ve named this
directory libs but you can use the name you want:
Effectively Asset Pipeline treats each of the subdirectories of grails-app/assets as another root for locating assets.
As you have seen in this post it’s pretty simple to organize the static
resources using asset-pipeline plugin and migrate from resources.
If you want to take a closer look to the code, this example application is