A quick guide to Groovy

learn Groovy
Table of contents
Reading Time: 4 minutes

So, you want to learn Groovy in a quick time? I am so glad you are here. This blog is going to be all about Groovy and stuffs related to it. Before we begin let’s understand what Groovy is and How it is different from Java. Groovy is a JVM based dynamic language that runs on the Java Compiler. It is different than Java in a way that It is more concise in terms of syntax. We will learn Groovy and more about all this in the article.

Alright let’s dive into Groovy and take a close look at it.

Now, We will be discussing the following points in this article.

1. Introduction to Groovy
2. Groovy Installation on Ubuntu
3. Groovy Shell
4. Execute shell commands using groovy
5. Executing groovy script or classes from command line
6. Groovy with IntelliJ
7. First Groovy project with IntelliJ
8. First Groovy script

Let’s discuss the above points one by one.

Introduction to Groovy

Groovy is an object oriented language which is based on Java platform. Groovy is a JVM based dynamic language that runs on the Java Compiler. Groovy contains Java like syntax and design pattern most of the Java code is also valid in groovy but not all, rather groovy is much more dynamic and a new version of Java in a better and productive manner.

Even though Groovy generate bytecodes similar to Java and runs on JVM, It can use existing Java library, the programming style and capabilities are very different than Java programming language. Once you start writing code in Groovy, you will notice that how easy it is to do something in Groovy than Java.

Groovy Installation on Ubuntu

It’s really easy to install Groovy on Ubuntu. Let’s see How we can actually install Groovy on our machine. The current stable version of Groovy is 2.4.13.
To install Groovy on Ubuntu
Step1.

sudo apt-add-repository ppa:groovy-dev/groovy

Step2.

sudo apt-get update

Step3.

sudo apt-get install groovy

The above steps will install stable Groovy version. After this check the Groovy version by typing following command

groovy --version

Groovy Shell

The Groovy Shell, aka. groovysh is a command-line application which allows easy access to evaluate Groovy expressions, define classes and run simple experiments.

groovysh — the Groovy command -like shell

To enter groovy shell you need to type groovysh in command line and hit enter.
You can write any groovy expressions inside the groovy shell and run.

Simple Expressions

println("Hello")

Variables
Shell variables are all untyped.
This will set a shell variable:

foo = "bar"

But, this will evaluate a local variable and will not be saved to the shell’s environment:

def foo = "bar"

This behavior can be changed by activating interpreter mode.

How to activate the interpreter mode? Use the following command to activate it.

:= interpreterMode

Functions

Functions can be defined in the shell, and will be saved for later use.

Defining a function is easy:

groovy:000> def hello(name) {
groovy:001> println("Hello $name")
groovy:002> }

And then using it is as one might expect:

hello("Jason")


Executing shell commands using Groovy

Executing shell commands using Groovy is very easy. For example If you want to execute any unix/linux command using groovy that can be done using execute() method and to see the output of the executed command we can append text after it.

"pwd".execute().text

Output

/home/username/someDirectory


Executing groovy script or classes from command line

You can execute any groovy script or a groovy class from command line using the following command.

For Groovy script

groovy groovyScript.groovy

For Groovy class

groovy GroovyClass.groovy

Groovy with Intellij

You can create a Groovy project in Intellij, all you have to do is, create a project with language Groovy and also you have to select a library groovy from the drop down or setup just like java.

Note – Groovy should be installed already to create a project with Groovy language.

First Groovy project with Intellij

Create a new project in Intellij following the above steps and then add a new Groovy class and start coding. Following is the Groovy class.

package com.knoldus

class GroovyApp {

    static void main(String[] args) {
        showMyName("Deepak")
    }

    static def showMyName(name) {
        println("Welcome $name")
    }
}

First Groovy script

You can also add Groovy script in your project and then call a Groovy class within the script. Take a look at the code below.

Step1. Create a Groovy class with name “GroovyApp”

package com.knoldus

class GroovyApp {

    def showMyName(name) {
        println("Welcome $name")
    }
}

Step2. Create a Groovy script with name “groovyScript” and add the following code

package com.knoldus

app = new GroovyApp()
app.showMyName("deepak")


Here, we are calling a method of the class GroovyApp from a groovy script. So that is how we call a class’s method from a groovy script.

Groovy is no replacement of Java but It has it’s own syntax and coding styles. However, Groovy is widely used to automate build system, provide dynamic configuration and testing. That was all folks. Do let me know in the comments, If you enjoyed this post, I’d be very grateful if you’d help it spread.Keep smiling, Keep coding!

knoldus-advt-sticker

Written by 

Deepak is a Software Consultant having experince of more than 5 years . He is very enthusiastic towards his work and is a good team player. He has sound knowledge of different technologies which include Java, C++, C, HTML, CSS, Javascript, C# always keen to learn new technologies.

1 thought on “A quick guide to Groovy4 min read

Comments are closed.