What Groovy is
Groovy is an object-oriented language for the Java platform. It compiles to the bytecode a Java compiler writes, so a Groovy class is an ordinary JVM class, Java libraries need no adapter, and a Java class can call a Groovy one without knowing it is Groovy. The language is dynamic by default: a variable can be untyped, a call is resolved at run time, and a script needs neither a class nor a main method.
A first script is one line, and the closures that Java added in version 8 have been in Groovy since 2007:
hello.groovy
def languages = ['Groovy', 'Java', 'Kotlin']
languages.each { name ->
println "Hello, ${name}"
}
groovy hello.groovy
Hello, Groovy
Hello, Java
Hello, Kotlin
Java underneath
A Groovy script runs on the same virtual machine as Java, with the same standard library. The script below builds a Java list, writes into it, and calls a Java static method; nothing in it belongs to Groovy except the missing types and the missing semicolons.
interop.groovy
List<String> names = new ArrayList<>()
names.add('Groovy')
names.add('Java')
println String.join(' and ', names)
println Integer.parseInt('42') + 1
println names.getClass().name
groovy interop.groovy
Groovy and Java
43
java.util.ArrayList
Where Groovy is used
Gradle build scripts are Groovy: a build.gradle file is a script that Gradle compiles and runs, and the Gradle course reads one line by line. Spock, the test framework of the JUnit platform, writes its specifications in Groovy. The Grails framework builds web applications with it, Jenkins pipelines are Groovy scripts in a Jenkinsfile, and SoapUI runs Groovy as the script language of its test steps.
The other use is scripting: a Java application embeds the Groovy runtime and lets a user extend it with a script, and a one-file program does a job that would otherwise need a build and a project layout.
What Groovy adds
- Optional ceremony. A statement needs no semicolon, a call needs no parentheses, and a script needs no class. The first script lesson shows all three.
- Strings that interpolate. A double-quoted string is a GString, and ${...} inside it is evaluated; the strings lesson covers the five forms.
- Collections with literal syntax and methods. [1, 2, 3] is a list, [name: 'Ada'] is a map, and collect, findAll and groupBy replace most loops.
- Closures. A block of code is a value that can be stored, passed, curried and composed.
- Metaprogramming. A method can be added to a class at run time, and a builder turns nested calls into XML, JSON or a build script.
- Optional static typing. @CompileStatic moves the checks back to the compiler and the speed back to Java's.
Where to go next
The next lesson installs the language and reads the version line; Install Groovy also links the install pages of each system. The Groovy errors page collects the messages a beginner meets, and the Groovy DSL lesson of the Gradle course shows the language at work in a build.
Related
- Install Groovy and check the version: the JDK, a system install and groovy --version.
- Java: the platform and the language Groovy extends.
- Gradle: the build tool whose scripts are Groovy.
- JUnit 5: the platform the Spock lessons run on.
Автор статьи: Arthur Isaev