Wednesday, March 12, 2008

Scripting Deployments

Grails has a whole bunch of commands that you can execute in the form of "grails ". Where you aware those commands that you run are power by Ant? More specifically Gant. You may ask yourself "Gant what is that?" In a nutshell (or any other kind of shell that you like) Gant is Groovy-based build system that uses Ant tasks. So you have all the power of the Ant tasks that you are use to using, but now you can say "Look mom no xml!"

I think ant is a good (but not perfect) build and automation tool. However sometimes its hard to follow the xml logic. Gant brings the power of the Ant tasks with the flexibility of Groovy. Gant has its own website at http://gant.codehaus.org/. If you want to see some Gant scripts look no further than where you have Grails installed (not your grails app, but the grails home). Then have a peek in the "scripts" subdirectory. When you type "grails " Grails will look in two locations: first grails_home/scripts and then the scripts directory inside your Grails application.

I think its pretty cool that you can extend the Grails command line system by just creating a Gant script inside the scripts directory of your application. Here is a simple Gant script that says "hello" that is placed in a file called SayHello.groovy inside the scripts directory of my Grails application:


target(default:"a simple gant script") {
echo( message : "hello world" )
}


To run the script I simple type "grails say-hello". You may have noticed if your Gant script is camel-cased that when you run the script the command name is all lower case and you separate the camel-cased words with the hyphen "-". If you have existing ant scripts you can easily convert them to Gant scripts in a matter of minutes.

Like I mentioned before you can use any Ant task inside a Gant script. However let me rephrase that, "you can use Any core Ant tasks". If you want to use the optional Ant tasks there is a little more work involved. You will need to get the jar file (from the Ant distribution) containing the optional Ant task and place it in either the grails_home/lib directory or the lib directory under your Grails project. You will also need to do the same for any jar files the optional ant task requires. For example the "scp" Ant tasks requries jsc.jar.

I created a Gant script called "DeployApp.groovy", so I can run it as "grails deploy-app". Below is the code for the script that calls the standard Grails task "war" to build the war file of my Grails application and then copies it to a server:


Ant.property(environment:"env")
grailsHome = Ant.antProject.properties."env.GRAILS_HOME"
baseDir = Ant.project.properties.basedir
Ant.property ( file : 'application.properties' )

includeTargets << new File ( "${grailsHome}/scripts/War.groovy" )

args = "scott.war"

target(default: "a simple gant script") {


Ant.property ( file : 'application.properties' )
Ant.property ( file : 'environment.properties' )

def appName = Ant.project.properties.'app.name'
def appVersion = Ant.project.properties.'app.version'

def host = Ant.project.properties.'deployhost'
def port = Ant.project.properties.'deployport'
def username = Ant.project.properties.'deployusername'
def password = Ant.project.properties.'deploypassword'


echo ( message : "starting deployment" )
war()

scp(file:args, todir:"${username}@${host}", password:password,port:port)


Have fun customizing your Grails environment with custom Gant scripts :-)

No comments: