0
# Apache Groovy
1
2
Apache Groovy is a powerful, optionally typed and dynamic language with static-typing and static compilation capabilities for the Java platform. It aims to improve developer productivity thanks to a concise, familiar and easy to learn syntax. The groovy-all package provides the complete Groovy distribution including the core language and all optional modules.
3
4
## Package Information
5
6
- **Package Name**: groovy-all
7
- **Package Type**: maven
8
- **Language**: Groovy/Java
9
- **Installation**: `implementation 'org.codehaus.groovy:groovy-all:3.0.25'`
10
11
## Core Imports
12
13
```groovy
14
import groovy.lang.*
15
import groovy.util.*
16
```
17
18
For Maven dependency:
19
```xml
20
<dependency>
21
<groupId>org.codehaus.groovy</groupId>
22
<artifactId>groovy-all</artifactId>
23
<version>3.0.25</version>
24
</dependency>
25
```
26
27
For Gradle:
28
```kotlin
29
implementation 'org.codehaus.groovy:groovy-all:3.0.25'
30
```
31
32
## Basic Usage
33
34
```groovy
35
import groovy.lang.GroovyShell
36
import groovy.lang.Binding
37
38
// Create and execute Groovy scripts
39
def binding = new Binding()
40
binding.setVariable('name', 'World')
41
def shell = new GroovyShell(binding)
42
def result = shell.evaluate('println "Hello, $name!"; return "Done"')
43
44
// Use Groovy collections and closures
45
def numbers = [1, 2, 3, 4, 5]
46
def doubled = numbers.collect { it * 2 }
47
def evens = numbers.findAll { it % 2 == 0 }
48
49
// Work with dynamic objects
50
def expando = new groovy.lang.Expando()
51
expando.name = 'Groovy'
52
expando.version = '3.0.25'
53
expando.greet = { "Hello from ${name} ${version}!" }
54
```
55
56
## Architecture
57
58
Apache Groovy is organized into several key layers:
59
60
- **Core Language**: Dynamic typing, closures, metaprogramming capabilities (`groovy.lang`)
61
- **Runtime Extensions**: Method extensions that add functionality to Java classes
62
- **Compilation System**: Both dynamic and static compilation with AST transformations
63
- **Builder Pattern Support**: Structured data and UI construction via builders
64
- **Integration Modules**: Specialized modules for JSON, XML, SQL, templating, and more
65
66
## Capabilities
67
68
### Core Language Features
69
70
The fundamental Groovy language constructs including script execution, dynamic objects, closures, and metaprogramming facilities.
71
72
```groovy { .api }
73
class GroovyShell {
74
GroovyShell()
75
GroovyShell(Binding binding)
76
GroovyShell(ClassLoader parent, Binding binding)
77
Object evaluate(String scriptText)
78
Object evaluate(File scriptFile)
79
Script parse(String scriptText)
80
}
81
82
class Binding {
83
Binding()
84
Binding(Map variables)
85
Binding(String[] args)
86
Object getVariable(String name)
87
void setVariable(String name, Object value)
88
boolean hasVariable(String name)
89
void removeVariable(String name)
90
Map getVariables()
91
Object getProperty(String property)
92
void setProperty(String property, Object newValue)
93
}
94
95
abstract class Closure<V> {
96
Object call(Object... args)
97
Closure<V> curry(Object... args)
98
Object getDelegate()
99
void setDelegate(Object delegate)
100
}
101
```
102
103
[Core Language](./core-language.md)
104
105
### JSON Processing
106
107
JSON parsing, generation, and manipulation using JsonSlurper and JsonBuilder with configurable parsing strategies.
108
109
```groovy { .api }
110
class JsonSlurper {
111
JsonSlurper()
112
Object parseText(String text)
113
Object parse(File file)
114
Object parse(URL url)
115
JsonSlurper setType(JsonParserType type)
116
int getMaxSizeForInMemory()
117
JsonSlurper setMaxSizeForInMemory(int size)
118
}
119
120
class JsonBuilder {
121
JsonBuilder()
122
JsonBuilder(Object content)
123
Object call(Closure c)
124
String toString()
125
String toPrettyString()
126
}
127
```
128
129
[JSON Processing](./json.md)
130
131
### XML Processing
132
133
XML parsing with GPath navigation, DOM manipulation, and markup generation using various XML processing approaches.
134
135
```groovy { .api }
136
class XmlSlurper {
137
XmlSlurper()
138
XmlSlurper(boolean validating, boolean namespaceAware)
139
GPathResult parseText(String text)
140
GPathResult parse(File file)
141
GPathResult parse(URL url)
142
}
143
144
class MarkupBuilder {
145
MarkupBuilder(Writer writer)
146
Object invokeMethod(String methodName, Object args)
147
void yield(Object value, boolean escapeMarkup)
148
}
149
```
150
151
[XML Processing](./xml.md)
152
153
### Database Access
154
155
SQL database operations, connection management, and result set processing with Groovy's SQL class and DataSet.
156
157
```groovy { .api }
158
class Sql {
159
static Sql newInstance(String url, String user, String password, String driverClassName)
160
static Sql newInstance(DataSource dataSource)
161
List<GroovyRowResult> rows(String sql, List<Object> params)
162
void execute(String sql, List<Object> params)
163
int executeUpdate(String sql, List<Object> params)
164
void close()
165
}
166
167
class DataSet {
168
DataSet(Sql sql, String table)
169
void add(Map<String, Object> values)
170
List<GroovyRowResult> findAll(Closure where)
171
int update(Closure where, Map<String, Object> values)
172
}
173
```
174
175
[Database Access](./sql.md)
176
177
### Template Processing
178
179
Template engines for generating dynamic content from templates with variable substitution and embedded code execution.
180
181
```groovy { .api }
182
class SimpleTemplateEngine {
183
SimpleTemplateEngine()
184
Template createTemplate(String templateText)
185
Template createTemplate(File templateFile)
186
}
187
188
interface Template {
189
Writable make(Map<String, Object> binding)
190
}
191
192
class GStringTemplateEngine {
193
GStringTemplateEngine()
194
Template createTemplate(String templateText)
195
}
196
```
197
198
[Template Processing](./templates.md)
199
200
### AST Transformations
201
202
Compile-time code generation and modification through annotations that transform the Abstract Syntax Tree.
203
204
```groovy { .api }
205
@interface ToString {
206
boolean includeNames() default false
207
boolean includeFields() default false
208
String[] excludes() default {}
209
String[] includes() default {}
210
}
211
212
@interface CompileStatic {}
213
214
@interface TypeChecked {}
215
216
@interface Immutable {
217
String[] excludes() default {}
218
boolean copyWith() default false
219
}
220
```
221
222
[AST Transformations](./ast-transforms.md)
223
224
### Command Line Interface
225
226
Command-line argument parsing and processing using PicoCLI integration for building CLI applications.
227
228
```groovy { .api }
229
class CliBuilder {
230
CliBuilder()
231
CliBuilder(String usage)
232
void setUsage(String usage)
233
OptionAccessor parse(String[] args)
234
def opt(String opt, String longOpt, String description)
235
}
236
```
237
238
[Command Line Interface](./cli.md)
239
240
### Swing UI Building
241
242
Swing user interface construction using builder pattern for creating desktop applications with Groovy.
243
244
```groovy { .api }
245
class SwingBuilder {
246
SwingBuilder()
247
Object invokeMethod(String name, Object args)
248
JFrame frame(Map<String, Object> attributes, Closure content)
249
JPanel panel(Map<String, Object> attributes, Closure content)
250
}
251
```
252
253
[Swing UI Building](./swing.md)
254
255
## Types
256
257
### Core Types
258
259
```groovy { .api }
260
interface GroovyObject {
261
Object invokeMethod(String name, Object args)
262
Object getProperty(String property)
263
void setProperty(String property, Object newValue)
264
MetaClass getMetaClass()
265
void setMetaClass(MetaClass metaClass)
266
}
267
268
abstract class Script {
269
abstract Object run()
270
Object getProperty(String property)
271
void setProperty(String property, Object newValue)
272
Binding getBinding()
273
void setBinding(Binding binding)
274
}
275
276
class Expando implements GroovyObject {
277
Expando()
278
Expando(Map<String, Object> properties)
279
}
280
281
abstract class GString implements Comparable, CharSequence, Writable {
282
Object[] getValues()
283
String[] getStrings()
284
String toString()
285
}
286
```
287
288
### Range Types
289
290
```groovy { .api }
291
class IntRange implements Range<Integer> {
292
IntRange(int from, int to)
293
IntRange(boolean inclusive, int from, int to)
294
boolean contains(Object value)
295
List<Integer> step(int stepSize)
296
}
297
298
class ObjectRange implements Range<Comparable> {
299
ObjectRange(Comparable from, Comparable to)
300
ObjectRange(Comparable from, Comparable to, boolean inclusive)
301
}
302
```
303
304
### Exception Types
305
306
```groovy { .api }
307
class GroovyRuntimeException extends RuntimeException {
308
GroovyRuntimeException(String message)
309
GroovyRuntimeException(String message, Throwable cause)
310
}
311
312
class MissingMethodException extends GroovyRuntimeException {
313
MissingMethodException(String method, Class type, Object[] arguments)
314
String getMethod()
315
Class getType()
316
}
317
318
class MissingPropertyException extends GroovyRuntimeException {
319
MissingPropertyException(String property, Class type)
320
String getProperty()
321
Class getType()
322
}
323
```