A quick guide to Groovy


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 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

About deepak028

There is nothing much to describe me.However, I am a very ordinary person who believes in sharing knowledge.
This entry was posted in Scala. Bookmark the permalink.

One Response to A quick guide to Groovy

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s