Groovy Reference

This content was generated with the assistance of AI and should be verified against the official documentation before being relied on in production.

The reference material consulted while preparing these pages is listed in the section bibliography.

Welcome to the Groovy reference. Apache Groovy is a dynamic, optionally typed language for the Java platform. It compiles to JVM bytecode, calls any Java library directly, and adds concise syntax on top of Java: closures, string interpolation, collection and map literals, properties, operators that map to methods, builders and domain specific languages. The same source can be a one-line script, a build file, a test, or a statically compiled library. Groovy’s standard library extensions, the Groovy Development Kit (GDK), add hundreds of methods to JDK classes such as String, File, List and Map, which is where much of its day-to-day productivity comes from.

History

Groovy was started by James Strachan in 2003 and developed at Codehaus, with a first 1.0 release in January 2007 and a specification request, JSR 241, that has been dormant since 2012. Pivotal (earlier SpringSource and VMware) sponsored its development until March 2015; Groovy then entered the Apache Incubator in March 2015 and graduated to an Apache top-level project in November 2015. Along the way the language gained annotations and generics (1.5), AST transformations and Grape (1.6), static type checking and compilation and extension modules (2.0), traits (2.3), the Parrot parser with lambdas and var (3.0), the org.apache.groovy coordinates, records, sealed types and GINQ (4.0), JDK 11 and the new groovysh (5.0), and native async/await, val, module imports and many new modules (6.0). Every release is listed with its date in What’s New and Migrating from Older Versions.

Release baseline

This section documents Apache Groovy 6.0 (6.0.0, released 2026-09-22; JDK 17 or newer), checked on 2026-10-14. Features that are newer than Groovy 4.0 are marked with the release that introduced them, and features that the official documentation labels incubating are labelled as such. On the check date the maintained lines were 6.0.0 (2026-09-22), 5.1.3 (2026-09-19), 5.0.8 (2026-07-29), 4.0.33 (2026-07-29) and 3.0.25 (2025-05-24, the last release of the 3.0 line). Frameworks and tools built on Groovy, such as Gradle, Grails, Spock, Geb, Micronaut and Jenkins pipelines, are not documented here; they are listed with a brief description and links on Ecosystem & Groovy in Other Tools, and Spock and Geb get a single page only because the official Groovy testing guide covers them.

The greenhouse scenario

The examples share one small, original domain: a greenhouse toolkit that monitors and waters a hobby greenhouse. It suits Groovy’s strengths — scripting, data processing, DSLs and Java interoperability — and grows page by page: a Plant class with properties, a Reading record, a sealed Zone hierarchy with Bench, HangingBasket and Bed, traits for watering and alerting, a small watering DSL, sensor logs in CSV, plant data in JSON, a seed catalogue in XML, TOML and YAML configuration, an in-memory H2 database queried with groovy.sql.Sql and GINQ, a report rendered with a template engine, asynchronous sensor polling and tests. A page whose concept fits the scenario poorly uses a minimal standalone example instead. Every example on these pages was run with Groovy 6.0.0 on JDK 26, and the output shown is the real output of that run (examples that use third-party tools and were not run are labelled as such on their page).

How the book was used

A copy of the first edition of Groovy in Action (Dierk König with Andrew Glover, Paul King, Guillaume Laforge and Jon Skeet, Manning Publications, 2007), which describes Groovy 1.0, was consulted as a reference for concept coverage and structure. It is not the source of any example: every example is written against the current official Groovy 6.0 documentation, the official documentation wins wherever the two differ, and no text, code listing or figure is copied from the book. Its core-language and classic-library chapters are still conceptually sound; its chapters on tooling, GroovyTestCase, Scriptom, early Grails and XML-RPC or SOAP are outdated or out of scope. Each page states in prose where older material is outdated for the concepts it touches, and What’s New and Migrating from Older Versions collects the legacy-to-modern replacements. The book’s second edition (Manning, 2015, Groovy 2.4) is the current edition; it was not used as a source.

New here? Read in this order

  1. Getting Started — install Groovy and run a first script.

  2. Differences with Java — what changes if you know Java.

  3. Running Scripts & the groovy Command — run scripts and one-liners.

  4. Strings & GStrings — string literals and interpolation.

  5. Lists & Arrays — list literals and indexing.

  6. Maps — map literals and access.

  7. Operators: Arithmetic, Relational & Logical — the everyday operators.

  8. Control Structures — if, switch, loops and their Groovy extras.

  9. Classes, Fields & Properties — classes and properties.

  10. Closures — the heart of everyday Groovy.

  11. Closure Delegation: Owner, Delegate & This — how closures resolve names.

  12. Working with Collections — the GDK collection methods.

  13. Traits — reusable behaviour.

  14. Static Type Checking & @CompileStatic — compile-time checking and static compilation.

  15. JSON — parsing and building JSON.

  16. XML Parsing, GPath & Security — reading XML.

  17. Runtime Metaprogramming & the MOP — the meta-object protocol.

  18. AST Transformations: Code Generation & Class Design — compile-time code generation.

  19. Domain Specific Languages — building DSLs.

  20. Testing with JUnit & groovy-test — testing Groovy code.

The other pages can be read in any order as you need them. Coming from an older book or tutorial? Start with What’s New and Migrating from Older Versions.

mindmap root((Groovy Reference)) Getting started Install and first script Differences with Java Groovy and other languages groovy command groovysh and groovyConsole Syntax and fundamentals Lexical structure Strings and GStrings Numbers Groovy Truth Lists and arrays Maps Ranges Variables and typing Packages and imports Operators Arithmetic and logical Object operators Regular expressions Operator overloading Control and semantics Control structures Exceptions Power assert Coercion Optionality Object orientation Classes and properties Methods and constructors Inheritance and generics Traits Records and sealed types Enums and annotations Closures and functional Closures Delegation Functional programming Typing CompileStatic Type checking extensions Built-in checkers The GDK Object methods Collections Files and paths External processes Dates and times Metaprogramming MOP Metaclasses and categories Extension modules AST transformations Writing transformations Concurrency async and await Generators and channels Parallel collections Actors and agents Data and modules JSON XML YAML and TOML CSV and Markdown Groovy SQL GINQ Templates HttpBuilder Builders DSLs and scripting Domain specific languages Customizers and secure scripting Integration and tools Embedding in Java groovyc Gradle Maven Ant Grape GroovyDoc Groovlets and JMX Performance Testing JUnit and GroovyTest Spock and Geb groovy-contracts Practice and reference Style guide Design patterns Whats new and migration Ecosystem Cheat sheet

What’s covered

Getting started

  • Getting Started — what Groovy is and where it came from, installing it (SDKMAN!, Homebrew, zip, Docker, Windows), JDK requirements per release line, Maven and Gradle coordinates and a first script.

  • Differences with Java — every documented difference from Java with a side-by-side example, from default imports and multi-methods to == and primitives.

  • Groovy & Other Languages — everyday idioms mapped from Java, Kotlin, Scala, Python and Ruby, and the trade-offs of dynamic typing.

  • Running Scripts & the groovy Command — the groovy command, -e, -n, -p, -i, the shebang line, arguments, exit codes and the classpath rules.

  • groovysh & groovyconsole — the JLine 3 REPL and the Swing console, their commands, history and embedding.

Syntax and fundamentals

Operators

  • Operators: Arithmetic, Relational & Logical — arithmetic, relational, logical, bitwise, ternary and Elvis operators with the precedence table.

  • Object Operators — safe navigation, direct field access, method pointers and references, spread, spaceship and subscript operators.

  • Regular Expressions — the pattern, find and match operators, groups, slashy strings and the GDK regex helpers.

  • Operator Overloading — the operator-to-method table, comparison rules and compound-assignment overloading.

Control structures and semantics

  • Control Structures — if, switch with isCase, switch expressions, loops, labels and the position rule.

  • Exceptions & Error Handling — try/catch/finally, multi-catch, try-with-resources and the dynamic exceptions.

  • Assertions & Power Assert — assert, power-assert output and assertions as executable documentation.

  • Type Coercion & Promotion — casts versus as, number promotion, closure to SAM coercion and map and list coercion.

  • Optionality — what may be left out: parentheses, semicolons, return and public.

Object orientation

Closures and functional programming

Typing

The GDK

  • GDK Overview & Object Methods — how the GDK extends the JDK, the Object methods, ConfigSlurper, Expando and observable collections.

  • Working with Collections — iterating, filtering, transforming, aggregating, sorting and combining collections.

  • Files I/O & Paths — reading, writing and traversing files and Path objects.

  • External Processes — running commands with execute(), process streams, pipes and timeouts.

  • Dates & Times — java.time extensions, formatting, parsing and the TimeCategory duration DSL.

Metaprogramming

Concurrency

Data and module guides

DSLs and scripting

Integration and tools

Testing

Practice and reference

Bibliography

The reference material consulted while preparing this section. The official Apache Groovy documentation is the primary source for every page; the book is a consulted reference for concept coverage only, and the third-party projects are cited only on the ecosystem and testing pages. On any discrepancy, the official documentation wins. No text, code listing or figure is copied from any book or documentation page. Each page lists the subset it derives from under its own == References heading.

Official Apache Groovy documentation

Specifications and standards

Book

  • Dierk König, with Andrew Glover, Paul King, Guillaume Laforge and Jon Skeet, Groovy in Action, Manning Publications, 2007. ISBN 1-932394-84-2 (978-1-932394-84-9). Written for Groovy 1.0; consulted for concept coverage only. The current edition is the second edition (Manning, 2015, Groovy 2.4), which was not used as a source.

Third-party projects (cited only on the ecosystem, testing and tools pages; none is documented further)