- 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
- 深入淺出Java虛擬機:JVM原理與實戰(zhàn)
- Hadoop+Spark大數(shù)據(jù)分析實戰(zhàn)
- Python程序設計案例教程
- Learning Apache Kafka(Second Edition)
- Java項目實戰(zhàn)精編
- Learning Salesforce Einstein
- C++面向對象程序設計習題解答與上機指導(第三版)
- Java系統(tǒng)化項目開發(fā)教程
- Java零基礎實戰(zhàn)
- 深入實踐Kotlin元編程
- Practical Microservices
- Java程序設計基礎(第6版)
- Hack與HHVM權威指南
- 征服C指針(第2版)
- JBoss AS 7 Development