- 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
- SQL Server 從入門(mén)到項(xiàng)目實(shí)踐(超值版)
- Android應(yīng)用程序開(kāi)發(fā)與典型案例
- Linux C/C++服務(wù)器開(kāi)發(fā)實(shí)踐
- 劍指Offer(專項(xiàng)突破版):數(shù)據(jù)結(jié)構(gòu)與算法名企面試題精講
- Learning Data Mining with Python
- HBase從入門(mén)到實(shí)戰(zhàn)
- Eclipse Plug-in Development:Beginner's Guide(Second Edition)
- UML 基礎(chǔ)與 Rose 建模案例(第3版)
- Creating Stunning Dashboards with QlikView
- Java程序設(shè)計(jì)案例教程
- Java圖像處理:基于OpenCV與JVM
- Maker基地嘉年華:玩轉(zhuǎn)樂(lè)動(dòng)魔盒學(xué)Scratch
- Python Linux系統(tǒng)管理與自動(dòng)化運(yùn)維
- 創(chuàng)新工場(chǎng)講AI課:從知識(shí)到實(shí)踐
- 深入大型數(shù)據(jù)集:并行與分布化Python代碼