- •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
Chapter 1
Starting groovyConsole to execute Groovy snippets
The Groovy distribution has another option for developers who are more familiar with a graphical interface and want to try out code snippets. The tool in question is
groovyConsole, which is very similar to groovysh (see the Using groovysh to try out Groovy commands recipe) except that it provides a GUI with syntax highlighting and basic code editor features.
In this recipe, we are going to cover basic GroovyConsole usage scenarios.
How to do it...
Open your shell and let's dive in:
1.To start GroovyConsole, you just need to fire the groovyConsole command in your command line:
The GroovyConsole screen is divided into two logical parts. The top area is meant for script editing, and the bottom area displays the script execution output when a script is launched.
29
www.it-ebooks.info
Getting Started with Groovy
2.By pressing Ctrl + R (CMD + R on a Mac) or going to the Script menu and selecting Run, the script in the edit area will be executed:
3.Press Ctrl + W (CMD + W on a Mac) to clean the output in the lower pane.
4.The bottom panel displays an output similar to the groovysh shell; that is, first the executed script is printed after the groovy> marker followed by the printed output and a result value, if any:
5.In the previous script, we set the value of the internal test property (this property is being stored within an instance of the groovy.lang.Script class that is used to execute all the code in the console). If you change that code to reuse the previous field value (for example, +=), you will actually notice that the value changes every time the code gets executed:
30
www.it-ebooks.info
Chapter 1
6.Similar to groovysh, if you define a variable using the def keyword, then you can't reuse the previous value. The def keyword is used to define locally scoped variables and in this case, it's bound only to single execution of the script:
31
www.it-ebooks.info
Getting Started with Groovy
7.Let's go through the editor features of the GroovyConsole application. The File menu offers standard functions to start over by clicking on the New File menu item, to open a new GroovyConsole window, to open a previously saved Groovy script,
to save or print contents of the editor, and to close the application as shown in the following screenshot:
8.The Edit menu provides functionality available in any modern text editor such as copy, paste, and basic search functionality, as shown in the following screenshot:
32
www.it-ebooks.info
Chapter 1
9.The View menu has some interesting features for manipulating what is displayed in the bottom output area, as shown in the following screenshot:
It is possible to increase or decrease the font size and configure the way the output panel displays information. For instance, you can disable the script output or clear the output screen at each run.
10.The History menu allows you to revive previous script versions that you have executed during your session with GroovyConsole, as shown in the following screenshot:
33
www.it-ebooks.info
Getting Started with Groovy
11.Since GroovyConsole is all about executing a script, the Script menu presents the most valuable options, as shown in the following screenshot:
You can run or abort your script execution, force script compilation, and add additional libraries to the class path. Also, you can Clear Script Context, which will lead to clearing all the script's accumulated properties (for example, test).
12.Another useful feature is that you can select a part of your script in the editor and execute only that part by clicking on Run Selection in the Script menu, as shown in the following screenshot:
There's more...
The GroovyConsole tool comes with a handy Object Browser that graphically shows the fields and methods available for a given class.
34
www.it-ebooks.info