Getting started with Grails for .net developers

I have been using the .Net framework for the past 12 years and my new career choice (Bioinformatics) requires me to use Linux and open source things like grails (Groovy on RAILS). Setting up grails and IntelliJ was quite tough and there was very little (end to end) step by step help available so I thought I would provide some.

When I say ‘setting up’, the actual grails/java environment and apache (web server) was not too hard but things like debugging, database connectivity, ORM’s, css and scripts (through something extravagantly named ‘the asset pipeline’) and plugins would (and did) have the visual studio, nuget dependent developer throwing their toys out of the cot.

I must add that now I am using it, and my environment is setup nicely, grails is quite good, definitely comparable to .Net Mvc (though Groovy is not quite C#)

As with all linux things, from a beginners perspective anyway, it involves coupling together nuts and bolts, stuff usually left to the good people at Microsoft, leaving you staring at your spanner in amazement. The steps can be broken down as follows:

  • Install Java
  • Install grails
  • Setup path variables
  • Install intelliJ (there are others but this was the one I chose)
  • Create and run a web application

Pretty straight forward and there are many guides on this, however the next stuff is painful if you are doing it for the first time;

  • Setting up debugging -this seems highly convoluted for something so simple with visual studio
  • Connecting to a mysql database
  • Configuring an ORM (I choose hibernate)
  • Setting up css and scripts (in a way that compares to .net bundling)
  • Installing addons/plugins (like you would with nuget)

Other things still to do/figure out (obviously these are from MS perspective, I am sure these are dealt with if not in other ways);

  • IOC -using the Spring framework this can apparently be done UPDATE: done here
  • Unit testing and mocking (Grails automatically creates UT stubs using something called spock but they dont compile automatically)
  • Messenger buses -RabbitMQ
  • Services/scheduled console apps
  • Something like LINQ
  • REST interfaces

Installing Java, Grails and IntelliJ

1. install java - run this command from a terminal window - sudo apt-get install openjdk-7-jre
2. Open .bashrc in the home directory and add the follow to the bottom
export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-i386
export PATH=$PATH:$JAVA_HOME/bin
3. Install grails - I downloaded the binary from here and then set the path (could be an easier way using sudo or something) and put it in the home directory
4. Open .bashrc in the home directory and add the follow to the bottom
export GRAILS_HOME=~/grails-2.3.11
export PATH=$PATH:$GRAILS_HOME/bin
5. Install IntelliJ -I did it through the command line but you can do this through the linux software center
6. Install apache, this guide will tell all you need to know 
7. In a command line create a folder for your first app and type grails create-app “APPLICATION-NAME” in the app directory
8. In a command line (in the app directory) run command: grails run-app and go to http://localhost:8080/ APPLICATION-NAME
9. Open grails application in intellij to edit

Now for the fun stuff -very simple when listed like this but tough using the ttrial and error method…….

Debugging

1. I replaced the fork line in the BuildConfig.goovy
grails.project.fork = [ test: false, run: false, war    : [maxMemory: 768, minMemory: 64, debug: false, maxPerm: 256, forkReserve: false], console: [maxMemory: 768, minMemory: 64, debug: false, maxPerm: 256]]
2. In command terminal in the app directory run grails run-app --debug-fork it should say Listening for transport dt_socket at address: 5005
3. In intelliJ go to menu → Run → Debug → Edit configuration → Add new (plus at top of dialog) → Remote → give it a name and accept defaults and create
4. Then Run → Debug and select the configuration you just created. This should attach to the lister ran up from the terminal

Notes: dont put a breakpoint on a brace as it won’t stop -I think I actually got debugging working long before I discovered this

Addins/Plugins

1. In place of Nuget there is something called Maven which seems to work quite well
2. Go to the icon in the top right (tooltip Project Structure)
3. Go to Modules → Add (+ on right) → library → From Maven → search for your package and click OK

Database connectivity

1. Add the following to Datasource.goovy
dataSource {
    pooled = false
    driverClassName = "com.mysql.jdbc.Driver"
    configClass = org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsAnnotationConfiguration
} 
// environment specific settings
environments {
    development {
        dataSource {
            dbCreate = "update"
            //one of 'create', 'create-drop','update'
            url = "jdbc:mysql://localhost/DBNAME?" +
                    "useUnicode=yes&characterEncoding=UTF-8"
            username = "root"
            password = PASSWORD
        }
    }
    test {
        dataSource {
            dbCreate = "update"
            url = "jdbc:mysql://localhost/DBNAME?" +
                    "useUnicode=yes&characterEncoding=UTF-8"
            username = "root"
            password = PASSWORD
        }
    }
    production {
        dataSource {
            dbCreate = "update"
            url = "jdbc:hsqldb:file:prodDb;shutdown=true"
            username = "root"
            password = PASSWORD
        }
    }
}

2. I didn’t bother with adapter type queries, I went straight to an ORM

ORM’s

1. I used hibernate and to be honest wasted so  much time trying to use Maven and tutorials but ended up knee deep in duplicate log references and incompatible versions etc. so I cheated.
2. I downloaded a clean version of this application from git and brought my code across.

Note: the grails update command he refers to in the readme is not in the latest version of grails (which I suspect is not a stable release) so I rolled back to 2.3.11

2. Some basic usage:

package hollyportal.LIMS

class Patient {
String name
String dob
String urn
String sex
Date dateAdded
static constraints = {
name(nullable: false)
dob(nullable: false)
urn(nullable: false)
sex(nullable: true)
dateAdded(nullable: false)
}
}

Entity -using annotations

Setting up css and javascript -using the dramatically named ‘asset pipeline’

1. This is actually quite simple but not obvious
2. Uncomment the following line in the BuildConfig.groovy file
compile ':asset-pipeline:1.9.4'
3. Run the app from the command line (grails run-app) and it will create an assets folder in the grails application, which you can add images, js and css.
4. You can then simply include it in the layout page (main.gsp by default) and it will magically find it and bundle it
<asset:stylesheet src="bootstrap.css"/>
<asset:javascript src="jquery-2.1.1.js"/>

I now have a running, debuggable web application with a database and ORM and properly manages assets.

As I start using grails commercially I will obviously find better ways to do some of these things and understand it better, but I found getting to this stage allowed me to really get into grails.

Leave a comment