Introduction to Groovy

1.1 What is Groovy?

A powerful, optionally typed, dynamic language for the Java platform, with static typing and static compilation available.

James Strachan created it in 2003. It became an Apache Software Foundation top-level project in 2015.

Syntax is close to Java (most Java code is valid Groovy) but much more concise, with closures, builders, metaprogramming, and scripting.

It compiles to JVM bytecode and interoperates seamlessly with any Java library.

1.2 Key Features

Uses Java classes directly. Java and Groovy classes can extend each other.

Optional semicolons, parentheses, return, and types. Automatic properties.

Dynamic typing (+ optional static) def for dynamic. @TypeChecked / @CompileStatic for static checking and speed.

First-class code blocks with delegation The Groovy Development Kit adds hundreds of methods to JDK classes Native collection syntax [1, 2], [a: 1], 1..10 String interpolation "Hello ${name}"

Runtime MOP (metaClass) and compile-time AST transformations Command chains, builders, delegation

Run .groovy scripts directly with no class or main needed Gradle, Jenkins, Spock, Grails, Micronaut, Geb, Nextflow

1.3 Where Groovy Is Used

Gradle build scripts (Groovy DSL), Jenkins pipelines (Jenkinsfile), Spock testing, Grails web apps, Nextflow (bioinformatics workflows), SoapUI/ReadyAPI scripting, Apache NiFi, Elasticsearch scripting (historical), Liferay and Atlassian (ScriptRunner) automation, and general JVM scripting.

1.4 Groovy vs Java vs Kotlin

Dynamic by default, optional static ?. operator (runtime) Very strong (runtime + compile-time) Annotation processors, reflection Compiler plugins, limited JShell, single-file run .kts scripts Good (static compile ≈ Java)