- •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
Kenneth A. Kousen
FOREWORD BY Guillaume Laforge
M A N N I N G |
www.it-ebooks.info |
|
Making Java Groovy
www.it-ebooks.info
www.it-ebooks.info
Making Java Groovy
KENNETH A. KOUSEN
M A N N I N G
SHELTER ISLAND
www.it-ebooks.info
For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact
Special Sales Department Manning Publications Co. 20 Baldwin Road
PO Box 261
Shelter Island, NY 11964 Email: orders@manning.com
©2014 by Manning Publications Co. All rights reserved.
No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher.
Photographs in this book were created by Martin Evans and Jordan Hochenbaum, unless otherwise noted. Illustrations were created by Martin Evans, Joshua Noble, and Jordan Hochenbaum. Fritzing (fritzing.org) was used to create some of the circuit diagrams.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps.
Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine.
Manning Publications Co. |
Development editor: |
Cynthia Kane |
20 Baldwin Road |
Copyeditor: |
Melinda Rankin |
PO Box 261 |
Proofreader: |
Melody Dolab |
Shelter Island, NY 11964 |
Typesetter: |
Dennis Dalinnik |
|
Cover designer: |
Marija Tudor |
ISBN: 9781935182948
Printed in the United States of America
1 2 3 4 5 6 7 8 9 10 – MAL – 19 18 17 16 15 14 13
www.it-ebooks.info
To my father, Morton Kousen, MD,
who taught me everything I know about dedication, persistence, and facing the future with a positive and
upbeat attitude, despite whatever pain and difficulties lay ahead. He will always be my best example of what a man should be.
www.it-ebooks.info
www.it-ebooks.info
|
|
|
|
brief contents |
PART 1 |
UP TO SPEED WITH GROOVY. ........................................ |
1 |
||
|
1 |
■ |
Why add Groovy to Java? 3 |
|
|
2 |
■ |
Groovy by example 18 |
|
|
3 |
■ |
Code-level integration |
46 |
|
4 |
■ |
Using Groovy features in Java 64 |
|
PART 2 |
GROOVY TOOLS .......................................................... |
91 |
||
|
5 |
■ |
Build processes 93 |
|
|
6 |
■ |
Testing Groovy and Java projects 126 |
|
PART 3 |
GROOVY IN THE REAL WORLD.................................... |
165 |
||
|
7 |
■ |
The Spring framework |
167 |
|
8 |
■ |
Database access 199 |
|
|
9 |
■ |
RESTful web services |
227 |
|
10 |
■ |
Building and testing web applications 257 |
vii
www.it-ebooks.info
www.it-ebooks.info
contents
foreword |
xv |
|
preface |
xvii |
|
acknowledgments |
xix |
|
about this book |
xxii |
|
about the cover illustration xxvi |
PART 1 UP TO SPEED WITH GROOVY . |
............................ |
|
1 |
||||
1 |
Why add Groovy to Java? 3 |
|
|
|
|
||
1.1 |
Issues with Java 4 |
|
|
|
|
||
|
|
Is static typing a bug or a feature? |
5 |
■ |
Methods must be in a class, |
||
|
|
even if you don’t need or want one |
7 |
■ |
Java is overly verbose 10 |
||
|
|
Groovy makes testing Java much easier |
|
11 |
■ Groovy tools simplify |
||
|
|
your build |
13 |
|
|
|
|
|
1.2 |
Groovy features that help Java |
14 |
|
|
||
|
1.3 |
Java use cases and how Groovy helps |
15 |
||||
|
|
Spring framework support for Groovy |
16 ■ |
Simplified database |
|||
|
|
access 16 |
■ Building and accessing web services 16 |
Web application enhancements 17
1.4 Summary 17
ix
www.it-ebooks.info
x CONTENTS
2 |
Groovy by example |
18 |
|
2.1 |
Hello, Groovy |
19 |
|
|
2.2 |
Accessing Google Chart Tools 19 |
|
|
|
Assembling the URL with query string 20 ■ Transmitting |
|
|
|
the URL 23 ■ |
Creating a UI with SwingBuilder 24 |
2.3Groovy Baseball 26
Database data and Plain Old Groovy Objects 29
Parsing XML 35 ■ HTML builders and groovlets 42
2.4 Summary 45
3 |
Code-level integration |
46 |
|
|
||
3.1 |
Integrating Java with other languages |
46 |
||||
|
3.2 |
Executing Groovy scripts from Java |
48 |
|||
|
|
Using JSR223 scripting for the Java Platform API 50 |
||||
|
|
Working with the Groovy Eval class 56 |
■ |
Working with |
||
|
|
the GroovyShell class |
57 ■ Calling Groovy from Java |
|||
|
|
the easy way 59 |
■ |
Calling Java from Groovy 62 |
3.3Summary 63
4 |
Using Groovy features in Java |
64 |
|||
4.1 |
Treating POJOs like POGOs |
65 |
|||
|
4.2 |
Implementing operator overloading in Java 67 |
|||
|
4.3 |
Making Java library classes better: the Groovy JDK 71 |
|||
|
4.4 |
Cool AST transformations |
74 |
||
|
|
Delegating to contained objects |
74 ■ Creating |
||
|
|
immutable objects 76 |
■ Creating singletons 81 |
||
|
4.5 |
Working with XML |
82 |
|
|
|
4.6 |
Working with JSON data |
89 |
|
4.7Summary 90
PART 2 |
GROOVY TOOLS .............................................. |
91 |
|
5 |
Build processes 93 |
|
|
5.1 |
The build challenge 94 |
|
|
|
5.2 |
The Java approach, part 1: Ant |
95 |
www.it-ebooks.info
CONTENTS xi
5.3 Making Ant Groovy |
97 |
|
|
The <groovy> Ant task |
97 ■ |
The <groovyc> Ant task |
98 |
Writing your build in Groovy with AntBuilder 100 |
|
||
Custom build scripts with Gant |
102 ■ Ant summary |
104 |
5.4 |
The Java approach, part 2: Maven |
105 |
||
|
The Groovy-Eclipse plugin for Maven |
106 |
||
|
The GMaven project |
110 ■ |
Maven summary 113 |
|
5.5 |
Grapes and @Grab |
114 |
|
|
5.6 |
The Gradle build system |
117 |
|
|
|
Basic Gradle builds |
118 ■ |
Interesting configurations 122 |
5.7Summary 124
6 |
Testing Groovy and Java projects |
126 |
|
|
|||||
6.1 |
Working with JUnit |
128 |
|
|
|
|
|
||
|
|
A Java test for the Groovy implementation |
131 |
|
|||||
|
|
A Groovy test for the Java implementation |
133 |
|
|||||
|
|
A GroovyTestCase test for a Java implementation 134 |
|
||||||
|
6.2 |
Testing scripts written in Groovy 137 |
|
|
|||||
|
|
Useful subclasses of GroovyTestCase: GroovyShellTestCase |
139 |
||||||
|
|
Useful subclasses of GroovyTestCase: GroovyLogTestCase 141 |
|||||||
|
6.3 |
Testing classes in isolation |
142 |
|
|
||||
|
|
Coerced closures 144 |
■ |
The Expando class 146 |
|
||||
|
|
StubFor and MockFor |
151 |
|
|
|
|
|
|
|
6.4 |
The future of testing: Spock |
156 |
|
|
||||
|
|
The Search for Spock |
156 |
■ |
Test well, and prosper 157 |
|
|||
|
|
Data-driven specifications |
159 |
■ The trouble with tribbles |
161 |
||||
|
|
Other Spock capabilities |
163 |
|
|
|
|
6.5Summary 164
PART 3 GROOVY IN THE REAL WORLD ........................ |
165 |
||
7 |
The Spring framework 167 |
|
|
7.1 |
A Spring application 168 |
|
|
|
7.2 |
Refreshable beans 175 |
|
|
7.3 |
Spring AOP with Groovy beans 179 |
|
|
7.4 |
Inline scripted beans 185 |
|
|
7.5 |
Groovy with JavaConfig 186 |
|
www.it-ebooks.info
xii |
|
|
|
CONTENTS |
|
|
|
|
|
|
|
|
||
|
7.6 |
Building beans with the Grails BeanBuilder |
190 |
|||||||||||
|
7.7 |
Summary |
197 |
|
|
|
|
|
|
|
|
|
|
|
8 |
Database access |
|
199 |
|
|
|
|
|
|
|
|
|
|
|
8.1 |
The Java approach, part 1: JDBC |
|
200 |
|
|
|
||||||||
|
8.2 |
The Groovy approach, part 1: groovy.sql.Sql |
203 |
|||||||||||
|
8.3 |
The Java approach, part 2: Hibernate and JPA |
208 |
|||||||||||
|
8.4 |
The Groovy approach, part 2: Groovy and GORM 213 |
||||||||||||
|
|
Groovy simplifications 213 |
■ |
Grails Object-Relational |
|
|||||||||
|
|
Mapping (GORM) |
213 |
|
|
|
|
|
|
|
|
|||
|
8.5 |
Groovy and NoSQL databases 220 |
|
|
|
|||||||||
|
|
Populating Groovy vampires |
|
220 ■ |
Querying and |
|
|
|||||||
|
|
mapping MongoDB data |
223 |
|
|
|
|
|
||||||
|
8.6 |
Summary |
226 |
|
|
|
|
|
|
|
|
|
|
|
9 |
RESTful web services |
227 |
|
|
|
|
|
|
|
|
||||
9.1 |
The REST architecture |
229 |
|
|
|
|
|
|||||||
|
9.2 |
The Java approach: JAX-RS |
230 |
|
|
|
|
|||||||
|
|
JAX-RS resource and tests |
232 |
|
|
|
|
|
||||||
|
9.3 |
Implementing JAX-RS with Groovy |
239 |
|
|
|||||||||
|
9.4 |
RESTful Clients |
242 |
|
|
|
|
|
|
|
|
|||
|
9.5 |
Hypermedia |
243 |
|
|
|
|
|
|
|
|
|
||
|
|
A simple example: Rotten Tomatoes |
244 ■ |
Adding transitional |
||||||||||
|
|
links 246 |
■ Adding structural links |
249 |
■ Using a JsonBuilder |
|||||||||
|
|
to control the output |
250 |
|
|
|
|
|
|
|
|
|||
|
9.6 |
Other Groovy approaches |
|
253 |
|
|
|
|
|
|||||
|
|
Groovlets |
253 ■ |
Ratpack |
|
255 ■ |
Grails and REST |
255 |
9.7Summary 256
10 |
Building and testing web applications |
257 |
|
10.1 |
Groovy servlets and ServletCategory |
258 |
|
|
10.2 |
Easy server-side development with groovlets 263 |
|
|
|
A “Hello, World!” groovlet 264 ■ Implicit variables |
|
|
|
in groovlets 266 |
|
www.it-ebooks.info
CONTENTS xiii
10.3 Unitand integration-testing web components 268
Unit-testing servlets with Spring 268 ■ Integration testing with Gradle 270 ■ Automating Jetty in the Gradle build 272 Using an integration-test source tree 274
10.4Grails: the Groovy “killer app” 277
|
The quest for the holy Grails 279 |
|
10.5 |
Summary 288 |
|
appendix A |
Installing Groovy |
289 |
appendix B |
Groovy by feature |
295 |
appendix C |
Soap-based web services available online at manning.com/kousen |
index 327
www.it-ebooks.info
www.it-ebooks.info