Groovy 2 Cookbook

Book description

Java and Groovy go together like ham and eggs, and this book is a great opportunity to learn how to exploit Groovy 2 to the full. Packed with recipes, both intermediate and advanced, it’s a great way to speed up and modernize your programming.

  • Simple and more advanced recipes to cover a broad number of topics and challenges
  • With scores of practical examples, this book will help you to build efficient, flexible, and well-integrated systems
  • Solve everyday programming problems with the elegance and simplicity of Groovy 2

In Detail

Get up to speed with Groovy, a language for the Java Virtual Machine (JVM) that integrates features of both object-oriented and functional programming. This book will show you the powerful features of Groovy 2 applied to real-world scenarios and how the dynamic nature of the language makes it very simple to tackle problems that would otherwise require hours or days of research and implementation.

Groovy 2 Cookbook contains a vast number of recipes covering many facets of today's programming landscape. From language-specific topics such as closures and metaprogramming, to more advanced applications of Groovy flexibility such as DSL and testing techniques, this book gives you quick solutions to everyday problems.

The recipes in this book start from the basics of installing Groovy and running your first scripts and continue with progressively more advanced examples that will help you to take advantage of the language's amazing features.

Packed with hundreds of tried-and-true Groovy recipes, Groovy 2 Cookbook includes code segments covering many specialized APIs to work with files and collections, manipulate XML, work with REST services and JSON, create asynchronous tasks, and more. But Groovy does more than just ease traditional Java development: it brings modern programming features to the Java platform like closures, duck-typing, and metaprogramming.

In this new book, you'll find code examples that you can use in your projects right away along with a discussion about how and why the solution works. Focusing on what's useful and tricky, Groovy 2 Cookbook offers a wealth of useful code for all Java and Groovy programmers, not just advanced practitioners.

Table of contents

  1. Groovy 2 Cookbook
    1. Table of Contents
    2. Groovy 2 Cookbook
    3. Credits
    4. About the Authors
    5. About the Reviewers
    6. www.PacktPub.com
      1. Support files, eBooks, discount offers and more
      2. Why Subscribe?
      3. Free Access for Packt account holders
    7. Preface
      1. The Groovy language
      2. What this book covers
      3. What you need for this book
      4. Who this book is for
      5. Conventions
      6. Reader feedback
      7. Customer support
        1. Downloading the example code
        2. Errata
        3. Piracy
        4. Questions
    8. 1. Getting Started with Groovy
      1. Introduction
      2. Installing Groovy on Windows
        1. Getting ready
        2. How to do it…
        3. There's more...
      3. Installing Groovy on Linux and OS X
        1. How to do it...
        2. How it works...
        3. There's more...
        4. See also
      4. Executing Groovy code from the command line
        1. How to do it...
        2. How it works...
        3. There's more...
        4. See also
      5. Using Groovy as a command-line text file editor
        1. How to do it...
        2. How it works...
        3. There's more...
        4. See also
      6. Using Groovy to start a server on the command line
        1. How to do it...
        2. See also
      7. Running Groovy with invokedynamic support
        1. Getting ready
        2. How to do it...
        3. There's more...
        4. See also
      8. Building Groovy from source
        1. Getting ready
        2. How to do it...
        3. How it works...
      9. Managing multiple Groovy installations on Linux
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      10. Using groovysh to try out Groovy commands
        1. How to do it...
        2. How it works...
        3. There's more...
      11. Starting groovyConsole to execute Groovy snippets
        1. How to do it...
        2. There's more...
      12. Configuring Groovy in Eclipse
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      13. Configuring Groovy in IntelliJ IDEA
        1. Getting ready
        2. How to do it...
        3. There's more...
    9. 2. Using Groovy Ecosystem
      1. Introduction
      2. Using Java classes from Groovy
        1. How to do it...
        2. How it works...
        3. There's more...
        4. See also
      3. Embedding Groovy into Java
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      4. Compiling Groovy code
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      5. Simplifying dependency management with Grape
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      6. Integrating Groovy into the build process using Ant
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      7. Integrating Groovy into the build process using Maven
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      8. Integrating Groovy into the build process using Gradle
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      9. Generating documentation for Groovy code
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      10. Checking Groovy code's quality with CodeNarc
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
    10. 3. Using Groovy Language Features
      1. Introduction
      2. Searching strings with regular expressions
        1. Getting ready
        2. How to do it...
        3. There's more...
        4. See also
      3. Writing less verbose Java Beans with Groovy Beans
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
      4. Inheriting constructors in Groovy classes
        1. How to do it...
        2. How it works...
      5. Adding the cloning functionality to Groovy Beans
        1. How to do it...
        2. How it works...
        3. There's more...
      6. Defining code as data in Groovy
        1. Getting ready
        2. How to do it...
        3. There's more...
      7. Defining data structures as code in Groovy
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      8. Implementing multiple inheritance in Groovy
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      9. Adding a functionality to the existing Java/Groovy classes
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      10. Defining type-checking rules for dynamic code
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
      11. Adding automatic logging to Groovy classes
        1. How to do it...
        2. How it works...
        3. There's more...
        4. See also
    11. 4. Working with Files in Groovy
      1. Introduction
      2. Reading from a file
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      3. Reading a text file line by line
        1. Getting ready
        2. How to do it...
        3. There's more...
        4. See also
      4. Processing every word in a text file
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      5. Writing to a file
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      6. Replacing tabs with spaces in a text file
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      7. Filtering a text file's content
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      8. Deleting a file or directory
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      9. Walking through a directory recursively
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      10. Searching for files
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      11. Changing file attributes on Windows
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
      12. Reading data from a ZIP file
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      13. Reading an Excel file
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      14. Extracting data from a PDF
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
    12. 5. Working with XML in Groovy
      1. Introduction
      2. Reading XML using XmlSlurper
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      3. Reading XML using XmlParser
        1. How to do it...
        2. How it works...
        3. See also
      4. Reading XML content with namespaces
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      5. Searching in XML with GPath
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      6. Searching in XML with XPath
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      7. Constructing XML content
        1. How to do it...
        2. How it works...
        3. There's more...
        4. See also
      8. Modifying XML content
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      9. Sorting XML nodes
        1. How to do it...
        2. How it works...
        3. There's more...
        4. See also
      10. Serializing Groovy Beans to XML
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
    13. 6. Working with JSON in Groovy
      1. Introduction
      2. Parsing JSON messages with JsonSlurper
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      3. Constructing JSON messages with JsonBuilder
        1. How to do it...
        2. How it works...
        3. See also
      4. Modifying JSON messages
        1. How to do it...
        2. How it works...
        3. See also
      5. Validating JSON messages
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      6. Converting JSON message to XML
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      7. Converting JSON message to Groovy Bean
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      8. Using JSON to configure your scripts
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
    14. 7. Working with Databases in Groovy
      1. Introduction
      2. Creating a database table
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      3. Connecting to an SQL database
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      4. Querying an SQL database
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      5. Modifying data in an SQL database
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      6. Calling a stored procedure
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      7. Reading BLOB/CLOB from a database
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      8. Building a simple ORM framework
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      9. Using Groovy to access Redis
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      10. Using Groovy to access MongoDB
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      11. Using Groovy to access Apache Cassandra
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
    15. 8. Working with Web Services in Groovy
      1. Introduction
      2. Downloading content from the Internet
        1. How to do it...
        2. How it works...
        3. There's more...
        4. See also
      3. Executing an HTTP GET request
        1. How to do it...
        2. How it works...
        3. There's more...
        4. See also
      4. Executing an HTTP POST request
        1. How to do it...
        2. How it works...
        3. There's more...
        4. See also
      5. Constructing and modifying complex URLs
        1. How to do it...
        2. How it works...
        3. See also
      6. Issuing a REST request and parsing a response
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      7. Issuing a SOAP request and parsing a response
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      8. Consuming RSS and Atom feeds
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      9. Using basic authentication for web service security
        1. How to do it...
        2. How it works...
        3. See also
      10. Using OAuth for web service security
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
    16. 9. Metaprogramming and DSLs in Groovy
      1. Introduction
      2. Querying methods and properties
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      3. Dynamically extending classes with new methods
        1. How to do it...
        2. How it works...
        3. There's more...
        4. See also
      4. Overriding methods dynamically
        1. Getting ready
        2. How to do it...
        3. How it works...
      5. Adding performance logging to methods
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      6. Adding a caching functionality around methods
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      7. Adding transparent imports to a script
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      8. DSL for executing commands over SSH
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      9. DSL for generating reports from logfiles
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
    17. 10. Concurrent Programming in Groovy
      1. Introduction
      2. Processing collections concurrently
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      3. Downloading files concurrently
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      4. Splitting a large task into smaller parallel jobs
        1. How to do it...
        2. How it works...
        3. See also
      5. Running tasks in parallel and asynchronously
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. See also
      6. Using actors to build message-based concurrency
        1. How to do it...
        2. How it works...
        3. See also
      7. Using STM to atomically update fields
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
      8. Using dataflow variables for lazy evaluation
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
        5. See also
    18. Index

Product information

  • Title: Groovy 2 Cookbook
  • Author(s): Andrey Adamovich, Luciano Fiandesio
  • Release date: October 2013
  • Publisher(s): Packt Publishing
  • ISBN: 9781849519366