Hosting

Saturday, April 16, 2016

Asset Pipeline Plugin for Grails

This is a very useful plugin which can help your website load faster and manage your static files easily.

How it works:


  1. To use this plugin you need to make an entry in the BuildConfig.groovy file.
  2. compile "org.grails.plugins:asset-pipeline:2.7.4". Use updated from here
  3. Once you add the above line and clean run your project, there will be a new folder created in your grails-app folder named assets.
  4. This folder has 3 sub folders: images, javascripts, stylesheets ( you can also add your own custom folder here )
  5. Place your files in the respective folders.
  6. You can then use the following tags to include the respective files:
  7. <asset:javascript src="application.js"/> <asset:stylesheet href="application.css"/>
  8. By default the bundling of JS and CSS files is disabled in development mode to ease the debuggin. 
  9. In order to enable it in development mode add this to your Config.groovy file: grails.assets.bundle=true
  10.  Asset also adds a feature other than just minifying the JS and CSS files. 
  11. It can combine multiple JS files into a single JS file. Adv: This will reduce the number of http request which are send out for each JS file. Thus reducing the page load time. This JS will then be cached.
  12. Same is the case for your CSS files.
  13. In order to budle the files, you need to create a JS file say app.js
  14. This file will contain the files which you want to combine in the following manner:
  15. //This is a javascript file with its top level require directives
    //= require  jquery
    //= require_self
    //= require bootstrap/bootstrap.min
  16. Require self will also include the JS code within app.js. Now in the above example, we will have 3 files merged into one single js file. i. e Jquery, app.js, bootstrap.min.js ( app.js and bootstrap.min.js are present in assets folder )
  17. In order to bundle CSS files, again create a new CSS file app.css.
  18. Add the following lines:
  19. /*
    *= require_self
    *= require bootstrap/bootstrap.min
    *= require bootstrap/font-awesome*/
  20. This will then combine 3 files into one. i. e app.css, bootstrap.min.css and font-awesome.css
  21.  When ever we create a war, these JS and CSS files are compiled and the minified version is created. Now asset by default appends an MD5 of the content within the file to its name.
  22. For applications which need to provide customer support and have to deliver hotfixes later have a difficulty in managing these files. As the name of file at the client will not match with one which has a hotfix.
  23. In order to overcome this issue, you can disable the digest which will disable the renaming of the files. You can add the following lines to your Config.groovy file:
  24. grails.assets.enableDigests = false // this will disable appending digest to minified static files
    grails.assets.skipNonDigests = false // this will not skip the non digest files
     
     
     

Thursday, June 11, 2015

Upgrading from grails 1.3.x to 2.5.x

1.    Setup the environment for grail 2.5.x
2.    Create a new project using 2.5.x with the same name as your old project.
3.    You may refer this link http://grails.github.io/grails-doc/2.4.4/guide/upgradingFrom23.html
4.    Once you have create the new project, hit the command “grails install-templates
5.    This is because there is a change in the web.xml ( sitemesh has been removed from the xml )
6.    Now if you have some changes in your xml which were made, then compare your old with the    new one and merge the changes ( src/templates/war )
7.    Main thing is the applicationContext.xml compare and merge the changes. In this
          <bean id="grailsApplication" class="org.codehaus.groovy.grails.commons.GrailsApplicationFactoryBean">
                    <description>Grails application factory bean</description>
                   <property name="grailsDescriptor" value="/WEB-INF/grails.xml" /> 
                   <property name="grailsResourceLoader" ref="grailsResourceLoader" />
          </bean>

The striked out line has been removed.

8.    If you were using the Holder classes i. e
       The following deprecated classes have been removed from Grails 2.4.x:
       •    org.codehaus.groovy.grails.commons.ApplicationHolder
       •    org.codehaus.groovy.grails.commons.ConfigurationHolder
       •    org.codehaus.groovy.grails.plugins.PluginManagerHolder
       •    org.codehaus.groovy.grails.web.context.ServletContextHolder
       •    org.codehaus.groovy.grails.compiler.support.GrailsResourceLoaderHolder
     If you or any plugins you have installed are using these classes you will get a compilation error.   The problem can be rectified by updating to new plugins and using grails.util.Holders instead.
9.    If you were using spring security for login, the SynchronizerToken class has been changed to  SynchronizerTokensHolder.
10.    This is the link to code https://github.com/gramant/grails-core-old/blob/master/grails-web/src/main/groovy/org/codehaus/groovy/grails/web/servlet/mvc/SynchronizerTokensHolder.groovy
11.    You now have to generate a token if its not present initially generateToken(url);
12.    In your gsp layout file use <r:layoutResources/>. This should come after the plugin are added.
         Example:
                       <g:javascript library="jquery" plugin="jquery"/>
                       <r:layoutResources/>
13.    Here is the plugins which I used in my BuildConfig.groovy
plugins {

                build ":tomcat:7.0.55"
                compile ':scaffolding:2.1.1'
                compile ':cache:1.1.8'
                runtime ':resources:1.2.8'
                runtime ":hibernate:3.6.10.18"//":hibernate4:4.3.5.4" // or ":hibernate:3.6.10.16"
                runtime ":jquery:1.11.1"
                compile ":remote-pagination:0.4.8"
                compile ":spring-security-core:1.2.7.4"
                compile ":spring-security-ldap:1.0.6"
                // Uncomment these (or add new ones) to enable additional resources capabilities
                //runtime ":zipped-resources:1.0.1"
                //runtime ":cached-resources:1.1"
                //runtime ":yui-minify-resources:0.1.5"
}


Upgrading from Grails 1.3.x to 2.4.x

1.    Setup the environment for grail 2.4.x
2.    Create a new project using 2.4.x with the same name as your old project.
3.    You may refer this link http://grails.github.io/grails-doc/2.4.4/guide/upgradingFrom23.html
4.    Once you have create the new project, hit the command “grails install-templates
5.    This is because there is a change in the web.xml ( sitemesh has been removed from the xml )
6.    Now if you have some changes in your xml which had made, then compare your old with the new one and merge the changes ( src/templates/war )
7.    Main thing is the applicationContext.xml compare and merge the changes. In this
      <bean id="grailsApplication" class="org.codehaus.groovy.grails.commons.GrailsApplicationFactoryBean">
               <description>Grails application factory bean</description>
               <property name="grailsDescriptor" value="/WEB-INF/grails.xml" /> 
               <property name="grailsResourceLoader" ref="grailsResourceLoader" />
      </bean>

The striked out line has been removed.

8.    If you were using the Holder classes i. e
       The following deprecated classes have been removed from Grails 2.4.x:
       •    org.codehaus.groovy.grails.commons.ApplicationHolder
       •    org.codehaus.groovy.grails.commons.ConfigurationHolder
       •    org.codehaus.groovy.grails.plugins.PluginManagerHolder
       •    org.codehaus.groovy.grails.web.context.ServletContextHolder
       •    org.codehaus.groovy.grails.compiler.support.GrailsResourceLoaderHolder
      If you or any plugins you have installed are using these classes you will get a compilation error.     The problem can be rectified by updating to new plugins and using grails.util.Holders instead.
9.    If you were using spring security for login, the SynchronizerToken class has been changed to SynchronizerTokensHolder.
10.    This is the link to code https://github.com/gramant/grails-core-old/blob/master/grails-web/src/main/groovy/org/codehaus/groovy/grails/web/servlet/mvc/SynchronizerTokensHolder.groovy
11.    You now have to generate a token if its not present initially generateToken(url);
12.    In your gsp layout file use <r:layoutResources/>. This should come after the plugin are added.
         Example:
                       <g:javascript library="jquery" plugin="jquery"/>
                       <r:layoutResources/>
13.    Here is the plugins which I used in my BuildConfig.groovy
plugins {

                build ":tomcat:7.0.54"
                compile ':scaffolding:2.1.1'
                compile ':cache:1.1.6'
                runtime ':resources:1.2.8'
                runtime ":hibernate:3.6.10.16"//":hibernate4:4.3.5.4" // or ":hibernate:3.6.10.16"
                runtime ":jquery:1.11.1"
                compile ":remote-pagination:0.4.8"
                compile ":spring-security-core:1.2.7.4"
                compile ":spring-security-ldap:1.0.6"
                // Uncomment these (or add new ones) to enable additional resources capabilities
                //runtime ":zipped-resources:1.0.1"
                //runtime ":cached-resources:1.1"
                //runtime ":yui-minify-resources:0.1.5"
}