August 27, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

What Groovy Can Do For You

  • February 2, 2007
  • By Koenig with Glover, King, Laforge and Skeet
  • Send Email »
  • More Articles »

Using groovy

The groovy command is used to execute Groovy programs and scripts. For example, Listing 1 shows the obligatory Fibonacci² number sequence Groovy program that prints the first 10 Fibonacci numbers. The Fibonacci number sequence is a pattern where the first two numbers are 1 and 1, and every subsequent number is the sum of the preceding two.

If you'd like to try this, copy the code into a file, and save it as Fibonacci. groovy. The file extension does not matter much as far as the groovy executable is concerned, but naming Groovy scripts with a .groovy extension is conventional. One benefit of using an extension of .groovy is that you can omit it on the command line when specifying the name of the script-instead of groovy MyScript. Groovy, you can just run groovy MyScript.

Listing 1 Fibonacci.groovy

Run this file as a Groovy program by passing the file name to the groovy command. You should see the following output:

> groovy Fibonacci
1 1 2 3 5 8 13 21 34 55

The groovy command has many additional options that are useful for commandline scripting. For example, expressions can be executed by typing groovy -e "println 1+1", which prints 2 to the console.

In this section, we have dealt with Groovy's support for simple ad-hoc scripting, but this is not the whole story. The next section expands on how Groovy fits into a code-compile-run cycle.

Compiling and running Groovy

So far, we have used Groovy in direct mode, where our code is directly executed without producing any executable files. In this section, you will see a second way of using Groovy: compiling it to Java bytecode and running it as regular Java application code within a Java Virtual Machine (JVM). This is called precompiled mode. Both ways execute Groovy inside a JVM eventually, and both ways compile the Groovy code to Java bytecode. The major difference is when that compilation occurs and whether the resulting classes are used in memory or stored on disk.

Compiling Groovy with groovyc

Compiling Groovy is straightforward, because Groovy comes with a compiler called groovyc. The groovyc compiler generates at least one class file for each Groovy source file compiled. As an example, we can compile Fibonacci.groovy from the previous section into normal Java bytecode by running groovyc on the script file like so:

> groovyc -d classes Fibonacci.groovy

In our case, the Groovy compiler outputs two Java class files to a directory named classes, which we told it to do with the -d flag. If the directory specified with -d does not exist, it is created. When you're running the compiler, the name of each generated class file is printed to the console.

For each script, groovyc generates a class that extends groovy.lang.Script, which contains a main method so that java can execute it. The name of the compiled class matches the name of the script being compiled.

More classes may be generated, depending on the script code; however, we don't really need to care about that because that is a Java platform topic. In essence, groovyc works the same way that javac compiles nested classes.

NOTE

The Fibonacci script contains the 10.times{} construct that causes groovyc to generate a class of type closure, which implements what is inside the curly braces. This class is nested inside the Fibonacci class. If you find this confusing, you can safely ignore it for the time being.

The mapping of class files to implementations is shown in Table 1, with the purpose of each explained.

Table 1 Classes generated by groovyc for the Fibonacci.groovy file

Now that we've got a compiled program, let's see how to run it.

Running a compiled Groovy script with Java

Running a compiled Groovy program is identical to running a compiled Java program, with the added requirement of having the embeddable groovy-all*.jar file in your JVM's classpath, which will ensure that all of Groovy's third-party dependencies will be resolved automatically at runtime. Make sure you add the directory in which your compiled program resides to the classpath, too. You then run the program in the same way you would run any other Java program, with the java command.³

> java -cp %GROOVY_HOME%/embeddable/groovy-all-1.0.jar;classes Fibonacci
1 1 2 3 5 8 13 21 34 55
Note that the .class file extension for the main class should not be specified when running with java.

All this may seem like a lot of work if you're used to building and running your Java code with Ant at the touch of a button. We agree, which is why the developers of Groovy have made sure you can do all of this easily in an Ant script.

Compiling and running with Ant

An Ant task is shipped with Groovy for running the groovyc compiler in an Ant build script. To use it, you need to have Ant installed.&sup4; We recommend version 1.6.2 or higher.

Listing 2 shows an Ant build script, which compiles and runs the Fibonacci.groovy script as Java bytecode.

Listing 2 build.xml for compiling and running a Groovy program as Java bytecode

Store this file as build.xml in your current directory, which should also contain the Fibonacci.groovy script, and type ant at the command prompt.

The build will start at the run target, which depends on the compile target and therefore calls that one first. The compile target is the one that uses the groovyc task. In order to make this task known to Ant, the taskdef is used. It finds the implementation of the groovyc task by referring to the groovy. classpath in the path definition.

When everything compiles successfully in the compile target, the run target calls the java task on the compiled classes.

You will see output like this:

> ant
Buildfile: build.xml

compile:
     [mkdir] Created dir: …classes
   [groovyc] Compiling 1 source file to …classes
run:

      [java] 1 1 2 3 5 8 13 21 34 55

BUILD SUCCESSFUL
Total time: 2 seconds

Executing ant a second time shows no compile output, because the groovyc task is smart enough to compile only when necessary. For a clean compile, you have to delete the destination directory before compiling.

The groovyc Ant task has a lot of options, most of which are similar to those in the javac Ant task. The srcdir and destdir options are mandatory.

Using groovyc for compilation can be handy when you're integrating Groovy in Java projects that use Ant (or Maven) for build automation..



Page 3 of 4



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel