What is this book about?

Groovy is an extremely powerful language yet simple and fun to use. It can be used for simple scripting (like Python or bash) or for building complex application. Run-time speed was used to be an issue, but with each release Groovy gets faster and close to native Java performances. The language is not difficult to learn for someone with a Java background and after a bit it becomes clear that Groovy is more fun and productive than Java, mostly because of the excessive verbosity of the Java language. Furthermore, Groovy has features - such as closures - that allow Java developers to try out functional programming and get closer to languages like Scala or Clojure as well as Java 8.

The book contains a number of recipes that put the language to work in different areas of software development and interesting new technologies, such as JSON/REST, DSL, NoSQL. Also, there are non-trivial recipes that can actually be used in real life development.

Why would I read this book?

Groovy cookbook is definitively an interesting read for developers who are attracted by the “getting things done” approach and are ready to get their hands dirty without much ceremony. The recipes in the book can help new developers to get acquainted with the language and experienced ones to access advanced features.

Known side effects

  • Religious worship of Groovy
  • Might break the time and space barrier by programming so fast
  • Envy from other programmers
  • How long did it take?

    1,5 years
  • Flower depicted on the cover

  • Weight

  • Printed chapters

  • eBook chapters

  • Recipes

  • Paragraphs

  • Words

    54 019
  • Code blocks

  • LOC

  • Groovy is mentioned

    650 times
  • Java is mentioned

    195 times
  • Most popular word (after Groovy)

    class (404)
  • Recipe/chapter

  • Paragraph/chapter

  • Word/chapter

  • LOC/chapter

  • Code block/chapter

  • Paragraph/recipe

  • Word/recipe

  • LOC/recipe

  • Code block/recipe



  1. Getting Started with Groovy
    • Installing Groovy on Windows
    • Installing Groovy on Linux
    • Executing Groovy code from the command-line
    • Using Groovy as a command-line text file editor
    • Using Groovy to start server on the command-line
    • Running Groovy with invokedynamic support
    • Building Groovy from source
    • Installing Groovy on Linux
    • Using groovysh to try out Groovy commands
    • Starting groovyConsole to execute Groovy snippets
    • Configuring Groovy in Eclipse
    • Configuring Groovy in IntelliJ IDEA
  2. Using Groovy Ecosystem
    • Using Java classes from Groovy
    • Embedding Groovy into Java
    • Compiling Groovy code
    • Simplifying dependencies management with Grape
    • Integrating Groovy into build process using Ant
    • Integrating Groovy into build process using Maven
    • Integrating Groovy into build process using Gradle
    • Generating documentation for Groovy code
    • Checking Groovy code quality with CodeNarc
  3. Using Groovy Language Features
    • Searching strings with regular expressions
    • Writing less verbose Java Beans with Groovy Beansa
    • Inheriting constructors in Groovy classes
    • Adding cloning functionality to Groovy Beans
    • Defining code as data in Groovy
    • Defining data structures as code in Groovy
    • Implementing multiple inheritance in Groovy
    • Adding functionality to existing Java/Groovy classes
    • Defining type checking rules for dynamic code
    • Adding automatic logging to Groovy classes
  4. Working with Files in Groovy
    • Reading from a file
    • Reading a text file line by line
    • Processing every word in a text file
    • Writing to a file
    • Replacing tabs with spaces in a text file
    • Filtering a text file content
    • Deleting a file or directory
    • Walking through a directory recursively
    • Searching for files
    • Changing file attributes on Windows
    • Reading data from a ZIP file
    • Reading an Excel file
    • Extracting data from a PDF
  5. Working with XML in Groovy
    • Reading XML using XmlSlurper
    • Reading XML using XmlParser
    • Reading XML content with namespaces
    • Searching in XML with GPath
    • Searching in XML with XPath
    • Constructing XML content
    • Modifying XML content
    • Sorting XML nodes
    • Serializing Groovy Beans to XML
  6. Working with JSON in Groovy
    • Parsing JSON messages with JsonSlurper
    • Constructing JSON messages with JsonBuilder
    • Modifying JSON messages
    • Validating JSON messages
    • Converting JSON message to XML
    • Converting JSON message to Groovy Bean
    • Using JSON to configure your scripts
  7. Working with Databases in Groovy
    • Creating a database table
    • Connecting to an SQL database
    • Querying an SQL database
    • Modifying data in an SQL database
    • Calling a stored procedure
    • Reading BLOB/CLOB from a database
    • Building a simple ORM framework
    • Using Groovy to access Redis
    • Using Groovy to access MongoDB
    • Using Groovy to access Apache Cassandra
  8. Working with Web Services in Groovy
    • Downloading content from the Internet
    • Executing an HTTP GET request
    • Executing an HTTP POST request
    • Constructing and modifying complex URLs
    • Issuing a REST request and parsing a response
    • Issuing a SOAP request and parsing a response
    • Consuming RSS and ATOM feeds
    • Using basic authentication for web service security
    • Using OAuth for web service security
  9. Metaprogramming and DSLs in Groovy
    • Querying methods and properties
    • Dynamically extending classes with new methods
    • Overriding methods dynamically
    • Adding performance logging to methods
    • Adding caching functionality around methods
    • Adding transparent imports to a script
    • DSL for executing commands over SSH
    • DSL for generating reports from log files
  10. Concurrent Programming with Groovy
    • Processing collections concurrently
    • Downloading files concurrently
    • Splitting a large task into smaller parallel jobs
    • Running tasks in parallel and asynchronously
    • Using actors to build message-based concurrency
    • Using STM to atomically update fields
    • Using dataflow variables for lazy evaluation
  11. Testing with Groovy
    • Unit testing Java code with Groovy
    • Testing SOAP web services
    • Testing RESTful services
    • Writing functional tests for web applications
    • Writing behavior-driven tests with Groovy
    • Testing the database with Groovy
    • Using Groovy in soapUI
    • Using JMeter and Groovy for load testing


Luciano Fiandesio

Luciano Fiandesio

Luciano Fiandesio is a programmer, technology enthusiast and entrepreneur living in Zurich, Switzerland. Luciano has been working for the last 18 years in 12 different countries as architect and developer for large corporations and small start-ups: Nokia, European Central Bank, BNP Paribas, Ericsson are among his clients. He loves coding and designing solutions that are both elegant and rock solid. When not busy learning the next big thing, he likes playing with his analogue cameras and cooking Italian food. He holds a Master Degree in Literature and Philosophy from Rome University.

Luciano has been using early versions of Groovy since 2005 to simplify the development of a B2B application. Since then Groovy (and Grails) has been an essential tool in his arsenal for accelerating the development process (and having fun coding). Luciano has deployed Groovy/Grails based solution in companies across Europe.

Andrey Adamovich

Andrey Adamovich

Andrey Adamovich is a software craftsman with many years of experience in different lifecycle phases of software creation. He is passionate about defining good development practices, documenting and presenting architecture, reuse of code and design patterns, profiling and analysis of application performance as well as extreme automation of development and operations activities.

Andrey Adamovich is a long-time Groovy user and has a deep knowledge of the language internals. Andrey uses Groovy in his day to day development job for simplifying the development process, which includes: code generation, super cool DSLs, rapid prototyping.

Special thanks!

Many different people (shown in alphabetical order) have helped shaping book’s content through their thorough reviews:

  • Lena Adamovich
  • Dmitry Buzdin
  • Kunal Dabir
  • Ayan Dave
  • Fergal Dearle
  • Matteo Fiandeso
  • Eric Kelm
  • Guillaume Laforge
  • Laura Santi
  • Pierluigi Vernetto
  • Dmitry Zakharov
  • Sergey Zavrotschi

Packt employees guided us through authoring, editing and publishing process:

  • Apeksa Chitnis
  • Jinesh Kampani
  • Sandeep Madnaik
  • Chandni Maishery
  • Kartikey Pandey
  • Tania Rana
  • Ankita Shashi
  • Ankita Thakur