Upgrading to Grails 3.2.0 from Grails 2.3.7 with Spring Security and Deployment to Tomcat7

In this post I detail how to upgrade from grails 2.3.7 to grails 3.2.0 with spring security and Deploy a war file to Tomcat7.

Setting Up Development Environment
  • Download Grails 3.2.0 from here.
  • Download Oracle JDK 8 Update 101.
  • Optionally download Gradle from here (other wise for building war file you will need to use grails gradle war).
Make sure your JAVA_HOME and GRAILS_HOME are set properly before continuing.

Setting Up Production Environment (Tomcat7)

  • Download Oracle JRE 8 Update 101
  • Ensure Java version is Java 8 Update 101 if not use this command on ubuntu
  • sudo update-alternatives --config java
  • Modify the JAVA_HOME setting in /etc/default/tomcat7 to the java 8 path as below
  • JAVA_HOME=/usr/lib/jvm/java-8-oracle/

Migrating Grails 2 application files to Grails 3


The first step in migrating to Grails 3 is to make a new grails 3 app done by the following:
grails create-app YOUR_APP_NAME
This will create a project structure for your grails 3 application. Next Copy your grails-app directory from your grails 2 project to your grails 3 project (do copy and overwrite inorder to keep the new grails app structure). 

If you have a web-app directory, copy the web-app folder to the the grails 3 project to the src/main directory and rename the web-app folder to webapp.


In the Grails 3 grails-app folder do the following:
  • Move the UrlMappings.groovy file from the grails-app/conf folder to grails-app/controllers
  • Move the BootStrap.groovy file from the grails-app/conf folder to grails-app/init folder.
  • Copy all your settings in the Config.groovy to the application.groovy file except for logging settings, that should be copied to the logback.groovy file.
  • If you are using a different port for your server setting, place that setting in the application.groovy file (should be server.port = PORT)
  • Make sure if you have a dataSource settings in your application.groovy file, then make sure there is no dataSource in your application.yml file (this will crash your grails app if you have this setting).

Build.gradle Settings


Since Grails 3 uses gradle as a build system, you will have to copy all your plugins from your BuildConfig.groovy to the build.gradle file. Some of your plug ins may or may not work but at minimum quartz and spring security do work (see below for where to put plugins).
ext['tomcat.version'] = '7.0.52' 
dependencies {
provided "org.springframework.boot:spring-boot-starter-tomcat"
.
.
compile "org.grails.plugins:spring-security-core:3.1.1"
compile 'org.grails.plugins:spring-security-ui:3.0.0.M2'
compile "org.grails.plugins:quartz:2.0.9"
}
Make sure your spring-boot-starter-tomcat has the "provided" keyword in front other wise you will have an error with tomcat7 deployment.


If you use the mail plugin for your grails 2 application also include the following:


compile 'org.grails.plugins:mail:2.0.0.RC6'

Before you run your app


There are a few caveats before running your grails app in the grails 3 environment.
  1. No external configuration files are in grails 3, everything is self containted in the grails 3 project folder. There are ways around this by using the application.yml file for your settings.
  2. There is no more org.codehaus.groovy package anymore. The package should still be part of the grails; for instance, org.codehaus.groovy.grails.web.mapping.LinkGenerator became grails.web.mapping.LinkGenerator
  3. If any of your gsp pages has or uses .encodeAsJSON() make sure to to include "<%@page expressionCodec="none" %>" on the page
  4. If you use FindAllBy in a list make sure you check to see if your list size is greater than zero other wise don't trigger that search (you will get an error in grails).
As for Spring Security there has been renaming in the variables and methods, please check your login and logout pages for the following:

  • /j_spring_security_check changed to /login/authenticate
  • j_username changed to username
  • j_password changed to password
  • _spring_security_remember_me changed to remember-me
  • The logout controller has become the logoff controller
see the docs for more changes in spring security.

IMPORTANT: If you upload files to your grails app on the client side, make sure to adjust the maxFileSize and maxRequestSize options in your application.groovy file as grails now limits uploads to 128 KB.


grails.controllers.upload.maxFileSize = MAX_UPLOAD_SIZE_IN_KB
grails.controllers.upload.maxRequestSize = MAX_UPLOAD_SIZE_IN_KB

Run your app


Use grails run-app to test your app.
If you cannot see your static resources (css, javascript, and images) then add the following lines to your application.groovy:

grails.resources.pattern ="/**"
grails.resources.uri.prefix = ''
grails.plugin.springsecurity.controllerAnnotations.staticRules = [
    [pattern: '/',               access: ['permitAll']],
    [pattern: '/error',          access: ['permitAll']],
    [pattern: '/index',          access: ['permitAll']],
    [pattern: '/index.gsp',      access: ['permitAll']],
    [pattern: '/shutdown',       access: ['permitAll']],
    [pattern: '/assets/**',      access: ['permitAll']],
    [pattern: '/**/js/**',       access: ['permitAll']],
    [pattern: '/**/css/**',      access: ['permitAll']],
    [pattern: '/**/images/**',   access: ['permitAll']],
    [pattern: '/**/favicon.ico', access: ['permitAll']]
]
grails.plugin.springsecurity.filterChain.chainMap = [
    [pattern: '/assets/**',      filters: 'none'],
    [pattern: '/**/js/**',       filters: 'none'],
    [pattern: '/**/css/**',      filters: 'none'],
    [pattern: '/**/images/**',   filters: 'none'],
    [pattern: '/**/favicon.ico', filters: 'none'],
    [pattern: '/**',             filters: 'JOINED_FILTERS']
]

Deploying to Tomcat7 Server


Once your grails 3 application is ready for testing on a production server, make a war file with the following command:

gradle war [or grails gradle war if you don't have gradle installed]

Make sure your not using grails war since that would make a war file with an embed version of tomcat.

Once the war file is created, copy the war file to the /var/lib/tomcat7/webapps directory on the server and restart tomcat7.

Comments

Popular posts from this blog

Deployment By Hardware