Book Image

Groovy 2 Cookbook

Book Image

Groovy 2 Cookbook

Overview of this book

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 (17 chapters)
Groovy 2 Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Managing multiple Groovy installations on Linux


If a developer needs to work with different Groovy distributions on the same machine, chances are that he or she would be involved in a lot of environment variable fiddling, such as PATH, JAVA_HOME, and GROOVY_HOME.

Luckily, there is a tool that helps to manage those variables as well as to download the required setup files on demand.

The name of this goody is GVM (Groovy enVironment Manager). GVM was inspired by similar tools from the Ruby ecosystem, RVM, and rbenv.

In this recipe, we will demonstrate how to use the GVM tool and show the benefits it delivers.

Getting ready

Use the package manager available on your Linux distribution to install curl, unzip, and java on your machine. For example, on Ubuntu it can be achieved with the following command sequence:

sudo apt-get update
sudo apt-get install curl
sudo apt-get install zip
sudo apt-get install openjdk-6-jdk

The GVM installation script will not work without those packages. You can skip the OpenJDK package in case you have Java 5 or later distribution already installed.

Then you need to fetch the installation script from GVM's website (http://get.gvmtool.net) and pass it to bash using the following command:

curl -s get.gvmtool.net | bash

It will start the set up process as shown in the following screenshot:

To finalize the installation, open a new terminal and run the following command:

source ~/.gvm/bin/gvm-init.sh

How to do it...

As soon as GVM is installed and running, you can start putting it to use:

  1. To install the latest Groovy distribution, you can issue the following command:

    > gvm install groovy
    Downloading: groovy 2.1.6
    ...
    Installing: groovy 2.1.6
    Done installing!
    
  2. At the end of the installation, it will ask you whether to make it the default or not. Since we all like "the latest and greatest", type Y (yes):

    Do you want groovy 2.1.6 to be set as default? (Y/n): Y
    
  3. To install different Groovy distribution (for example, v1.8.6, which is still rather popular) you can fire the following command:

    > gvm install groovy 1.8.6
    Downloading: groovy 1.8.6
    ...
    Installing: groovy 1.8.6
    Done installing!
    
  4. Again it will ask about setting 1.8.6 as default Groovy distribution. Answer n (no) in this case since we would like to keep v2 as the primary one:

    Do you want groovy 1.8.6 to be set as default? (Y/n): n
    
  5. To set (or to ensure) Groovy version used by default, use the following command:

    > gvm default groovy 2.1.6
    Default groovy version set to 2.1.6
    
  6. You can also verify that Groovy is running and is the requested version by typing:

    > groovy --version
    Groovy Version: 2.1.6 JVM: ...
    
  7. To switch temporarily to a different Groovy distribution, just type:

    > gvm use groovy 1.8.6
    Using groovy version 1.8.6 in this shell.
    
  8. Another way to check which version of Groovy is currently active is:

    > gvm current groovy
    Using groovy version 1.8.6
    
  9. For example, this script will not run under 1.8.6:

    > groovy -e "println new File('.').directorySize()"
    Caught: groovy.lang.MissingMethodException:
       No signature of method:
          java.io.File.directorySize() is applicable ...
    
  10. If we switch to the latest Groovy, the script will succeed as shown:

    > gvm use groovy
    Using groovy version 2.1.6 in this shell.
    > groovy -e "println new File('.').directorySize()"
    126818311
    
  11. To remove the unnecessary distribution, we can just run:

    > gvm uninstall groovy 1.8.6
    Uninstalling groovy 1.8.6...
    

How it works...

The reason the directorySize() method (steps 9 and 10) didn't work for v1.8.6 of Groovy is simply because this method was only introduced in v2.

As we already mentioned, GVM manages the values of environment variables to direct your Groovy commands to the proper distribution. It also downloads, unpacks, and caches Groovy installation archives under the ~/.gvm/var directory.

There's more...

GVM also can manage other popular Groovy-based products; for example, Gradle (to build a framework that we are going to discuss in the Integrating Groovy into the build process using Gradle recipe in Chapter 2, Using Groovy Ecosystem), Grails (a web application framework), Griffon (a desktop application framework), and so on in a similar way.

This recipe can also be applied to a Mac running OS X. You can enjoy GVM on Windows too, but you need to install and run Cygwin (a Linux environment simulation for Windows).

See also

Additional useful information can be found on the following product's home pages: