- •Credits
- •About the Authors
- •About the Reviewers
- •www.PacktPub.com
- •Table of Contents
- •Preface
- •Introduction
- •Installing Groovy on Windows
- •Installing Groovy on Linux and OS X
- •Executing Groovy code from the command line
- •Using Groovy as a command-line text file editor
- •Running Groovy with invokedynamic support
- •Building Groovy from source
- •Managing multiple Groovy installations on Linux
- •Using groovysh to try out Groovy commands
- •Starting groovyConsole to execute Groovy snippets
- •Configuring Groovy in Eclipse
- •Configuring Groovy in IntelliJ IDEA
- •Introduction
- •Using Java classes from Groovy
- •Embedding Groovy into Java
- •Compiling Groovy code
- •Generating documentation for Groovy code
- •Introduction
- •Searching strings with regular expressions
- •Writing less verbose Java Beans with Groovy Beans
- •Inheriting constructors in Groovy classes
- •Defining code as data in Groovy
- •Defining data structures as code in Groovy
- •Implementing multiple inheritance in Groovy
- •Defining type-checking rules for dynamic code
- •Adding automatic logging to Groovy classes
- •Introduction
- •Reading from a file
- •Reading a text file line by line
- •Processing every word in a text file
- •Writing to a file
- •Replacing tabs with spaces in a text file
- •Deleting a file or directory
- •Walking through a directory recursively
- •Searching for files
- •Changing file attributes on Windows
- •Reading data from a ZIP file
- •Reading an Excel file
- •Extracting data from a PDF
- •Introduction
- •Reading XML using XmlSlurper
- •Reading XML using XmlParser
- •Reading XML content with namespaces
- •Searching in XML with GPath
- •Searching in XML with XPath
- •Constructing XML content
- •Modifying XML content
- •Sorting XML nodes
- •Serializing Groovy Beans to XML
- •Introduction
- •Parsing JSON messages with JsonSlurper
- •Constructing JSON messages with JsonBuilder
- •Modifying JSON messages
- •Validating JSON messages
- •Converting JSON message to XML
- •Converting JSON message to Groovy Bean
- •Using JSON to configure your scripts
- •Introduction
- •Creating a database table
- •Connecting to an SQL database
- •Modifying data in an SQL database
- •Calling a stored procedure
- •Reading BLOB/CLOB from a database
- •Building a simple ORM framework
- •Using Groovy to access Redis
- •Using Groovy to access MongoDB
- •Using Groovy to access Apache Cassandra
- •Introduction
- •Downloading content from the Internet
- •Executing an HTTP GET request
- •Executing an HTTP POST request
- •Constructing and modifying complex URLs
- •Issuing a REST request and parsing a response
- •Issuing a SOAP request and parsing a response
- •Consuming RSS and Atom feeds
- •Using basic authentication for web service security
- •Using OAuth for web service security
- •Introduction
- •Querying methods and properties
- •Dynamically extending classes with new methods
- •Overriding methods dynamically
- •Adding performance logging to methods
- •Adding transparent imports to a script
- •DSL for executing commands over SSH
- •DSL for generating reports from logfiles
- •Introduction
- •Processing collections concurrently
- •Downloading files concurrently
- •Splitting a large task into smaller parallel jobs
- •Running tasks in parallel and asynchronously
- •Using actors to build message-based concurrency
- •Using STM to atomically update fields
- •Using dataflow variables for lazy evaluation
- •Index
www.PacktPub.com
Support files, eBooks, discount offers and more
You might want to visit www.PacktPub.com for support files and downloads related to your book.
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at service@packtpub.com for more details.
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
TM
http://PacktLib.PacktPub.com
Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can access, read and search across Packt's entire library of books.
Why Subscribe?
ff
ff
ff
Fully searchable across every book published by Packt Copy and paste, print and bookmark content
On demand and accessible via web browser
Free Access for Packt account holders
If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view nine entirely free books. Simply use your login credentials for immediate access.
www.it-ebooks.info
www.it-ebooks.info
Table of Contents
Preface |
1 |
Chapter 1: Getting Started with Groovy |
7 |
Introduction |
7 |
Installing Groovy on Windows |
8 |
Installing Groovy on Linux and OS X |
10 |
Executing Groovy code from the command line |
11 |
Using Groovy as a command-line text file editor |
14 |
Using Groovy to start a server on the command line |
16 |
Running Groovy with invokedynamic support |
17 |
Building Groovy from source |
18 |
Managing multiple Groovy installations on Linux |
20 |
Using groovysh to try out Groovy commands |
23 |
Starting groovyConsole to execute Groovy snippets |
29 |
Configuring Groovy in Eclipse |
35 |
Configuring Groovy in IntelliJ IDEA |
42 |
Chapter 2: Using Groovy Ecosystem |
47 |
Introduction |
47 |
Using Java classes from Groovy |
48 |
Embedding Groovy into Java |
51 |
Compiling Groovy code |
57 |
Simplifying dependency management with Grape |
60 |
Integrating Groovy into the build process using Ant |
63 |
Integrating Groovy into the build process using Maven |
71 |
Integrating Groovy into the build process using Gradle |
75 |
Generating documentation for Groovy code |
78 |
Checking Groovy code's quality with CodeNarc |
82 |
www.it-ebooks.info
Table of Contents
Chapter 3: Using Groovy Language Features |
89 |
Introduction |
89 |
Searching strings with regular expressions |
90 |
Writing less verbose Java Beans with Groovy Beans |
92 |
Inheriting constructors in Groovy classes |
99 |
Adding the cloning functionality to Groovy Beans |
100 |
Defining code as data in Groovy |
104 |
Defining data structures as code in Groovy |
108 |
Implementing multiple inheritance in Groovy |
117 |
Adding a functionality to the existing Java/Groovy classes |
123 |
Defining type-checking rules for dynamic code |
126 |
Adding automatic logging to Groovy classes |
132 |
Chapter 4: Working with Files in Groovy |
135 |
Introduction |
136 |
Reading from a file |
136 |
Reading a text file line by line |
138 |
Processing every word in a text file |
140 |
Writing to a file |
142 |
Replacing tabs with spaces in a text file |
144 |
Filtering a text file's content |
146 |
Deleting a file or directory |
149 |
Walking through a directory recursively |
151 |
Searching for files |
153 |
Changing file attributes on Windows |
157 |
Reading data from a ZIP file |
159 |
Reading an Excel file |
160 |
Extracting data from a PDF |
162 |
Chapter 5: Working with XML in Groovy |
167 |
Introduction |
167 |
Reading XML using XmlSlurper |
168 |
Reading XML using XmlParser |
170 |
Reading XML content with namespaces |
172 |
Searching in XML with GPath |
175 |
Searching in XML with XPath |
178 |
Constructing XML content |
184 |
Modifying XML content |
187 |
Sorting XML nodes |
189 |
Serializing Groovy Beans to XML |
192 |
ii
www.it-ebooks.info
|
Table of Contents |
|
|
Chapter 6: Working with JSON in Groovy |
197 |
Introduction |
197 |
Parsing JSON messages with JsonSlurper |
198 |
Constructing JSON messages with JsonBuilder |
201 |
Modifying JSON messages |
204 |
Validating JSON messages |
205 |
Converting JSON message to XML |
211 |
Converting JSON message to Groovy Bean |
214 |
Using JSON to configure your scripts |
218 |
Chapter 7: Working with Databases in Groovy |
223 |
Introduction |
223 |
Creating a database table |
224 |
Connecting to an SQL database |
228 |
Querying an SQL database |
231 |
Modifying data in an SQL database |
235 |
Calling a stored procedure |
239 |
Reading BLOB/CLOB from a database |
242 |
Building a simple ORM framework |
244 |
Using Groovy to access Redis |
246 |
Using Groovy to access MongoDB |
252 |
Using Groovy to access Apache Cassandra |
256 |
Chapter 8: Working with Web Services in Groovy |
263 |
Introduction |
263 |
Downloading content from the Internet |
264 |
Executing an HTTP GET request |
265 |
Executing an HTTP POST request |
268 |
Constructing and modifying complex URLs |
271 |
Issuing a REST request and parsing a response |
272 |
Issuing a SOAP request and parsing a response |
276 |
Consuming RSS and Atom feeds |
279 |
Using basic authentication for web service security |
283 |
Using OAuth for web service security |
284 |
Chapter 9: Metaprogramming and DSLs in Groovy |
289 |
Introduction |
289 |
Querying methods and properties |
290 |
Dynamically extending classes with new methods |
293 |
Overriding methods dynamically |
296 |
Adding performance logging to methods |
299 |
iii
www.it-ebooks.info
Table of Contents |
|
|
|
|
|
Adding a caching functionality around methods |
305 |
|
Adding transparent imports to a script |
317 |
|
DSL for executing commands over SSH |
319 |
|
DSL for generating reports from logfiles |
327 |
|
Chapter 10: Concurrent Programming in Groovy |
335 |
|
Introduction |
335 |
|
Processing collections concurrently |
336 |
|
Downloading files concurrently |
340 |
|
Splitting a large task into smaller parallel jobs |
344 |
|
Running tasks in parallel and asynchronously |
349 |
|
Using actors to build message-based concurrency |
355 |
|
Using STM to atomically update fields |
361 |
|
Using dataflow variables for lazy evaluation |
365 |
|
Index |
371 |
iv
www.it-ebooks.info