11 Jun 2012

Grails Development Environment Setup

Grails 0 Comment

This is a part-1 of a series of how to configure and setup a complete environment (development, build & deploy) for a grails application.  The following are the various series that we would be working on ..

Overview

A typical development environment contains the following

  • SCM: Source Code Management system.  In this case we will be using GIT.  Industry has various SCM products like Perforce, SVN, CVS, ClearCase and so on..
  • Continuous Integration Server (Hudson/Jenkins):  It pulls all the code changes committed by developers, does the automatic builds, runs all the required tests & then publishes the artifacts into the artifact repository.
  • Artifact Repository: This acts as a dual purpose, one it acts as a proxy for most of the third party open source libraries so that we need not go over the net all the time to download the libraries and two our own applications and libraries can now be published on this server.
  • Dev: Development boxes generally where developers pull the code from a version control (scm), make the required code changes and then push/commit the changes back in the source code version control system.
  • deployer: a host which can be used to pull the required artifacts an push to the appropriate stacks. Its not necessary to have a different host just for this purpose.  We could potentially using any one of the host in one of the stack to act as a deployment machine.
  • stacks (qa1, stage1, prod): but these are the various stacks where the apps are deployed for testing prior to releasing in production.

Dev environment

Typical Flow

  • 1a. code pull: Developer gets the required code from SCM.  1b. artifact pull: as part of the maven/grails build process the required libraries are automatically downloaded.
  • 2. code push: after making the changes the modified code is pushed to the server/SCM.
  • 3. code pull: Hudson/Continuous integration server pull the latest modified code.
  • 4a. build: builds are automatically kicked off.
  • 4b. unit test: all unit tests are run.
  • 4c. integration tests: all integration tests are run.
  • 5. publish artifacts:  The newly generated artifacts after all builds & tests are successful are then published to the “artifact repo”.
  • 6a. pull artifacts: As part of the deployment scripts, the newly generated artifacts are downloaded from the “artifact repo” & subsequently pushed to the stack
  • 6b. deploy: as the second step in the process the new artifacts are now pushed to the appropriate stacks.

Installation & Configuration

We currently use Git as a SCM & will assume that GIT is already installed. In this section we will be looking at how to configure Sonatype & grails dev environment to talk to each other.

  • Install Artifact Repository (Sonatype)
  • Configure Artifact Repository (Sonatype)
  • Configure Grails to use local repository
  • Publish the plugin to local repository

 Install Artifact Repository (Sonatype)

    1. Download Sonatype (~/Downloads/nexus-2.0.5-bundle.tar.gz)
    2. Install Sonatype as per instructions

Screen Shot 2012 06 11 at 6 03 07 AM

Configure Artifact Repository (Sonatype)

    1. Go To http://localhost:8081/nexus/, remember in actual installation this will be something like http://repo.comany.com:8081/nexus
    2. Login admin/admin123
    3. Click “Security” -> “Users”, select “deployment” user and “Set Password”.
    4. Add Hosted Release repository (nbos-grails). Click “Repositories” -> “Add” -> “Hosted Repository
    5. Add Hosted Snapshot repository (nbos-grails-snapshot). make sure we select the appropriate “Repository Type” we should now see two additional repos as show in the picture.

Configure Grails to use local repository

we will now try to create a plugin (grails-sample-plugin) and an application that uses this plugin (grails-sample-app)

clone or create an empty sample plugin grails-sample-plugin. I already had an empty grails-sample-plugin repo on our Git. so quickly cloned it and we will create an empty grails plugin.

cd ~/work
git clone git@scm.nbostech.com:sandbox/nbos-grails-plugin.git

lets create the sample plugin configure grails artifact resolver log level to verbose ..

[~/work]$ grails create-plugin nbos-grails-plugin
| Created plugin NbosGrailsPlugin
[~/work]$

configure grails-app/conf/BuildConfig.groovy to start using our local repo.

grails.project.dependency.resolution = {
 .....

  log "info" // log level of Ivy resolver, either 'error', 'warn', 'info', 'debug' or 'verbose'
  repositories {

    mavenRepo "http://localhost:8081/nexus/content/groups/public"
    //grailsCentral()
    .....
 }
}

lets try to build & package the plugin, we should notice that all the artifact requests are now going thru our local repository.

[~/work/nbos-grails-plugin] cd nbos-grails-plugin;
[~/work/nbos-grails-plugin]$ grails package-plugin
| Compiling 11 source files

we should see grails mentioning that its download the libraries from the local repository now

| Downloading: ezmorph-1.0.6.jar.sha1
[SUCCESSFUL ] net.sf.ezmorph#ezmorph;1.0.6!ezmorph.jar (45ms)
downloading http://localhost:8081/nexus/content/groups/public/xerces/xercesImpl/2.8.1/xercesImpl-2.8.1.jar ...

and the plugin package created

| Plugin packaged grails-nbos-grails-plugin-0.0.1.zip
[~/work/nbos-grails-plugin]$  ls *.zip
grails-nbos-grails-plugin-0.1.zip
[~/work/nbos-grails-plugin]$

we can confirm that by checking the number of files under sonatype_work directory

[~/applications/nexus-2.0.5]$ find ../sonatype-work/ | wc -l
1678
[~/applications/nexus-2.0.5]$

Publish the plugin to local repository

Lets now publish the plugin to our local repository. In order to deploy the plugin to a maven repository we have to first configure grails to point to local repo http://localhost:8081/nexus.  Note this is going to be different based on where the artifact repo is setup.  All we need to do is edit ~/.grails/settings.groovy and put the following.

// snapshot repo
grails.project.repos.nbossnapshot.url = "http://localhost:8081/nexus/content/repositories/nbos-grails-snapshot"
 grails.project.repos.nbossnapshot.username = "deployment"
 grails.project.repos.nbossnapshot.password = "deploy"

  // release repo
 grails.project.repos.nbos.url = "http://localhost:8081/nexus/content/repositories/nbos-grails"
 grails.project.repos.nbos.username = "deployment"
 grails.project.repos.nbos.password = "deploy"

 //
 grails.project.repos.default = "nbos"

and then install a plugin called release plugin that we need to use.

[~/work/grails-sample-plugin]$ grails install-plugin release

...

| Plugin installed.

and  then deploy our new artifact/plugin, if our artifact is a snapshot we should use “–repository=nbossnapshot”

[~/work/nbos-grails-plugin]$ grails maven-deploy --repository=nbos

...

| Plugin packaged grails-nbos-grails-plugin-0.0.1.zip
| Using configured username and password from grails.project.repos.nbos...
Maven deploy complete.

we should now be able to get to the plugin using our local repo url and also in the Sonatype UI