The Ultimate Guide: Getting Started with Groovy and Groovy Scripts

Powerful, dynamic language, with static-typing and static compilation capabilities, for the Java platform aimed at improving developer productivity .Groovy syntax is simple and easy. It saves a lot of code and effort thus increasing the productivity of developer if he had to do the same thing in Java.

In this tutorial you will learn what is groovy and how to install Groovy on windows and Linux machine . Later you will see two examples which helps you kickstart for Writing Groovy Scripts.

Table of Content

  1. What is Groovy?
  2. Prerequisites
  3. How to Install Groovy on Windows Machine
  4. How to Install Groovy on Ubuntu Machine
  5. Groovy Syntax
  6. Groovy Examples
  7. Conclusion

What is Groovy?

Groovy is a Powerful static as well as dynamic language which is almost same as Java language with few differences. Groovy language is vastly used in Jenkins Pipelines. It integrates with Java libraries very well to deliver powerful enhancements and features including domain specific language authoring and scripting capabilities.

Basic Features of Groovy

  • Groovy supports all Java libraries and it has its own libraries as well.
  • It has a simple similar syntax as that of Java but in more simpler form
  • It has both static and dynamic nature
  • It has great extensibility for the language and tooling.
  • Last but not the least it a free open source language which is being used by lot of developers.

Prerequisites

  • Ubuntu 18 Machine or Windows machine
  • Make sure to have Java 8 plus installed on machines. To check Java version run the following command.
java --version
On Ubuntu Machine
On Windows Machine

How to Install Groovy on ubuntu Machine

Installing Groovy on Ubuntu machine is pretty straightforward. Lets Install the Groovy on Ubuntu 18 machine.

  • First Update the ubuntu official repository by running the apt command.
sudo apt update
  • Now, download the groovy installation script by running the curl command.
curl -s get.sdkman.io | bash
  • Now Install the groovy using the sdk command

How to install Groovy on Windows machine

  • Now you will see windows installer package, once you click on it it will automatically download the file.
  • Now click on the the downloaded windows installer package and installation will begin.
  • Accept the license Agreement
  • Make sure you select Typical for Setup Type and click on Install
  • Now Groovy is successfully installed on windows machine. Open Groovy console from the Start menu & run a simple command to test.

Groovy Syntax

Shebang line

  • This allows script to run groovy scripts directly from command line provided you have groovy installed and groovy command is available on the PATH
#!/usr/bin/env groovy
println "Hello from the shebang line"

Strings

  • Strings are basically chain of characters. Groovy strings are written with single quotes ' or double quotes '' and even with triple quotes '''
'This is an example of single line'

"This is an example of double line"

def threequotes = '''
line1
line2
line3
'''

String interpolation

Groovy expressions can be interpolated and it is just like replacing a placeholder with its value. Placeholder in groovy are surrounded by ${} or $ . Also if you pass GString in any method where string is required then you should replace it by calling toString() on that method.

def name  = "automate"
def greet =  "Hello $name"

Groovy Examples

Lets now see two examples of Groovy

  1. JsonSlurper : JsonSlurper is a class that parses JSON text or reader content into Groovy data
    • creating instance of the JsonSlurper class
    • Using the parseText function of the JsonSlurper class to parse some JSON text
    • access the values in the JSON string via the key.
import groovy.json.JsonSlurper 

class Example {
   static void main(String[] args) {
      def jsonSlurper = new JsonSlurper() # creating instance of the JsonSlurper class
      def object = jsonSlurper.parseText('{ "name":  "John", "ID" : "1"}') 
 	
      println(object.name);
      println(object.ID);
   } 
}
  1. Catching Exceptions
    • Accessing an array with an index value which is greater than the size of the array
class Example {
   static void main(String[] args) {
      try {
         def arr = new int[3];
         arr[5] = 5;
      }catch(ArrayIndexOutOfBoundsException ex) {
         println("Catching the Array out of Bounds exception");
      }catch(Exception ex) {
         println("Catching the exception");
      }
		
      println("Let's move on after the exception");
   } 
}

Conclusion

This tutorial is pretty straightforward and to get you started with Groovy. In this tutorial you learnt what is groovy and how to install Groovy on windows and Linux machine . Later you learnt two examples which helps you kickstart for Writing Groovy Scripts.

Well , Groovy is used at various places such as Jenkins pipelines etc.What do you plan to code with Groovy next ?