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.

18 That 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.

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]

19 The 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.

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:

1.  Why 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.

2.  Groovy 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.

3.  Code-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.

4.  Using 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:

5.  Build 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.

6.  Testing 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.)

Part 3, “Groovy in the real world,” examines various topics that Java developers encounter on a regular basis:

7.  The 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.

8.  Database 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.

9.  RESTful 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]

21 This really is good stuff you won’t find anywhere else.

10.  Building 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]

22 Think of those as the tl;dr (“too long; didn’t read” in internet parlance) sections.

Three appendixes cover additional topics:

  1. Installing Groovy— This appendix explains how to install Groovy using the downloads, the Windows installer, and the latest cool tool: GVM, the Groovy Environment Manager.
  2. Groovy by feature— Here I provide a topic-by-topic review of Groovy, meant to complement chapter 2, “Groovy by example.”
  3. SOAP-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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.123.155