Groovy Scripting for the JVM
Light · Postcard
102 × 152 mm · PDF
Dark · Postcard
102 × 152 mm · PDF
Ink saver · Postcard
102 × 152 mm · PDF
Light · A4 landscape
297 × 210 mm · PDF
Dark · A4 landscape
297 × 210 mm · PDF
Ink saver · A4 landscape
297 × 210 mm · PDF
Light · A3 landscape
420 × 297 mm · PDF
Dark · A3 landscape
420 × 297 mm · PDF
Ink saver · A3 landscape
420 × 297 mm · PDF
Start here
groovy <file>.groovy
groovy -e '<code>'
groovyshgroovy -vgroovy -n -e '<code>'
groovy -cp <path> <file>
groovy -Dkey=value <file>
@Grab('g:m:v')
Concepts
- Script .groovy source
- Bytecode in memory
- JVM needs a JDK
- Libraries Maven repos
Running
groovy <file>.groovy [args]
groovy -e '<code>'
groovy -vgroovy -cp <path> <file>
groovy -Dkey=value <file>
groovy -d <file>
groovy -l <port>
groovy -b <class> <file>
groovy -pa <file>
groovy --helpOne-liners
groovy -n -e '<code>' <file>
groovy -p -e '<code>' <file>
groovy -a -n -e '<code>' <file>
groovy -i .bak -p -e '<code>'
<file>
groovy -c <charset> <file>
Shell
groovyshgroovysh -qgroovysh -e '<code>'
/help/show/load <file>
/save <file>
/history/inspect <expr>
/grab <g:m:v>
/exitDependencies
@Grab('<g>:<m>:<v>')
@GrabResolver(name='<n>',
root='<url>')
@GrabExclude('<group>:<module>')
@GrabConfig(systemClassLoader=true)
grape install <group> <module>
[<version>]
grape listgrape resolve <group> <module>
<version>
grape uninstall <group> <module>
<version>
Syntax
def x = 1println "Hi ${name}"
'''line1 line2'''
/a\d+/def f = { a, b -> a + b }
list.collect { it * 2 }
a?.ba ?: blist*.namea <=> ba == bx ?= 5obj.&methodclass Foo { String name }
@CompileStatic@TypeCheckedCollections
[1, 2, 3][a: 1, b: 2][] as Set(1..5)list.each { println it }
list.findAll { it > 1 }
list.find { it > 1 }
list.inject(0) { s, x -> s + x }
list.sum()list.groupBy { it.size() }
list.sort { it.name }
list.collectEntries { [(it): it.size()] }
map.each { k, v -> println "$k=$v" }
list.any { it > 1 }
list.unique()Files & text
new File('<path>').text
new File('<path>').eachLine { }
new File('<path>').text =
'<data>'
new File('<path>') <<
'<data>'
new File('.').eachFileRecurse { }
'echo hi'.execute().text
str =~ /<regex>/
str ==~ /<regex>/
new groovy.json.JsonSlurper().parseText(s)
groovy.json.JsonOutput.toJson(obj)
Recipes
@Grab('org.apache.commons:commons-lang3:3.14.0')import org.apache.commons.lang3.StringUtilsprintln StringUtils.capitalize('groovy')
groovy -n -e 'if (line =~ /ERROR/) println line' app.log
groovy -i .bak -p -e 'line.replace("foo", "bar")' *.txt
def url = 'https://api.example.com/items'def data = new groovy.json.JsonSlurper().parse(url.toURL())data.each { println it.name }



