
- •brief contents
- •contents
- •foreword
- •preface
- •acknowledgments
- •about this book
- •Roadmap
- •Code conventions and downloads
- •Author Online
- •About the author
- •about the cover illustration
- •1 Why add Groovy to Java?
- •1.1 Issues with Java
- •1.1.1 Is static typing a bug or a feature?
- •1.1.2 Methods must be in a class, even if you don’t need or want one
- •1.1.3 Java is overly verbose
- •1.1.4 Groovy makes testing Java much easier
- •1.1.5 Groovy tools simplify your build
- •1.2 Groovy features that help Java
- •1.3 Java use cases and how Groovy helps
- •1.3.1 Spring framework support for Groovy
- •1.3.2 Simplified database access
- •1.3.3 Building and accessing web services
- •1.3.4 Web application enhancements
- •1.4 Summary
- •2 Groovy by example
- •2.1 Hello, Groovy
- •2.2 Accessing Google Chart Tools
- •2.2.1 Assembling the URL with query string
- •2.2.2 Transmitting the URL
- •2.2.3 Creating a UI with SwingBuilder
- •2.3 Groovy Baseball
- •2.3.1 Database data and Plain Old Groovy Objects
- •2.3.2 Parsing XML
- •2.3.3 HTML builders and groovlets
- •2.4 Summary
- •3 Code-level integration
- •3.1 Integrating Java with other languages
- •3.2 Executing Groovy scripts from Java
- •3.2.1 Using JSR223 scripting for the Java Platform API
- •3.2.2 Working with the Groovy Eval class
- •3.2.3 Working with the GroovyShell class
- •3.2.4 Calling Groovy from Java the easy way
- •3.2.5 Calling Java from Groovy
- •3.3 Summary
- •4 Using Groovy features in Java
- •4.1 Treating POJOs like POGOs
- •4.2 Implementing operator overloading in Java
- •4.3 Making Java library classes better: the Groovy JDK
- •4.4 Cool AST transformations
- •4.4.1 Delegating to contained objects
- •4.4.2 Creating immutable objects
- •4.4.3 Creating singletons
- •4.5 Working with XML
- •4.6 Working with JSON data
- •4.7 Summary
- •5 Build processes
- •5.1 The build challenge
- •5.2 The Java approach, part 1: Ant
- •5.3 Making Ant Groovy
- •5.3.1 The <groovy> Ant task
- •5.3.2 The <groovyc> Ant task
- •5.3.3 Writing your build in Groovy with AntBuilder
- •5.3.4 Custom build scripts with Gant
- •5.3.5 Ant summary
- •5.4 The Java approach, part 2: Maven
- •5.4.2 The GMaven project
- •5.4.3 Maven summary
- •5.5 Grapes and @Grab
- •5.6 The Gradle build system
- •5.6.1 Basic Gradle builds
- •5.6.2 Interesting configurations
- •5.7 Summary
- •6 Testing Groovy and Java projects
- •6.1 Working with JUnit
- •6.1.1 A Java test for the Groovy implementation
- •6.1.2 A Groovy test for the Java implementation
- •6.1.3 A GroovyTestCase test for a Java implementation
- •6.2 Testing scripts written in Groovy
- •6.2.1 Useful subclasses of GroovyTestCase: GroovyShellTestCase
- •6.2.2 Useful subclasses of GroovyTestCase: GroovyLogTestCase
- •6.3 Testing classes in isolation
- •6.3.1 Coerced closures
- •6.3.2 The Expando class
- •6.3.3 StubFor and MockFor
- •6.4 The future of testing: Spock
- •6.4.1 The Search for Spock
- •6.4.2 Test well, and prosper
- •6.4.4 The trouble with tribbles
- •6.4.5 Other Spock capabilities
- •6.5 Summary
- •7 The Spring framework
- •7.1 A Spring application
- •7.2 Refreshable beans
- •7.3 Spring AOP with Groovy beans
- •7.4 Inline scripted beans
- •7.5 Groovy with JavaConfig
- •7.6 Building beans with the Grails BeanBuilder
- •7.7 Summary
- •8 Database access
- •8.1 The Java approach, part 1: JDBC
- •8.2 The Groovy approach, part 1: groovy.sql.Sql
- •8.3 The Java approach, part 2: Hibernate and JPA
- •8.4 The Groovy approach, part 2: Groovy and GORM
- •8.4.1 Groovy simplifications
- •8.5 Groovy and NoSQL databases
- •8.5.1 Populating Groovy vampires
- •8.5.2 Querying and mapping MongoDB data
- •8.6 Summary
- •9 RESTful web services
- •9.1 The REST architecture
- •9.3 Implementing JAX-RS with Groovy
- •9.4 RESTful Clients
- •9.5 Hypermedia
- •9.5.1 A simple example: Rotten Tomatoes
- •9.5.2 Adding transitional links
- •9.5.3 Adding structural links
- •9.5.4 Using a JsonBuilder to control the output
- •9.6 Other Groovy approaches
- •9.6.1 Groovlets
- •9.6.2 Ratpack
- •9.6.3 Grails and REST
- •9.7 Summary
- •10 Building and testing web applications
- •10.1 Groovy servlets and ServletCategory
- •10.2 Easy server-side development with groovlets
- •10.2.1 A “Hello, World!” groovlet
- •10.2.2 Implicit variables in groovlets
- •10.3.2 Integration testing with Gradle
- •10.3.3 Automating Jetty in the Gradle build
- •10.4 Grails: the Groovy “killer app”
- •10.4.1 The quest for the holy Grails
- •10.5 Summary
- •A.1 Installing a JDK
- •A.2 Installing Groovy
- •A.3 Testing your installation
- •A.4 IDE support
- •A.5 Installing other projects in the Groovy ecosystem
- •B.1 Scripts and the traditional example
- •B.2 Variables, numbers, and strings
- •B.2.1 Numbers
- •B.2.2 Strings and Groovy strings
- •B.3 Plain Old Groovy Objects
- •B.4 Collections
- •B.4.1 Ranges
- •B.4.2 Lists
- •B.4.3 Maps
- •B.5 Closures
- •B.6 Loops and conditionals
- •B.6.1 Loops
- •B.6.2 Conditionals
- •B.6.3 Elvis
- •B.6.4 Safe de-reference
- •B.7 File I/O
- •B.8.1 Parsing and slurping XML
- •B.8.2 Generating XML
- •B.8.3 Validation
- •B.9 JSON support
- •B.9.1 Slurping JSON
- •B.9.2 Building JSON
- •index
- •Symbols

about this book
Who are you?
I assume you are a developer and are at least comfortable with Java.18 You don’t have to be an expert, but any discussions of the basics of object-oriented programming are beyond the scope of this book.
I do not, however, assume that you have experience with Groovy. The Groovy concepts are covered where they are used, and because I wanted the chapters to be as independent as possible, that means some redundancy is involved. The question of how to teach Groovy bothered me for some time, because I knew that some people prefer the traditional, feature-by-feature tutorial, whereas others much prefer small but nontrivial examples. In the end, I solved the problem by doing both. Chapter 2 is entitled “Groovy by example,” and appendix B is called “Groovy by feature.” Hopefully one or the other or both will provide you with what you need.19
18That unfortunate burst of blatant honesty just cut my potential audience by far too many. If you’re buying the book just to have it look cool on your bookshelf, or to build a book fort, or to prop open your office door, or to hold down stacks of new cover sheets for your T.P.S. reports, or for any other reason that doesn’t involve actually reading it, please feel free to do so. By the way, you can get your own T.P.S. report cover sheets at http://www.chrisglass.com/journal/downloads/TPSreport.pdf, among other places.
19The definitive reference for Groovy is still Groovy in Action, 2nd edition, by Dierk König et al., http://manning
.com/koenig2/, my all-time favorite technical book.
xxii
www.it-ebooks.info

ABOUT THIS BOOK |
xxiii |
Roadmap
The book is divided into three parts. The first part is about the Groovy language and how to combine Groovy and Java in the same project. The second part covers testing and build processes with which Groovy can help. The third part is a survey of the typical problems Java developers encounter and how you can use Groovy to make them easier to solve.
Note that the chapters are as independent as possible. By that I mean that each chapter contains projects that combine build files, tests, persistence layers, and so on. The chapter titles represent which topic is covered in depth in that chapter, but you don’t need to read them in any particular order.
The chapters in part 1, “Up to speed with Groovy,” are as follows:
1Why add Groovy to Java?—Here I try to identify the issues that make Java awkward or verbose, as well as the inconsistencies that have accumulated over the years, and how Groovy can help you manage them. This is the “elevator pitch” chapter, with the arguments you can use on your manager to justify adding Groovy to a Java project.
2Groovy by example—This chapter contains a handful of examples that highlight features of the language that I’ll use throughout the book. As noted earlier, appendix B (“Groovy by feature”) provides an alternative way to help you learn any Groovy you need.
3Code-level integration—How can Groovy and Java be mixed at the language level? This chapter also explores how to work with Groovy scripts from Java, including how to test them.
4Using Groovy features in Java—What features does the Groovy language provide that can be used anywhere, regardless of problem? This chapter covers POGOs, operator overloading, AST transformations, and the Groovy JDK.
Part 2, “Groovy tools,” discusses testing and build processes and how Groovy can make them easier:
5Build processes—Managing automated builds is a pain point in many organizations. In this chapter, I look at both Ant and Maven from the Java world and how Groovy works with each, and then I discuss one of the breakout projects from the Groovy ecosystem: Gradle.
6Testing Groovy and Java projects—Groovy is a dynamic language, making testing even more important than usual. This chapter discusses testing tools like JUnit and how Groovy works with them, along with the great mocking capabilities built into the language. It finishes with a serious discussion of the Spock testing framework.20
20 The Spock discussion includes far too many Star Trek references, but they were no tribble at all. (Sorry.)
www.it-ebooks.info

xxiv |
ABOUT THIS BOOK |
Part 3, “Groovy in the real world,” examines various topics that Java developers encounter on a regular basis:
7The Spring framework—Spring is one of the most successful and pervasive open source projects in the Java world, and it works well with Groovy in a variety of ways. This chapter uses Groovy classes as regular Spring beans and aspects and then discusses refreshable beans, inline scripted beans, and the BeanBuilder from Grails.
8Database access—Every Java developer eventually works with persistent storage. This chapter talks about using the groovy.sql.Sql class to handle raw SQL and uses an example from MongoDB as a representative NoSQL database. It also contains a discussion of GORM, the Grails Object Relational Mapping API from Grails, that uses Groovy domain-specific languages to combine and configure Spring and Hibernate.
9RESTful web services—The REST approach for designing web services that can be combined in scalable, efficient ways is examined, using the JAX-RS 2.0 specification as a foundation. In addition to the typical URL-driven database, though, I show how Groovy can be used to implement hypermedia as transitional links, as structural links, or through custom providers.21
10Building and testing web applications—Groovy uses metaprogramming to make web development easier. It also includes groovlets, which make developing simple applications easy. Finally, this chapter includes a basic discussion of the Grails framework, arguably the Groovy killer app.
Each chapter in parts 2 and 3 discusses a particular aspect of Java programming. I try to follow this structure:
■Review the current Java approach to the problem.
■Present any hybrid Java/Groovy solutions.
■Introduce pure Groovy alternatives.
For example, in chapter 6 on testing, I start with JUnit, then show the GroovyTestCase subclass of JUnit’s TestCase, and later talk about the Spock testing framework. Because not all the chapter topics break down cleanly that way, the beginning of each chapter includes a figure that summarizes the technologies covered and how they relate to each other. Also, at the end of each major section is a “Lessons Learned” block to summarize the main points.22
Three appendixes cover additional topics:
AInstalling Groovy—This appendix explains how to install Groovy using the downloads, the Windows installer, and the latest cool tool: GVM, the Groovy Environment Manager.
BGroovy by feature—Here I provide a topic-by-topic review of Groovy, meant to complement chapter 2, “Groovy by example.”
21This really is good stuff you won’t find anywhere else.
22Think of those as the tl;dr (“too long; didn’t read” in internet parlance) sections.
www.it-ebooks.info
ABOUT THIS BOOK |
xxv |
CSOAP-based web services—(Available as a bonus download from www.manning.com/ MakingJavaGroovy.) Most companies have moved on from the Service Oriented Architecture (SOA) approach to integration that dominated the early 2000s, but Groovy works easily with the existing Java tools for SOAP and WSDL. In case you’re working with legacy applications, this appendix shows how to use Groovy in those situations.
Code conventions and downloads
All source code in the book is in a fixed-width font like this, which sets it off from the surrounding text. Code examples appear throughout the book. Longer listings appear under clear listing headers, while shorter listings appear between lines of text. In many listings, the code is annotated to point out the key concepts.
Source code for all the working examples is available from the publisher's website at www.manning.com/MakingJavaGroovy and from the GitHub repository at https:// github.com/kousen/Making-Java-Groovy. You will find many extra examples here, beyond those covered in the book.
Author Online
Purchase of Making Java Groovy includes free access to a private web forum run by Manning Publications where you can make comments about the book, ask technical questions, and receive help from the author and from other users. To access the forum and subscribe to it, point your web browser to www.manning.com/MakingJavaGroovy. This page provides information on how to get on the forum once you’re registered, what kind of help is available, and the rules of conduct on the forum.
Manning’s commitment to our readers is to provide a venue where a meaningful dialog between individual readers and between readers and the author can take place. It’s not a commitment to any specific amount of participation on the part of the author, whose contribution to the AO remains voluntary (and unpaid). We suggest you try asking the author some challenging questions lest his interest stray!
The Author Online forum and the archives of previous discussions will be accessible from the publisher’s website as long as the book is in print.
About the author
Ken Kousen is an independent consultant and technical trainer specializing in all areas related to Java, especially involving open source projects like Spring, Hibernate, Android, Groovy, and Grails. He has over 20 years of experience in the field and numerous technical certifications. His academic background includes BS degrees in both Mathematics and Mechanical Engineering from M.I.T., an MS and PhD in Aerospace Engineering from Princeton, and an MS in Computer Science from Rensselaer Polytechnic Institute.
www.it-ebooks.info