
- •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

Getting Started with Groovy
See also
ff http://groovy.codehaus.org/Eclipse+Plugin
Configuring Groovy in IntelliJ IDEA
Since v8, IntelliJ IDEA, one of the most popular Java IDEs, has native support for Groovy. There is no need to install a plugin to start coding in Groovy and benefit from a number of interesting features such as code completion and refactoring. The latest iteration of IntelliJ IDEA, Version 12, has full support for Groovy 2.0.
In this recipe, we are going to show how to set up a Groovy project in IDEA and showcase some of the most interesting qualities of the integration.
Getting ready
To get started with this recipe, you need v12 of IntelliJ IDEA. The IDE comes in two versions, Community Edition and Ultimate. JetBrains, the company behind IDEA, offers the Community Edition for free, while it charges for the Ultimate version. The good news is that Groovy support is available in the free version of the IDE so you can start using it straight away. Download IntelliJ IDEA from http://www.jetbrains.com.
You also need to install Java and a Groovy distribution. Refer to the installation recipes from earlier in this chapter.
How to do it...
Let's start with the project creation:
1.Click on the Create New Project link in the main IntelliJ IDEA start page, as shown in the following screenshot:
42
www.it-ebooks.info

Chapter 1
2.In the next wizard page, select Groovy Module and enter the name and location of your project, as shown in the following screenshot:
43
www.it-ebooks.info

Getting Started with Groovy
3.The Project SDK field should be set to the version of the JDK you want to use for this project. If the Project SDK is empty and there is nothing in the list, then click on the New... button, select JDK and locate the JDK installation directory in the open folder selection dialog.
4.After JDK is selected click on the Next button to get to the Groovy SDK selection page, as shown in the following screenshot:
5.If it is a fresh installation, most likely the Use library drop-down list will be empty. Click on the Create… button and locate the Groovy installation directory. That's it, click on the Finish button.
6.Once the project is created, the IDE allows to create Groovy classes as well as Groovy scripts. Scripts can be used as scrapbooks for testing code snippets and can be later integrated into a class:
44
www.it-ebooks.info

Chapter 1
7.You can also create Java classes that call methods on Groovy classes. Let's create a simple Groovy class, named MyGroovyClass, as shown in the following screenshot:
8.The class defines three attributes and uses the concise Groovy approach, with no need for getters and setters to access the variables.
9.Now let's create a Java class, MyJavaClass, as shown in the following screenshot:
As you can see in the previous screenshot, the code autocompletion works perfectly and it's even able to propose synthetic methods that are generated by the Groovy compiler, such as getters and setters.
For a broader look at the integration between Java and Groovy, take a look at the
Using Java classes from Groovy recipe in Chapter 2, Using Groovy Ecosystem.
45
www.it-ebooks.info

Getting Started with Groovy
There's more...
Several refactoring goodies are also available, including Extract Parameter that also works for closures (see the Defining code as data in Groovy recipe in Chapter 3, Using Groovy Language Features), as shown in the following screenshot:
The result of the refactoring will be as shown in the following screenshot:
46
www.it-ebooks.info