Compiling Groovy code

Groovy scripts are normally executed by running the groovy command from the console; for example:

groovy someScript.groovy

The groovy command generates the JVM bytecode on the fly and immediately executes it. There are scenarios in which the Groovy code is required to be compiled to bytecode as a *.class file. A typical case is when Java and Groovy code have to be used side-by-side for building a mixed Groovy/Java application.

Groovy has an answer to that by offering a compiler command, groovyc (similarly to Java's javac), that can also be invoked from the command line or through a build tool such as Ant (see the Integrating Groovy into the build process using Ant recipe), Maven (see the Integrating Groovy into the build process using Maven recipe), or Gradle (see the Integrating Groovy into the build process using Gradle recipe).

In this recipe, we are going to show you the principal purposes of the groovyc command.

Getting ready

The best way to show the Groovy compiler in action is to actually invoke it on some Groovy code.

Let's create a file named fizzbuzz.groovy and add the following code to it:

1.upto(100) {
   ans = ''
   if (it % 3 == 0) { ans ='Fizz' }
   if (it % 5 == 0) { ans += 'Buzz' }
   if (ans == '') {
     println it
   } else {
     println ans
   }
}

The code prints numbers from 1 to 100. For numbers that are multiples of three, it prints Fizz, and for the multiples of five, it prints Buzz. For numbers which are multiples of both three and five, it prints FizzBuzz. This little coding exercise was made popular by Jeff Atwood back in 2007 in his CODING HORROR blog http://www.codinghorror.com/blog/2007/02/why-cant-programmers-program.html.

How to do it...

Once the file is ready, let's make sure it prints Fizz and Buzz where it has to:

  1. Open the console and type:
    groovy fizzbuzz.groovy
  2. And now, let's call the Groovy compiler from the same command line:
    groovyc fizzbuzz.groovy
  3. The compiler should generate two *.class files:
    fizzbuzz$_run_closure1.class and fizzbuzz.class.
  4. Finally, we can now try to run the compiled Groovy files using the standard Java command:

    On Windows:

    java -cp ".;%GROOVY_HOME%lib*" fizzbuzz

    On Linux/OS X type:

    java -cp ".:$GROOVY_HOME/lib/*" fizzbuzz
  5. The output of our little program should be printed on the screen as follows:
    1
    2
    Fizz
    4
    Buzz
    Fizz
    7
    8
    Fizz
    Buzz
    11

How it works...

The reason groovyc produces two class files lies in the dynamic nature of Groovy and the usages of special constructs—closures—in our code snippet. The code block that appears after the each statement is actually a closure that can be stored and passed as a data structure in Groovy. Closures are discussed in more detail in the Defining code as data in Groovy recipe in Chapter 3, Using Groovy Language Features. Groovy creates a separate internal class for each such dynamic code block. The main script code is placed in fizzbuzz.class.

Also, as you probably noticed, we added all the Groovy distribution libraries to the classpath since compiled Groovy code relies on their functionality.

See also

For single file compilation, groovyc is easy to use, though if you need to compile dozens or even hundreds of classes, then it would be better to use a build tool for that:

  • Integrating Groovy into the build process using Ant
  • Integrating Groovy into the build process using Maven
  • Integrating Groovy into the build process using Gradle
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.251.57