- Groovy 2 Cookbook
- Andrey Adamovich Luciano Fiandesio
- 577字
- 2021-07-23 15:57:22
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:
- Open the console and type:
groovy fizzbuzz.groovy
- And now, let's call the Groovy compiler from the same command line:
groovyc fizzbuzz.groovy
- The compiler should generate two
*.class
files:fizzbuzz$_run_closure1.class and fizzbuzz.class.
- 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
- 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
- MySQL數(shù)據(jù)庫管理實(shí)戰(zhàn)
- JavaScript全程指南
- Production Ready OpenStack:Recipes for Successful Environments
- Learning Linux Binary Analysis
- Mastering Python High Performance
- PhpStorm Cookbook
- Ext JS 4 Web Application Development Cookbook
- 51單片機(jī)C語言開發(fā)教程
- Android項(xiàng)目實(shí)戰(zhàn):手機(jī)安全衛(wèi)士開發(fā)案例解析
- Unity 3D/2D移動(dòng)開發(fā)實(shí)戰(zhàn)教程
- Learning YARN
- 網(wǎng)絡(luò)數(shù)據(jù)采集技術(shù):Java網(wǎng)絡(luò)爬蟲實(shí)戰(zhàn)
- 寫給大家看的Midjourney設(shè)計(jì)書
- Instant Apache Camel Messaging System
- WordPress Search Engine Optimization(Second Edition)