Over a million developers have joined DZone.

Book Review: Groovy 2 Cookbook

DZone's Guide to

Book Review: Groovy 2 Cookbook

· Java Zone
Free Resource

What every Java engineer should know about microservices: Reactive Microservices Architecture.  Brought to you in partnership with Lightbend.

I was excited to review Groovy 2 Cookbook (Packt Publishing, 2013) by Andrey Adamovich and Luciano Fiandesio because I have been very happy with two other cookbook-style Groovy books (Groovy Recipes: Greasing the Wheels of Java [Pragmatic Programmers, 2008] and Groovy and Grails Recipes  ) and because this is the first cookbook-style Groovy book covering  Groovy 2  (the other two were written well before Groovy 2). In fact, the only two Groovy books currently available in print that I'm aware of that cover Groovy 2 are this book and  Programming Groovy 2: Dynamic Productivity for the Java Developer [Pragmatic Bookshelf, 2013] (the  next edition of Groovy in Action is likely heavy on Groovy 2 topics). The subtitle of  Groovy 2 Cookbook is "Over 90 recipes that provide solutions to everyday programming challenges using the powerful features of Groovy 2."

The Preface of Groovy 2 Cookbook starts the book with this quote (and I agree with the authors' stated opinion about Groovy's flexibility):

Groovy 2 Cookbook consists of problem-solving recipes for Groovy, one of the most flexible programming languages we have ever used. This book contains solutions to more than 100 common problems, shown with more than 500 Groovy code snippets.

Groovy 2 Cookbook contains 11 chapters in roughly 370 pages with numerous screen snapshots and code listings.

Intended Audience and Prerequisite Background

The Preface states that Groovy 2 Cookbook "covers Groovy 2.0 and 2.1." It also describes the intended audience of the book as "Java and Groovy developers who have an interest in discovering new ways to quickly get the job done using the Groovy language." The Preface states that "no extensive Groovy experience is required to understand and use the code and the explanations accompanying the examples" because "book's recipes start simple" and the assumed knowledge includes "general knowledge of computer science, data structures, complexity, and concurrent programming."

General Content

The Preface has a brief description of each chapter, but very little description is needed as the names of the recipes outlined in the Table of Contents gives a pretty good idea of the topics covered in Groovy 2.0 Cookbook. For example, Chapter 1 is called "Getting Started with Groovy" and that chapter's recipes' titles give a pretty clear description of what is covered. The eleventh chapter of Groovy 2 Cookbook is not actually contained in the book itself, but a link to it online is available in the brief summary of Chapter 11 that exists in the Preface.

In a sign of the maturation of Groovy and increased awareness of Groovy, this chapter does not focus on basics of the Groovy language and does not spend pages comparing Groovy to Java (a short section in this Preface covered that very briefly.) Instead, the recipes of the first chapter focus on installation of Groovy on different operating systems, using Groovy from the command line, using Groovy with groovysh, and configuring IDEsEclipse and IntelliJ IDEA (but not NetBeans) to work with Groovy. Note that both Groovy Recipes and Groovy and Grails Recipes devote considerable pages and a number of recipes each to an introduction to the Groovy language and contrasting and comparing it with Java.

The second chapter focuses on integrating Groovy and Java, which I consider one of Groovy's biggest advantages when compared to other scripting languages and when compared even to other JVM-based languages. The chapter includes coverage of the Groovy Adaptable (Advanced) Packaging Engine (Grape) and integratingGroovy with Ant and Maven (GMaven), Gradle, and Groovydoc.

The recipes in the first two chapters introduced Groovy features in an incidental manner as recipes were covered. Chapter 3 amplifies this effect and really shows off Groovy features via its recipes. As part of this, the advantages of Groovy compared to Java for certain applications are more clearly seen. Features covered includeregular expression support, easier JavaBean-style classes with Groovy (including use of @Canonical), usingGroovy's DSL capabilities, and using Groovy's logging support.

It could be argued that my primary use of Groovy is as a scripting language to read and manipulate files. Many of the types of things I like to do with Groovy are covered in Chapter 4 of Groovy 2 Cookbook. The chapter covers use of Groovy to read and write files and to manipulate text content of a file. The chapter also covers reading a file in Zip format, an Excel file (using Apache POI), and a PDF file (using iText).

Chapter 5 ("Working with XML in Groovy") and Chapter 6 ("Working with JSON in Groovy") contains recipes detailing some of the feature I most miss from Groovy when writing code in Java: XML and JSON writing, parsing, and manipulation.

After file reading/writing/manipulation (including XML/JSON), database access is probably the thing I do next most with Groovy and that is the subject of Chapter 7 ("Databases in Groovy"). The majority of the recipes in this chapter on Groovy and the database address SQL/relational databases (HyperSQL 2.3.0 specifically), but the final three recipes of the chapter look at using Groovy with NoSQL databases Redis (using Jedis), MongoDB(using GMongo), and Apache Cassandra (using Hector).

The eighth chapter of Groovy 2 Cookbook is on working with web services in Groovy with recipes cover SOAP-based and REST-based web services implemented with Groovy.

Chapter 9 ("Metaprogramming and DSLs in Groovy") covers subjects that may be more mind-bending for traditional Java developers than other Groovy features covered so far. These are the "heavy-hitting" features that make many of the seemingly smaller features and conciseness of Groovy possible. One recipe introduced in this chapter demonstrated ImportCustomizer, a Groovy class I had not previously used or even been aware of. Discovering this reminded me that although this book has "Groovy 2" in its title, it also covers Groovy 1.x features that came after other cookbook-style Groovy books were published. For example, ImportCustomizerwas introduced with Groovy 1.8 and so Groovy 2 Cookbook can cover it when other cookbook-style Groovy books published before Groovy 1.8 obviously could not.

I was pleased to see that Groovy 2 Cookbook's tenth chapter is devoted to "concurrent programming in Groovy." The introduction to this chapter explains that "most of the recipes in this chapter will use the awesome GPars(Groovy Parallel Systems) framework." Chapter 10 is the final chapter in the printed or electronic books.

The eleventh chapter, on testing with Groovy, is accessible online instead; the link to it is found in the section of the Preface that briefly summarizes each chapter of the book. This chapter includes recipes covering unit testing of Java with Groovy, testing web services (SOAP-based and REST-based),

Groovy 2 Features

As would be expected, there is a lot of overlap between the three cookbook-style Groovy books in terms of types of recipes covered. The biggest differentiating factor between Groovy 2 Cookbook and the other two cookbook-oriented Groovy books I referenced earlier is that Groovy 2 Cookbook includes Groovy 2 features instead of covering Java-to-Groovy transition as much as the other two. Some of the Groovy 2 features covered in recipes in Groovy 2 Cookbook include application of invokedynamic for compiling and running Groovy code and static type checking with @groovy.transform.TypeChecked.

As stated above, Groovy 2 Cookbook not only demonstrates features new to Groovy 2 that aren't covered in older Groovy books but also covers features introduced in the later 1.x releases that came out after many of the most popular Groovy books. In particular, I have found Groovy 1.8 to be the "minimally acceptable" version of Groovy for my uses because so many nice features were added in that release and this book covers many of them.

Miscellaneous Observations

Who I Recommend This Book For

Groovy 2 Cookbook is a fine book that covers many of the most important Groovy features in a problem-solving approach expected for cookbook-style books. It is up against stiff competition as Groovy Recipes and Groovy and Grails Recipes are both fine books as well. If a developer could only buy one of these books, factors to consider would be how familiar the developer is with Groovy already and whether the developer would rather having the book focus on introduction to Groovy with a focus on Groovy 1.0 (Groovy Recipes or Groovy and Grails Recipes might be favored in that case) or would rather have less introduction to and background on Groovy and have more focus on Groovy 2 and later Groovy 1.x features (Groovy 2 Cookbook might be favored in this latter case). All three books have their own advantages and disadvantages and I've enjoyed and learned something from all three. One other distinguishing feature of Groovy 2 Cookbook is that it seems to me to incorporate third-party Java and Groovy libraries, frameworks, and toolkits in its recipes more than the other two cookbook-style cookbooks.

Although I have learned things about Groovy and its ecosystem from all three cookbook-style books on Groovy, there's no question that there is significant overlap among the three books. One of the best ways for a developer to decide which to purchase might be to browse the table of contents of each book because their respective recipes' titles indicate what is covered in each book.

For any Java developer out there with little or no Groovy experience who wants to learn Groovy from a cookbook-style book, I'd probably recommend Groovy Recipes, but that might be because that's where most of my early Groovy learning came from. Groovy 2 Cookbook is approachable for those new to Groovy, but will probably be even more useful to someone with a minimum amount of Groovy experience (at least read an article or blog or two on Groovy). For any developer who wants a cookbook that heavily covers Grails, then the aptly titled Groovy and Grails Recipes is the best choice.


Of the three cookbook-style Groovy books, Groovy 2 Cookbook is understandably the best of the three at covering "modern Groovy." As its title suggests, it covers Groovy 2, but it also covers later versions of Groovy 1.x that were significant and came out after the first two Groovy cookbook-style books were released. In addition, it covers the broader Groovy ecosystem and related products available in 2013 (year of its publication) better than the other two Groovy cookbook-style books.

Microservices for Java, explained. Revitalize your legacy systems (and your career) with Reactive Microservices Architecture, a free O'Reilly book. Brought to you in partnership with Lightbend.


Published at DZone with permission of Dustin Marx, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}