What makes me like Groovy
$ groovy -e "println 'Hello, Groovy.'"
$ groovy myScript.groovy
# Faster using groovy server (JVM re-use)
$ groovyclient -e "println 'Hello, GroovyServ.'"
(even community edition)
https://groovyconsole.appspot.com
Place groovy files in /src/main/groovy
2.3.4
1.3
grvy
true
org.codehaus.gmaven
gmaven-plugin
${gmaven-version}
generateStubs
compile
generateTestStubs
testCompile
1.7
org.codehaus.gmaven.runtime
gmaven-runtime-1.7
1.3
org.codehaus.groovy
groovy-all
org.codehaus.groovy
groovy-all
${groovy-version}
def name = "Xavier"
println "My name is $name"
assert "Xavier" == "Mon nom est Xavier."[12..17]
assert "Xavier" == "Mon nom est Xavier."[12..-2]
int i = "18".toInteger()
//Multiline
def str = """
Lorem Ipsum is simply dummy text of the printing
and typesetting industry.
Lorem Ipsum has been the industry's
standard dummy text ever since the 1500s
"""
class Person {
String name
int age
}
new Person(name:"Xavier", age:21)
def map = [
"name":"Xavier",
"age":21
]
new Person(map)
assert [] instanceof ArrayList
assert [1, 2] + [3, 4] == [1, 2, 3, 4]
def names = ["Xavier", "Lenny"]
names.each {name -> println name}
names.each {println it}
names.eachWithIndex { String name, int i -> println "$i:$name"}
names.collect{name -> "Hi $name"}
names.find {it.startsWith("X")}
names.findAll {it.length() > 3}
names.groupBy {it[0]}."X".size() // => 1
def (beforeM, afterM) = names.split { it[0] < "M"}
assert beforeM == ["Lenny"] && afterM == ["Xavier"]
//create a closure and apply it on the collection
def p = {o -> println "${new Date()}...$o"}
names.each(p)
//chain
"xavier-lenny".split("-").collect { it.toUpperCase() }.join(";").with(p)
//read the whole content of a file
def body = new File("file-name.txt").text
//and same for write
new File("file-name.txt").text = "this is a file body \n second line"
new File('/path/to/file').eachLine('UTF-8') { println it }
def myCsvFile = new File("/tmp/a.csv")
myCsvFile.text = """author;age\nXavier;29\nLenny;7"""
myCsvFile.splitEachLine(";", { f ->
println "${f[0]}-${f[1]}"
})
myCsvFile.readLines().size() == 3
//Sample file for testing
def myFile = new File("/tmp/a.json")
myFile.text = """{"name":"Xavier", "age":29}"""
//Json to Object from File or from String
def author = new JsonSlurper().parse(myFile)
author = new JsonSlurper().parseText(myFile.text)
assert author instanceof Map
//Edit the map
author.age = 30
//Object to Json
myFile.text = new JsonBuilder(author).toString()
println myFile.text
// => {"age":30,"name":"Xavier"}
//Sample file for testing
def myXmlFile = new File("/tmp/a.xml")
myXmlFile.text = """Xavier """
//Json to Object from File or from String
def authorX
authorX = new XmlSlurper().parse(myXmlFile) //SAX
authorX = new XmlParser().parse(myXmlFile) //DOM
authorX = new XmlParser().parseText(myXmlFile.text)
assert authorX instanceof Map
assert authorX.name == "Xavier"
assert authorX.@age == 30
def list = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
withPool( 10 ) {
list.eachParallel { elm, idx ->
println "Working on elt:$elm at idx:$idx"
}
println "Fork/join and wait all tasks to finish... Done ! "
}