Groovy Guide — What is Groovy? Scripting for the JVM
In this tutorial, you will learn about Groovy Guide. We cover key concepts, practical examples, and best practices to help you master this topic.
Groovy is a dynamic optionally-typed language for the JVM that combines the power of Java with scripting productivity -- featuring closures, builders, GStrings, seamless Java integration, and compile-time metaprogramming via AST transformations.
What You'll Learn
- The history and design of Groovy
- Optional typing and GStrings
- Closures and GDK enhancements
- AST transformations and metaprogramming
- Gradle and Grails ecosystem
Why It Matters
Groovy is the scripting language of the JVM -- used by Gradle (standard Android build system), Jenkins pipeline scripts, and Grails web applications. Durga Antivirus Pro uses Groovy for automated testing and CI/CD pipeline configuration.
Real-World Use
Every Android developer uses Groovy through Gradle build scripts. Jenkins automation pipelines are written in Groovy. Grails powers rapid web application development with convention-over-configuration.
flowchart LR
A["What is Groovy?"] --> B["GDK"]
B --> C["Closures"]
C --> D["Builders"]
D --> E["Metaprogramming"]
A:::current --> B
style A fill:#2563eb,stroke:#2563eb,color:#fff
style B fill:#dbeafe,stroke:#2563eb,color:#1e40af
style C fill:#dbeafe,stroke:#2563eb,color:#1e40af
style D fill:#dbeafe,stroke:#2563eb,color:#1e40af
style E fill:#f1f5f9,stroke:#94a3b8,color:#64748b
The History of Groovy
James Strachan created Groovy in 2003 as a scripting language for the JVM. It became an Apache project and evolved into the foundation of Gradle and Grails.
Key milestones:
- 2003: Groovy 1.0 (initial)
- 2012: Groovy 2.0 -- static compilation support
- 2015: Groovy 2.4 -- last major version under Pivotal
- 2017: Groovy 2.5 -- Apache project
- 2020+: Groovy 3.0+ -- new syntax features, continued maintenance
Optional Typing
def x = 42 // dynamic
x = "now a string" // OK
int y = 100 // static like Java
Closures
def square = { it * it }
println square(5) // 25
def eachItem = [1, 2, 3]
eachItem.each { println it }
AST Transformations
import groovy.transform.*
@ToString
class Person {
String name
int age
}
A Quick Taste
println "Hello, Groovy!"
println (1..10).sum()
println [1,2,3].collect { it * 2 }
Common Mistakes
1. Confusing == with .equals()
In Groovy, == calls .equals() automatically. Use .is() for reference comparison.
2. Modifying collections while iterating
Use findAll instead of removing during iteration.
3. Assuming closures are like Java lambdas
Closures can modify local variables and have delegate/owner properties.
4. Forgetting def in method parameters
In Groovy, parameters need either def or explicit types.
Practice Questions
1. What is a GString?
A string with embedded expressions: "Hello ${name}". Different from Java's String.
2. How do closures differ from methods? Closures are first-class values, can be passed as arguments, stored in variables, and capture scope.
3. What are AST transformations? Annotations that modify the Abstract Syntax Tree during compilation, generating boilerplate at compile time.
Challenge: Write a Groovy script using @ToString and @Canonical and demonstrate their output.
FAQ
{{< faq question="Is Groovy still relevant in 2026?" >}} Yes. Groovy is the foundation of Gradle (Android build system), Jenkins pipelines, and Grails. Its DSL capabilities make it irreplaceable for build scripts. {{< /faq >}}
{{< faq question="Should I learn Groovy or Kotlin?" >}} For Gradle or Jenkins, learn Groovy. For new JVM applications, Kotlin is more modern. Many developers learn both. {{< /faq >}}
{{< faq question="Can Groovy replace Java?" >}} For scripting, testing, and DSLs, yes. For large-scale enterprise, Java or Kotlin are better for tooling and performance. {{< /faq >}}
{{< faq question="What is Grails?" >}} A full-stack web framework built on Groovy, inspired by Ruby on Rails, with GORM, scaffolding, and Spring Boot integration. {{< /faq >}}
{{< faq question="How does Groovy handle performance?" >}} Groovy is slower than Java for compute-intensive code. Use @CompileStatic to match Java performance for hot code paths. {{< /faq >}}
What's Next
Now that you understand what Groovy is, proceed to learn the GDK (Groovy Development Kit).
| Topic | Description | Link |
|---|---|---|
| GDK | Groovy's enhanced standard library | {{< ref "02-gdk" >}} |
| Closures | Anonymous functions and DSLs | {{< ref "03-closures" >}} |
| Java | Compare Groovy with Java | Java |
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro