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.
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
As soon as GVM is installed and running, you can start putting it to use:
> gvm install groovy Downloading: groovy 2.1.6 ... Installing: groovy 2.1.6 Done installing!
Y
(yes):Do you want groovy 2.1.6 to be set as default? (Y/n): Y
> gvm install groovy 1.8.6 Downloading: groovy 1.8.6 ... Installing: groovy 1.8.6 Done installing!
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
> gvm default groovy 2.1.6 Default groovy version set to 2.1.6
> groovy --version Groovy Version: 2.1.6 JVM: ...
> gvm use groovy 1.8.6 Using groovy version 1.8.6 in this shell.
> gvm current groovy Using groovy version 1.8.6
> groovy -e "println new File('.').directorySize()" Caught: groovy.lang.MissingMethodException: No signature of method: java.io.File.directorySize() is applicable ...
> gvm use groovy Using groovy version 2.1.6 in this shell. > groovy -e "println new File('.').directorySize()" 126818311
> gvm uninstall groovy 1.8.6 Uninstalling groovy 1.8.6...
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.
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).
Additional useful information can be found on the following product's home pages: