How to Use Groovy?

groovy- What is it?

Groovy What Is It??

Groovy is a language to start with but it is a super version of Java which can use all Java’s enterprise capabilities with some other extra features. So Groovy is dynamic feature-rich, java friendly language.

It can be used for code coverage. It is having various libraries for mocking[3rd party and built in].

Then It supports
  • Hudson- Jenkins now
  • Teamcity
  • Anthilpro to call a groovy code
  • Code narc for code Matrics
  • ANT, Maven, GMaven, GANT, Cradle like Build tool

Groovy=Java-Boilerplate code
+ Optional dynamic typing
+Domain Specific Language
+Object Oriented
+Optionally typed
+Support Operator Overloading
+literal declaration for List
+Support for maps, ranges, regular expressions
+Efficient Object Navigation
+Supports of Groovy Bean
+Grep and Switch
+Template builders
+Supports CI[Continuous Integration]
+Supports Build tools

Who can use Groovy?

Groovy can be used by people of an organization like

  • Developers
  • Test engineers
  • Test creators other than testers
  • Business Analysts
  • Subject Matter Experts
  • Maintainance team

Groovy can be used by an organization for

  • Maturity level
  • Degree of automation
  • Adoptability
  • Tools in use
  • Reporting
When Java imposed lot more restrictions around itself, Groovy came to fill up the limitations of java. The moto of Groovy are-
  • Write once use anywhere
  • Automatic memory management
  • dynamic nature that gives users the options to modify code, augment the code, change the pattern of the code during run time/compile time
  • Works on JVM
Similar languages to Groovy are
  • Jython
  • JPearl
  • Scala
  • Clojure
  • JRuby
  • Small Talk
Now some advantages of groovy:
  • Easy to learn and Develop
  • Good candidate if the application is built on Java
  • Mostly use inside JVM that bypasses the GUI[Mainly used to bypass GUI and create testdata]
  • Support polyglot Language
  • Easy, plug and play
Few disadvantages of groovy:
  • Need JVM
  • Less useful if the UAT is developed in .NET, Python, PHP
  • Ruby is a close competitor

How to code in Groovy?

System.out.println("Hello World");
// Semicolon is optional
println "Hello World"
def name="techtrvelhub"
print "$name"
def cars=['maruti','tata','ford']
cars.each{car->aeerst car<'dutson'}

The improvement introduced on Groovy 2.0 is the support for the invoke dynamic instruction.[New instructions are available in java-7.] . Groovy already had a windows installation.In this post we will look how to install groovy in Linux machines. This post also talks about How to use indy command,groovy installation path linux,install groovy sdk linux.

Dynamic languages generate bytecode by invoking a lot of methods to perform a single operation. In this way, reflection and dynamic proxies are very costly operations.They are used extensively too. Even JIT(Just In Time) compiler can not help to improve the runtime performance of the JVM. It is due to the fact that runtime performance of JVM depends on information and pattern.

All the new invoke dynamic partially mitigate these issues hence it produces better JIT optimization. Invoke dynamics is also known as indy.runs on Groovy and Java-7.

Now can a developer use it?

No,indy is purely compile time and runtime feature.So no developer can use it from source code. This is same like garbage collection coding in Java.Developer has very little or no control of indy(that is runtime performance).

How to invoke the groovy with invoke dynamic? or how to leverage invokedynamic instruction?

groovy installation path linux

  • Check the java version,Indy works post Java-1.7. So in command prompt type java-version. It will produce Java version “1.7.0_XX”.
  • Go to groovy home directory and make a separate folder called “old”.
  • Move all the jars that starts with groovy[in the lib directory] to old folder.
  • Go to root directory of groovy and find indy folder.
  • Copy all the jars from it to groovy home directory.
  • Rename the jar files in such a way that -indy classifier should not be a part of the name.
  • Now compile your Groovy code with –indy flag.

How to use indy command?how to install groovy sdk linux

groovy--indy scriptname.groovy

Note: if — indy flag is omitted then indy will not be invoked and it will be regular Groovy execution even if we have change the libs.

The operation is similar to the assert operation in Java.

How To Install Groovy In Windows

Where do we get Groovy?
take the latest Binary Release . Download that one as zip.


Unzip the zipped content. It looks like the below screen shoot:
How To Install Groovy In Windows
Now how to run the groovy..

1. Open the cmd.
2. Navigate till to the path where the Groovy is installed.
3. Go to the bin folder
4. write groovyConsole in cmd.
5. It will open the console [Screenshot given below]

How To Install Groovy In Windows
How To Install Groovy In Windows
How To Install Groovy In Windows

Now for this way,we always need to navigate to the installed folder and give the command. The second way , which I like is to add them to the path. Create a path variable called GROOVY_HOME.

  1. Press Windows + Break key combination
  2. Click on Advanced System Settings
  3. It will open System Properties
  4. Click on Advanced Tab.
  5. Click on Environment Variables.
  6. Go to System Variable section. Click on New… button
How To Install Groovy In Windows

7. Find the Path variable in the list of system variables and append or insert the %GROOVY_HOME%bin; string to it

How To Install Groovy In Windows

Just open command from
and give command as groovyconsole.How To Install Groovy In Windows



Share and Enjoy !

Leave a Reply

Your email address will not be published. Required fields are marked *