Wednesday, October 7, 2009

Grails development environment

Install Java

Install using apt-get:

sudo apt-get install sun-java6-jdk sun-java6-plugin

The following command is used to ensure the correct version is used:

sudo update-java-alternatives -s java-6-sun

To add JAVA_HOME to path for all users, edit /etc/bash.bashrc

export JAVA_HOME=/usr/lib/jvm/java-6-sun
export PATH=$PATH:$JAVA_HOME/bin

If you want to set JAVA_HOME for a single user, check this article: http://www.cyberciti.biz/faq/linux-unix-set-java_home-path-variable/.

Install Grails

Download Grails at http://grails.org/, and unzip it to /usr/lib/grails-1.1.1 (assume the version is 1.1.1, yours might be different).

Add GRAILS_HOME variable to /etc/bash.bashrc

export JAVA_HOME=/usr/lib/jvm/java-6-sun
export GRAILS_HOME=/usr/lib/grails-1.1.1
export PATH=$PATH:$JAVA_HOME/bin:$GRAILS_HOME/bin

To test the installation, type:

grails --help


Install Netbeans

Download Netbeans at http://www.netbeans.org/downloads/index.html. Select the Java bundle which includes Groovy support.

The downloaded file is a sh script. The current version is 6.7.1. Make it executable:

sudo chmod +x netbeans-6.7.1-ml-java-linux.sh

Run it to install Netbeans:

sudo ./netbeans-6.7.1-ml-java-linux.sh

This will launch the installer. Keep the default settings.

After the installation, you should have a "NetBeans IDE 6.7.1" menu item in the Ubuntu Programming menu. If you cannot find it, open System > Preferences > Main Menu, and enable NetBeans in the Programming menu.

Launch NetBeans. You might need to set GRAILS_HOME to /usr/lib/grails-1.1.1 in the Groovy tab at Tools > Options > Miscellaneous.

Configure MySQL

Grails comes pre-packaged with HSQLDB, but since we're using MySQL, we have a few quick steps to tell Grails how to talk to our database. First, download the Java MySQL driver from http://www.mysql.com/products/connector/j/.

Open the zip(or tar.gz) file and extract the mysql-connector-java-5.1.10-bin.jar file into the lib directory of your Grails application. Let's say the grails application is called 'kw'.

ls /home/david/workspace/kw/lib/

mysql-connector-java-5.1.10-bin.jar

Create an empty database, e.g. kw_dev, and grant all privileges to your database developer

create database kw_dev;
grant all on kw_dev.* to 'your_db_dev_username'@'localhost';

Edit DataSource.groovy under kw/grails-app/conf/

dataSource {
  pooled = true
  driverClassName = "com.mysql.jdbc.Driver"
  username = "" // your database username
  password = "" // your password
}
hibernate {
  cache.use_second_level_cache=true
  cache.use_query_cache=true
  cache.provider_class='com.opensymphony.oscache.hibernate.OSCacheProvider'
}
// environment specific settings
environments {
  development {
    dataSource {
      dbCreate = "create-drop" // one of 'create', 'create-drop','update'
      url = "jdbc:mysql://localhost/kw_dev"
    }
  }
  test {
    dataSource {
      dbCreate = "create-drop"
      url = "jdbc:mysql://localhost/kw_dev"
    }
  }
  production {
    dataSource {
      dbCreate = "update"
      url = "jdbc:mysql://localhost/kw_dev"
    }
  }
}