Archive for the ‘Groovy’ category

Groovy Recipes

May 2nd, 2009

groovyrecipesSimply put this book will save you a lot of time.

The Groovy Recipes book by Scott Davis contains many examples of very common activities that will help anyone!

  • Share/Bookmark

Groovy + Ant = GANT

December 3rd, 2008

gantVenkat Subramaniam has a theory about XML. He says:

Like humans, XML starts out cute when it’s small and gets annoying when it becomes larger.

If you have the same sentiment as Venkat, you will appreciate Gant. Gant is a powerful tool for scripting Ant task by writing Groovy code instead of XML. Below you will find samples from a Gant script and a explanation on what the code does. I wont go through the entire script but you can get it here.

Notice: The script calls a property file that you will need to create. The property file specifies locations for different directories. If you downloaded the script and you want to run it, make sure you change the file name to build.gant. To run the script, navigate to the location of the file and type gant.

In a nutshell, you write a Gant script by:

1. Declaring a target:

target ( ‘nameOfTarget’ : ‘ Description of Target ‘ ) {

}

2. Adding tasks inside the target:

targer ( ’sampleTarget’ : ‘This is a sample target’) {

ant.doSomething(more code)

ant.doSomething(more code)

}

Lets start examining and explaining the script:

First two lines of the script:

Ant.property(file: ‘build.properties’)
def antProperty = Ant.project.properties

These are the first two lines of the script and they are simply saying, “Hey! I have a file called build.properties that I will be using during this process and I will refer to it with the word antProperty. So whenever you see the word antProperty, I want you to look inside the build.properties file for the value”. This is the file that you will need to create in order to build your project.

First target definition in the script:

target( ‘build’ : ‘This target build the project’) {
ant.echo(’Hello I am starting the build process now please be patient’)
depends(resources)
depends(webcontent)
depends(compile)
depends(war)
}

This is the first target of many for the script. This section of code simply creates a target called build. The first task in this target is to print out a line. This is done with the echo command, ant.echo. Just like Ant, it simply prints out the statement declared inside the quotes. Finally we are saying, “Hey! This target depends on the completion of other targets called resources, webcontent, compile and war!”

First path definition in the script:

target( ‘resources’ : ‘Grabs resources such as the property files’) {
ant.echo(”I am grabbing all the resources”)
ant.mkdir(dir: antProperty.’dumpPropertiesHere’)
ant.copy(todir: antProperty.’dumpPropertiesHere’) {
path(refid: ‘properties’)
}
}
def properties = ant.path(id: ‘properties’) {
fileset(dir: antProperty.’getPropertiesFiles’) {
include(name: ‘*.properties’)
}
}

This piece of code has another target and a extra path definition. The resources target is going to copy some files into a specified location. Again, the first line simply prints the string inside the quotes.

The second line is simply creating a directory with the ant.mkdir task. The name for the directory is specified by calling the properties file.

The third line is going to copy files into the directory we created with the ant.mkdir task. The ant.copy(todir: antProperty.’dumpPropertiesHere’) is saying, “Yoooo machine, dump some stuff in here! You can find the files I need in a place called properties

Finally we define where this properties location is. We accomplish this by using the ant.path task. In the first line we give it an id. The fileset task is simply saying “The files that I want are in here and please include all files ending in .propeties“.

These tasks that I just described above cover about 70% of the code that you will find in my script. The following tasks will describe other important actions you will need to build your project.

Compiling your code:

ant.javac(srcdir: antProperty.’getSrc’, destdir: antProperty.’setClassesHere’) {
classpath() {
path(refid: ‘buildCP’)
}
}
}

The task ant.javac is the command you use to compile your Java code. In this task you are passing two parameters, the first parameter tells the system where to find the source code you want to compile, while the second parameter tells the application where to set the classes after they are compiled.

The classpath() task is basically just telling the system where you find the classes it will need in order to compile your code.

Creating a war file:

target ( ‘war’ : ‘Creates the .war’) {
ant.echo(’Starting a war!’)
ant.jar(destfile: antProperty.’gant.war.file’, basedir: antProperty.’buildWarWith’, includes: ‘**’)
}

Here we call the ant.jar task in order to create the war file. The first parameter is the name of the .war file. The second parameter points the system to the location where it will find all the files that it needs. Finally, we tell the system to include all the files in that base directory.

setDefaultTarget(build)

Finally setDefaultTarget tells the system which target to start with.

As you probably noticed, one of the advantages of using Gant over basic Ant is the expressiveness you can achieve by using Groovy instead of XML. I originaly wrote this Gant script to begin my training in Groovy syntax, after a couple of minutes I found myself mostly copying and pasting the code since I had so many targets. So even thougth I think this is a good way of learning the basics of Groovy syntax the biggest benefit was being able to use this script on other projects and not having to look at XML. Please let me know know if you have any questions or comments about the script, or if you have any feedback on how I could make it better.

  • Share/Bookmark

Programming Groovy

November 30th, 2008

proggroovyJust finished reading Programming Groovy by Venkat Subramaniam.

This is a great book for anyone interested in learning about Groovy. As a beginner Groovy programmer, this book helped me understand all the benefits of using Groovy over plain old Java. Venkat also does an amazing job of explaining the metaprogramming features of Groovy in a way that is both fun and easy to comprehend.

If you are interested in Groovy, I recommend you get a copy of this book. I know I will keep referring to it as I get more experience with Groovy.

println ‘Thanks Venkat!’

  • Share/Bookmark

Installing Groovy on Ubuntu

November 11th, 2008

This is how I installed Groovy on Ubuntu. (Notice I run everything as sudo on my laptop)

The first step in this process is to get all the necessary files.

  1. If you dont have a Java JDK you will need to download one (I am using JDK 6).
  2. Download Groovy .zip file.

The second step is to install the files

  1. For java just place the JDK in the directory that you want, navigate to the directory you dumped the file and then run sh jdk-6u10-linux-i586.bin (Remember I am using Java 6 so the name of the file might be different for you.)
  2. For groovy, again place the downloaded file in the directory that you want. unzip the file by typing unzip groovy-binary-1.5.7.zip. (Again name of the file might change depending on the version that you downloaded.)

The third and final step in this process is to modify the PATH environment variable and add the JAVA_HOME and GROOVY_HOME to the environment variables.

  1. Edit the environment file by typing gedit /etc/environment
  2. Add and modify as necessary the following lines to the /etc/environment file.
  3. Modify the Path variable by adding the Groovy bin directory:

    PATH=”/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:
    /usr/<locationOfGroovy>/<groovyVersion>/bin”

    Add the following variables:

    GROOVY_HOME=”/usr/<locationOfGroovy>/<groovyVersion>/”
    JAVA_HOME=”/usr/<locationOfJava>/<jdkVersion>

This is what my /etc/environemnt file looks like:

PATH=”/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/tools/groovy-1.5.7/bin”
LANG=”en_US.UTF-8″
GROOVY_HOME=”/usr/tools/groovy-1.5.7/”
JAVA_HOME=”/usr/tools/jdk1.6.0_10″

Once you are done, save the file, log out and log back in, change to sudo (remember I run everything as sudo) and now groovy should work. And if it doesn’t YOU messed up, because my instructions are never wrong (kidding).

You can test by typing groovyConsole in the terminal. A console should come up where you can start writting groovy code. But you should know this console is NOT and IDE, this is just a place to type and test code if needed.

  • Share/Bookmark