Making Java Groovy

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

Making Java Groovy is a practical handbook for developers who want to blend Groovy into their day-to-day work with Java. It starts by introducing the key differences between Java and Groovy—and how you can use them to your advantage. Then, it guides you step-by-step through realistic development challenges, from web applications to web services to desktop applications, and shows how Groovy makes them easier to put into production. About this book You don't need the full force of Java when you're writing a build script, a simple system utility, or a lightweight web app—but that's where Groovy shines brightest. This elegant JVM-based dynamic language extends and simplifies Java so you can concentrate on the task at hand instead of managing minute details and unnecessary complexity. Making Java Groovy is a practical guide for developers who want to benefit from Groovy in their work with Java. It starts by introducing the key differences between Java and Groovy and how to use them to your advantage. Then, you'll focus on the situations you face every day, like consuming and creating RESTful web services, working with databases, and using the Spring framework. You'll also explore the great Groovy tools for build processes, testing, and deployment and learn how to write Groovy-based domain-specific languages that simplify Java development. Written for developers familiar with Java. No Groovy experience required.

Author(s): Ken Kousen
Publisher: Manning Publications
Year: 2013

Language: English
Pages: 368
Tags: Библиотека;Компьютерная литература;Java;

Front cover......Page 1
brief contents......Page 8
contents......Page 10
foreword......Page 16
preface......Page 18
acknowledgments......Page 20
Who are you?......Page 23
Roadmap......Page 24
About the author......Page 26
about the cover illustration......Page 27
Part 1—Up to speed with Groovy......Page 28
1 Why add Groovy to Java?......Page 30
1.1 Issues with Java......Page 31
1.1.1 Is static typing a bug or a feature?......Page 32
1.1.2 Methods must be in a class, even if you don’t need or want one......Page 34
1.1.3 Java is overly verbose......Page 37
1.1.4 Groovy makes testing Java much easier......Page 38
1.1.5 Groovy tools simplify your build......Page 40
1.2 Groovy features that help Java......Page 41
1.3 Java use cases and how Groovy helps......Page 42
1.3.3 Building and accessing web services......Page 43
1.4 Summary......Page 44
2 Groovy by example......Page 45
2.2 Accessing Google Chart Tools......Page 46
2.2.1 Assembling the URL with query string......Page 47
2.2.2 Transmitting the URL......Page 50
2.2.3 Creating a UI with SwingBuilder......Page 51
2.3 Groovy Baseball......Page 53
2.3.1 Database data and Plain Old Groovy Objects......Page 56
2.3.2 Parsing XML......Page 62
2.3.3 HTML builders and groovlets......Page 69
2.4 Summary......Page 72
3.1 Integrating Java with other languages......Page 73
3.2 Executing Groovy scripts from Java......Page 75
3.2.1 Using JSR223 scripting for the Java Platform API......Page 77
3.2.2 Working with the Groovy Eval class......Page 83
3.2.3 Working with the GroovyShell class......Page 84
3.2.4 Calling Groovy from Java the easy way......Page 86
3.2.5 Calling Java from Groovy......Page 89
3.3 Summary......Page 90
4 Using Groovy features in Java......Page 91
4.1 Treating POJOs like POGOs......Page 92
4.2 Implementing operator overloading in Java......Page 94
4.3 Making Java library classes better: the Groovy JDK......Page 98
4.4.1 Delegating to contained objects......Page 101
4.4.2 Creating immutable objects......Page 103
4.4.3 Creating singletons......Page 108
4.5 Working with XML......Page 109
4.6 Working with JSON data......Page 116
4.7 Summary......Page 117
Part 2—Groovy tools......Page 118
5 Build processes......Page 120
5.1 The build challenge......Page 121
5.2 The Java approach, part 1: Ant......Page 122
5.3.1 The Ant task......Page 124
5.3.2 The Ant task......Page 125
5.3.3 Writing your build in Groovy with AntBuilder......Page 127
5.3.4 Custom build scripts with Gant......Page 129
5.3.5 Ant summary......Page 131
5.4 The Java approach, part 2: Maven......Page 132
5.4.1 The Groovy-Eclipse plugin for Maven......Page 133
5.4.2 The GMaven project......Page 137
5.4.3 Maven summary......Page 140
5.5 Grapes and @Grab......Page 141
5.6 The Gradle build system......Page 144
5.6.1 Basic Gradle builds......Page 145
5.6.2 Interesting configurations......Page 149
5.7 Summary......Page 151
6 Testing Groovy and Java projects......Page 153
6.1 Working with JUnit......Page 155
6.1.1 A Java test for the Groovy implementation......Page 158
6.1.2 A Groovy test for the Java implementation......Page 160
6.1.3 A GroovyTestCase test for a Java implementation......Page 161
6.2 Testing scripts written in Groovy......Page 164
6.2.1 Useful subclasses of GroovyTestCase: GroovyShellTestCase......Page 166
6.2.2 Useful subclasses of GroovyTestCase: GroovyLogTestCase......Page 168
6.3 Testing classes in isolation......Page 169
6.3.1 Coerced closures......Page 171
6.3.2 The Expando class......Page 173
6.3.3 StubFor and MockFor......Page 178
6.4.1 The Search for Spock......Page 183
6.4.2 Test well, and prosper......Page 184
6.4.3 Data-driven specifications......Page 186
6.4.4 The trouble with tribbles......Page 188
6.4.5 Other Spock capabilities......Page 190
6.5 Summary......Page 191
Part 3—Groovy in the real world......Page 192
7 The Spring framework......Page 194
7.1 A Spring application......Page 195
7.2 Refreshable beans......Page 202
7.3 Spring AOP with Groovy beans......Page 206
7.4 Inline scripted beans......Page 212
7.5 Groovy with JavaConfig......Page 213
7.6 Building beans with the Grails BeanBuilder......Page 217
7.7 Summary......Page 224
8 Database access......Page 226
8.1 The Java approach, part 1: JDBC......Page 227
8.2 The Groovy approach, part 1: groovy.sql.Sql......Page 230
8.3 The Java approach, part 2: Hibernate and JPA......Page 235
8.4.2 Grails Object-Relational Mapping (GORM)......Page 240
8.5.1 Populating Groovy vampires......Page 247
8.5.2 Querying and mapping MongoDB data......Page 250
8.6 Summary......Page 253
9 RESTful web services......Page 254
9.1 The REST architecture......Page 256
9.2 The Java approach: JAX-RS......Page 257
9.2.1 JAX-RS resource and tests......Page 259
9.3 Implementing JAX-RS with Groovy......Page 266
9.4 RESTful Clients......Page 269
9.5 Hypermedia......Page 270
9.5.1 A simple example: Rotten Tomatoes......Page 271
9.5.2 Adding transitional links......Page 273
9.5.3 Adding structural links......Page 276
9.5.4 Using a JsonBuilder to control the output......Page 277
9.6.1 Groovlets......Page 280
9.6.3 Grails and REST......Page 282
9.7 Summary......Page 283
10 Building and testing web applications......Page 284
10.1 Groovy servlets and ServletCategory......Page 285
10.2 Easy server-side development with groovlets......Page 290
10.2.1 A “Hello, World!” groovlet......Page 291
10.2.2 Implicit variables in groovlets......Page 293
10.3.1 Unit-testing servlets with Spring......Page 295
10.3.2 Integration testing with Gradle......Page 297
10.3.3 Automating Jetty in the Gradle build......Page 299
10.3.4 Using an integration-test source tree......Page 301
10.4 Grails: the Groovy “killer app”......Page 304
10.4.1 The quest for the holy Grails......Page 306
10.5 Summary......Page 315
A.1 Installing a JDK......Page 316
A.2 Installing Groovy......Page 317
A.3 Testing your installation......Page 318
A.4 IDE support......Page 319
A.5 Installing other projects in the Groovy ecosystem......Page 320
appendix B—Groovy by feature......Page 322
B.1 Scripts and the traditional example......Page 323
B.2.1 Numbers......Page 325
B.2.2 Strings and Groovy strings......Page 327
B.3 Plain Old Groovy Objects......Page 329
B.4 Collections......Page 330
B.4.1 Ranges......Page 331
B.4.2 Lists......Page 332
B.4.3 Maps......Page 334
B.5 Closures......Page 336
B.6.1 Loops......Page 338
B.6.2 Conditionals......Page 340
B.6.4 Safe de-reference......Page 341
B.7 File I/O......Page 342
B.8 XML......Page 344
B.8.1 Parsing and slurping XML......Page 345
B.8.2 Generating XML......Page 347
B.8.3 Validation......Page 348
B.9.1 Slurping JSON......Page 350
B.9.2 Building JSON......Page 351
A......Page 354
B......Page 355
C......Page 356
E......Page 357
G......Page 358
H......Page 360
J......Page 361
L......Page 362
O......Page 363
Q......Page 364
S......Page 365
V......Page 367
Y......Page 368
Back cover......Page 369