Book Image

Groovy for Domain-Specific Languages, Second Edition

By : Fergal Dearle
Book Image

Groovy for Domain-Specific Languages, Second Edition

By: Fergal Dearle

Overview of this book

Table of Contents (20 chapters)
Groovy for Domain-specific Languages Second Edition
About the Author
About the Reviewers
Free Chapter
Introduction to DSLs and Groovy


The Java virtual machine runs on everything from the largest mainframe to the smallest microchip and supports every conceivable application. But Java is a complex, and sometimes arcane, language to develop with. Groovy allows us to build targeted single-purpose mini languages, which can run directly on the JVM along with the regular Java code.

This book provides a comprehensive tutorial on designing and developing mini Groovy-based domain-specific languages (DSLs). It is a complete guide to the development of several mini DSLs with a lot of easy-to-understand examples. This book will help you gain all of the skills needed to develop your own Groovy-based DSLs.

Groovy for Domain-specific Languages, Second Edition, guides you from the basics through to the more complex metaprogramming features of Groovy. The focus is on how the Groovy language can be used to construct domain-specific mini languages.

Practical examples are used throughout to demystify these seemingly complex language features and to show how they can be used to create simple and elegant DSLs. The examples include a quick and simple Groovy DSL to interface with Twitter.

The book concludes with a chapter focusing on integrating a Groovy-based DSL in such a way as the scripts can be readily incorporated into your own Java applications. The overall goal of this book is to take developers through the skills and knowledge they need to start building effective Groovy-based DSLs to integrate into their own applications.

What this book covers

Chapter 1, Introduction to DSLs and Groovy, discusses how DSLs can be used in place of general-purpose languages to represent different parts of a system. You will see how adding DSLs to your applications can open up the development process to other stakeholders in the development process. You'll also see how, in extreme cases, the stakeholders themselves can even become co-developers of the system by using DSLs that let them represent their domain expertise in the code.

Chapter 2, Groovy Quick Start, covers the basics of installing Groovy and running simple Groovy scripts.

Chapter 3, Essential Groovy DSLs, covers two essential Groovy-based tools, Gradle and Spock. Gradle is a build, test, and deployment automation tool, which is powered by a Groovy DSL. Spock is a unit testing and specification framework built over JUnit. Both tools are used extensively throughout the book.

Chapter 4, The Groovy Language, covers a whistle-stop tour of the Groovy language. It also touches on most of the significant features of the language as a part of this tour.

Chapter 5, Groovy Closures, covers closures in some depth. It covers all of the important aspects of working with closures. You can explore the various ways to call a closure and the means of passing parameters. You will see how to pass closures as parameters to methods, and how this construct can allow the adding of mini DSL syntax to our code.

Chapter 6, Example DSL – GeeTwitter, focuses on how we can start with an existing Java-based API and evolve it into a simple user-friendly DSL that can be used by almost anybody. You'll learn the importance of removing boilerplate code and how you can structure our DSL in such a way that the boilerplate is invisible to our DSL users.

Chapter 7, Power Groovy DSL Features, covers all of the important features of the Groovy language, and looks in depth at how some of these features can be applied to developing DSLs.

Chapter 8, AST Transformations, covers how to use the Groovy abstract syntax tree (AST) transformations. AST transformations are a mechanism for us to hook into the Groovy compilation process. Here we look at compile time metaprogramming and see how we can use AST transformations to build code on the fly during the compilation process.

Chapter 9, Existing Groovy DSLs, discusses some existing Groovy DSLs that are in current use and are free to download.

Chapter 10, Building a Builder, explains how Groovy provides two useful support classes that make it much simpler to implement our own builders than if we used the MOP. You'll see how to use BuilderSupport and FactoryBuilderSupport to create our own builder classes.

Chapter 11, Implementing a Rules DSL, takes a look at Groovy bindings to see how they can be used in our DSL scripts. By placing closures strategically in the binding, you can emulate named blocks of code. You can also provide built-in methods and other shorthand by including closures and named Boolean values in the binding. These techniques can be used to great effect to write DSL scripts that can be read and understood by stakeholders outside of the programming audience.

Chapter 12, Integrating It All, takes all the knowledge from the previous chapters and builds a fully functioning web application based on a simple Game Engine DSL for Tic Tac Toe.

What you need for this book

It is highly recommended that you download the example code to use while you read the book. You will also need to download and install three key pieces of software, Groovy, Spock, and Gradle. Download and installation instructions for these are included in Chapter 2, Groovy Quick Start, and Chapter 3, Essential Groovy DSLs.

Who this book is for

This book is for any software developers who have an interest in building domain scripting into their applications. No knowledge of Groovy is required, although it will be helpful. This book will not teach Groovy, but will quickly introduce the basic ideas of Groovy. An experienced developer should have no problems with this and will move quickly onto the more involved aspects of creating DSLs with Groovy. No experience of creating a DSL is required.

The book should also be useful for experienced Groovy developers who have so far only used Groovy DSLs, such as Groovy builders, and would like to start building their own Groovy-based DSLs.


In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "Before you go to the Groovy documentation for MarkupBuilder to look for the book, author, and surname methods in MarkupBuilder, let me save you the effort."

A block of code is set as follows:

<?xml version="1.0"?>
              <surname> Dearle</surname>
   <title>Groovy for DSL</title>

Any command-line input or output is written as follows:

Hello, World!
Goodbye, World!

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "Click on the button to Create New App, then complete the form to create you app."


Warnings or important notes appear in a box like this.


Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail , and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files from your account at for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit and register to have the files e-mailed directly to you.


Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to and enter the name of the book in the search field. The required information will appear under the Errata section.


Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.


If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.