Just decided I wanted to get into programming with Groovy again so I took a look to find out how hard would it be to install this on FreeBSD 9.1. Did a little research online and as it turns out, not too hard :)

First you'll need Java installed - there are a few different Java Development Kits (JDK) available but I decided to go ahead with OpenJDK.

1. Install it - while we're on it we also install wget:

time pkg_add -r openjdk7 wget

This should take just under 6 minutes.

JAVA is now installed under: /usr/local/openjdk7

2. To check if it works, use the following to get the Java version:

java -version

You should get something like this:

openjdk version "1.7.0_09"
OpenJDK Runtime Environment (build 1.7.0_09-b05)
OpenJDK Client VM (build 23.5-b02, mixed mode)]

3. If you're using the default C shell you need something like this to set the JAVA_HOME:

setenv JAVA_HOME /usr/local/openjdk7

4. If you want it permanently set, you can do this:

echo 'setenv JAVA_HOME /usr/local/openjdk7' >> ~/.cshrc

Now let's go ahead and install Groovy--

5. Download the latest Groovy (Binary Release) from here: http://groovy.codehaus.org/Download (about 27MB)

Switch directories to /tmp first:

cd /tmp; wget http://dist.groovy.codehaus.org/distributions/groovy-binary-2.1.3.zip

(took under 3 min)

6. Unzip somewhere reasonable: (the target directory will be created automatically)

unzip groovy-binary-2.1.3.zip -d /usr/local

7. Set the GROOVY_HOME variable:

setenv GROOVY_HOME /usr/local/groovy-2.1.3

8. Make it permanent:

echo 'setenv GROOVY_HOME /usr/local/groovy-2.1.3' >> ~/.cshrc

9. Also fix your path (C shell only)

set path=($GROOVY_HOME/bin $path)

10. Make it permanent:

echo 'set path=($GROOVY_HOME/bin/ $path)' >> ~/.cshrc

11. Test Groovy:

groovy -v

The result should look something like this:

Groovy Version: 2.1.3 JVM: 1.7.0_09 Vendor: Oracle Corporation OS: FreeBSD

Awesome.

12.

Groovy 'hello world' Shell Script:
#!/usr/bin/env groovy

println "hello world"

13. Add it with this one liner

printf '#!/usr/bin/env groovy'\n' println "hello world"' > helloworld.groovy

14. Make it executable:

chmod +x helloworld.groovy

15. Run it like this:

groovy helloworld.groovy

As you can imagine the output will look something like this:

root@weirdbricks:/root # ./helloworld.groovy
hello world

16. Simple Groovy loop:

for (i in 0..10) { println i }

17. Some other examples:

println "Get OS Name: ${System.properties['os.name']}"

Output:

Get OS Name: FreeBSD

18. Get JAVA Version:

println "Get JAVA Version: ${System.properties['java.version']}"

Output:

Get JAVA Version: 1.7.0_09

References: