Skip to content

Introduction to Groovy

homepage-banner

Introduction

Groovy is a dynamic object-oriented programming language that runs on the Java Virtual Machine. It was designed to be an easy-to-learn language that supports scripting and rapid application development. Groovy is often used for scripting in build systems like Gradle and for automation of tasks in various Java applications.

Installing Groovy on Windows

Installing Groovy on Windows is a straightforward process. Here are the steps:

  1. Download the latest version of Groovy from the official website.
  2. Extract the contents of the downloaded file to a directory of your choice.
  3. Set the GROOVY_HOME environment variable to the directory where you extracted Groovy.
  4. Add the %GROOVY_HOME%\bin directory to your PATH environment variable.

Installing Groovy on macOS

Installing Groovy on macOS is also an easy process. Here are the steps:

  1. Install Homebrew if you haven’t already. Homebrew is a package manager for macOS that simplifies the installation of software.
  2. Open Terminal and run the command brew install groovy.
  3. Wait for the installation to complete.

Installing Groovy on Linux

Installing Groovy on Linux varies depending on the distribution you are using. In general, you can use the package manager of your distribution to install Groovy. Here are the steps for Ubuntu:

  1. Open Terminal and run the command sudo apt-get update.
  2. Run the command sudo apt-get install groovy.
  3. Wait for the installation to complete.

Groovy Script Examples

Example 1: Hello World

One of the simplest Groovy scripts you can write is the famous “Hello World” program. Here’s the code:

println "Hello, World!"

This script will print “Hello, World!” to the console when executed.

Example 2: File Manipulation

Another common use case for Groovy scripts is file manipulation. Here’s an example that reads a file and prints its contents to the console:

def file = new File('/path/to/file.txt')
file.eachLine { line ->
    println line
}

This script creates a new File object and reads each line of the file using the eachLine method. It then prints each line to the console.

Example 3: Web Scraping

Groovy makes web scraping easy with its built-in support for XML and HTML parsing. Here’s an example that scrapes the titles of the top 10 posts on the front page of Reddit:

@Grab('org.jsoup:jsoup:1.14.1')
import org.jsoup.Jsoup

def doc = Jsoup.connect('<https://www.reddit.com/>').get()
doc.select('.scrollerItem').each { post ->
    def title = post.select('.Post__title').text()
    println title
}

This script uses the jsoup library to connect to the Reddit homepage and parse the HTML. It then selects each post on the page and extracts its title using CSS selectors.

Conclusion

Groovy is a powerful language for scripting and automation. Its simplicity and integration with the Java ecosystem make it a popular choice for developers. These examples only scratch the surface of what’s possible with Groovy, so if you’re interested, I encourage you to explore the language further. Happy scripting!

Reference

  • Modern Programming Made Easy: Using Java, Scala, Groovy, and JavaScript (Adam L. Davis)
  • https://groovy-lang.org/
  • https://www.cheat-sheets.org/saved-copy/rc015-groovy_online.pdf
  • https://onecompiler.com/cheatsheets/groovy
Leave a message