Before I introduce *Grails *to you, I want to write a little bit about Groovy, because it is used as the main language in grails.

What is Groovy?

Groovy is a dynamic language build on top of Java Virtual Machine whose features are inspired by the success of languages like Python, Ruby and smalltalk.

Lets go forth step by step.

Groovy is a language for Java Virtual Machine

What does that mean? It mean that groovy can be compiled into bytecode to run on JVM. No groovy interpreter/compiler is required on the target machine, when you are planning to run your compiled groovy code. All you need is just JVM.

You can use java libs in groovy easily. It means that groovy from the start is able to reuse all existing Java libraries. It also means that you do not need to throw away your old Java code!

Calling groovy code from Java is also possible!

Groovy is a dynamic language

It supports dynamic typing. So you can write code like this:

def myVar = "Text";

println "Value of var: $myVar";

myVar = 5 * 20;

println "Value of var: $myVar";

Running this code will produce following output:

Value of var: Text
Value of var: 100

Groovy supports closures.

What is a closure? If you are familiar with C/C++ you can think about a closure as pointer to function.

def sayHi = { name ->
    println "Hi, $name!"

// Call closure
sayHi("groovy learner!")

I guess you can tell that output will be “Hi, groovy learner!”. Closures can be used as function arguments, of course :)

def sayHi = { name ->
    println "Hi, $name!"

def callCode(def code, def param) {

callCode(sayHi, "closure as argument!")

Try to guess what output will be!

Groovy is really good for scripting

Have you noticed that there are no classes in the previous examples? Groovy does not require your compilation file to have a class. It allows you to write a script in groovy (with no public class with *main *method), compile it into a java class and execute. You will only need groovy script runtime jar in your class path.

As a part of being good for scripting, groovy provides easy dependency management using Grapes. You can defined in your script which additional libraries you need and groovy will download them when script is executed. No need to package all those .jar‘s with your script!

And one more good thing about it is that Codehaus guys decided not to reinvent the wheel. They did not create their own “package” format. Instead Grapes make use of maven. Yes. You can use any jar from maven repository in your groovy script. And you can achieve that with a single line of code. It also means that all libraries, required by your dependency, will be downloaded as well! Here follows an example of script which uses Google Guava library:

@Grab(group='', module='guava', version='11.0.2')
// Look Ma! I am using import from library that
// will be downloaded only in runtime!

println "2 in power of 10: " + LongMath.pow(2, 10)

Groovy is really easy to learn

If you already know java. Or Ruby. Or python. Or any other high-level programming language.

As for Java, consider following code:

package com.binarybuffer.groovy;

public class EntryPoint {
public static void main(String[] args) {
       System.out.println("Hello world!");

Does it look like a regular Java class? Well, it is also a valid Groovy class! So you already can write code in groovy. What are you waiting for?! Go on, download it and start having fun.

Additional resources: