Apache Groovy is a powerful, optionally typed and dynamic language, with static-typing and static compilation capabilities, for the Java platform aimed at improving developer productivity thanks to a concise, familiar and easy to learn syntax.
npx @tessl/cli install tessl/maven-org-apache-groovy--groovy@5.0.0Apache Groovy is a powerful, optionally typed and dynamic language, with static-typing and static compilation capabilities, for the Java platform aimed at improving developer productivity thanks to a concise, familiar and easy to learn syntax. It integrates smoothly with any Java program, and immediately delivers to your application powerful features, including scripting capabilities, Domain-Specific Language authoring, runtime and compile-time meta-programming and functional programming.
pom.xml: <groupId>org.apache.groovy</groupId><artifactId>groovy</artifactId><version>5.0.0</version>implementation 'org.apache.groovy:groovy:5.0.0'import groovy.lang.*
import groovy.util.*For Java interop:
import groovy.lang.GroovyShell;
import groovy.lang.Binding;
import groovy.lang.Script;// Simple script execution
def shell = new GroovyShell()
def result = shell.evaluate("2 + 2")
println result // 4
// Using closures
def multiply = { a, b -> a * b }
println multiply(3, 4) // 12
// Collections and ranges
def numbers = [1, 2, 3, 4, 5]
def evens = numbers.findAll { it % 2 == 0 }
println evens // [2, 4]
def range = 1..10
println range.collect { it * 2 } // [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]Apache Groovy is built around several key components:
Essential classes for the Groovy runtime including dynamic objects, closures, scripts, and the meta-programming system.
// Core interfaces and classes
interface GroovyObject {
Object invokeMethod(String name, Object args)
Object getProperty(String propertyName)
void setProperty(String propertyName, Object newValue)
MetaClass getMetaClass()
void setMetaClass(MetaClass metaClass)
}
abstract class Script extends GroovyObjectSupport {
abstract Object run()
Binding getBinding()
void setBinding(Binding binding)
}
abstract class Closure<V> extends GroovyObjectSupport {
V call(Object... args)
Closure<V> curry(Object... args)
Closure<V> memoize()
}High-level APIs for evaluating Groovy code, managing bindings, and creating reusable scripts.
class GroovyShell {
GroovyShell()
GroovyShell(Binding binding)
Object evaluate(String scriptText)
Object evaluate(File file)
Script parse(String scriptText)
Class parseClass(String text)
}
class Binding {
Object getVariable(String name)
void setVariable(String name, Object value)
Map getVariables()
}Comprehensive meta-programming capabilities for runtime method and property manipulation.
interface MetaClass {
Object invokeMethod(Object object, String methodName, Object[] arguments)
Object getProperty(Object object, String property)
void setProperty(Object object, String property, Object newValue)
List<MetaMethod> getMethods()
List<MetaProperty> getProperties()
}
class ExpandoMetaClass extends MetaClassImpl {
// Allows dynamic addition of methods and properties at runtime
}Enhanced collection operations, builder patterns, configuration management, and utility classes.
class Expando extends GroovyObjectSupport {
// Dynamic object that allows adding properties and methods at runtime
}
abstract class BuilderSupport {
protected abstract Object createNode(Object name)
protected abstract Object createNode(Object name, Object value)
protected abstract Object createNode(Object name, Map attributes)
}
class ConfigObject implements Map<String, Object> {
// Configuration object supporting hierarchical properties
}Complete JSON parsing and generation with builder patterns and streaming support.
class JsonSlurper {
Object parseText(String text)
Object parse(Reader reader)
Object parse(File file)
}
class JsonBuilder {
Object call(Closure closure)
String toString()
String toPrettyString()
}
class JsonOutput {
static String toJson(Object object)
static String prettyPrint(String jsonPayload)
}Comprehensive XML parsing, building, and manipulation with GPath expressions.
class XmlSlurper {
Object parseText(String text)
Object parse(File file)
Object parse(Reader reader)
}
class MarkupBuilder extends BuilderSupport {
// Builder for creating XML/HTML markup
}
class XmlParser {
Node parseText(String text)
Node parse(File file)
Node parse(Reader reader)
}Database operations with enhanced result sets and fluent query building.
class Sql {
static Sql newInstance(String url, String user, String password, String driver)
void eachRow(String sql, Closure closure)
List<GroovyRowResult> rows(String sql)
int executeUpdate(String sql)
boolean execute(String sql)
void call(String sql, Closure closure)
}
class DataSet {
void each(Closure closure)
DataSet findAll(Closure closure)
void add(Map<String, Object> values)
}Template engines for code generation and text processing.
class SimpleTemplateEngine {
Template createTemplate(String text)
Template createTemplate(Reader reader)
}
class StreamingTemplateEngine {
Template createTemplate(String text)
Template createTemplate(Reader reader)
}CLI building and option parsing utilities.
class CliBuilder {
OptionAccessor parse(String[] args)
void usage()
}
class OptionAccessor {
// Accessor for parsed command-line options
}// Core range types
interface Range<T extends Comparable> {
boolean contains(Object o)
T getFrom()
T getTo()
boolean isReverse()
}
class IntRange implements Range<Integer> {
// Integer range implementation (e.g., 1..10)
}
// String interpolation
abstract class GString extends GroovyObjectSupport {
String toString()
Object[] getValues()
String[] getStrings()
}
// Tuple types
class Tuple extends Object {
// Base class for immutable tuples
}
// Utility types
class MapEntry<K, V> implements Map.Entry<K, V> {
K getKey()
V getValue()
V setValue(V value)
}