export GROOVY_HOME=~/path/to/groovy/ export PATH=$GROOVY_HOME/bin:$PATH
println 1.class int a = 10 println a.class
class java.lang.Integer class java.lang.Integer
javaString = 'java' groovyString = "${javaString}" j = '${javaString}' bigGroovyString = """ ${javaString} ${groovyString} ${j} ${2 + 2} """ println bigGroovyString
java java ${javaString} 4
groovy:000> a = "a" ===> a groovy:000> a + "123" ===> a123 groovy:000> a * 5 ===> aaaaa
groovy:000> a = 'abc' ===> abc groovy:000> a++ ===> abd groovy:000> a-- ===> ab
groovy:000> r =~ '^a$' ===> java.util.regex.Matcher[pattern=^a$ region=0,1 lastmatch=]
groovy:000> a = [1, 3, 5] ===> [1, 3, 5] groovy:000> b = [1: true, 0: false] ===> {1=true, 0=false}
groovy:000> a = "0123456789" ===> 0123456789 groovy:000> a[1..4] ===> 1234 groovy:000> a[1..-1] ===> 123456789 groovy:000> a[-1..0] ===> 9876543210 groovy:000> a[1..<9] ===> 12345678 groovy:000> a[1, 3, 5] ===> 135 groovy:000> b = 1..5 ===> 1..5 groovy:000> a[b] ===> 12345
groovy:000> 'a'..'aa' ===> a..aa
for (i in 0..9) { print i } for (int i = 0; i < 9; ++i) { print i } for (Integer i : 0..9) { print i }
def functionA(argA) { print ArgA } int functionB(int argB) { print argB return argB } String fuctionC() { "Hello World" }
def cl = {a, b -> println a println b } cl(1, 2)
1.upto 10, { print it } 10.times { print it }
'qwerty'.each { print it } ('a'..'z').each { print it } ('a'..'z').findAll { el -> // = filter el in ['e', 'y', 'u', 'i', 'o', 'a'] }.each { print it + ' ' } (0..10).collect { el -> // = map el * 10 }.each { print it + ' ' } def sum = (0..10).inject(0) { prev, elem -> // = reduce return prev + elem }
def cloA = {param -> def cloB = { return param * 10 } } def b = cloA(10) println b(10)
new File('.').eachFile { println it }
./.project ./src ./.settings ./.classpath ./bin
new File('textfile.txt').eachLine { println it }
def pw = new File('textfile.txt').newPrintWriter() pw.println("new line")
class Account { String name BigDecimal value } // // - // a = new Account() // a.setName("Account #1") // a.setValue(new BigDecimal(10)) a = new Account(name : "Account #1", value : new BigDecimal(10)) // def name = a.getName() a.setName("Account #2") println "${a.name}" class Person { def first def last // void setFirst(first) { println "${this.first} is becoming ${first}" this.first = first } } p = new Person(first : "A", last : "G") // , p.first = "C" println "${p.first} ${p.last}" // java class ExtendedAccount extends Account { def debt // ExtendedAccount(name, value, debt) { setName(name) setValue(value) setDebt(debt) } def String toString() { "${name} ${value} ${debt}" } } // "Could not find matching constructor for: ExtendedAccount()" //e = new ExtendedAccount() println new ExtendedAccount("A", new BigDecimal(10), 1)
@Immutable class ImmutableClass { String a Integer b } def ic = new ImmutableClass(a : "a", b : 1)
def b = a ?: "b"
def user = Users.get("a") def posts = user?.posts println posts
parent*.action == parent.collect {ch -> child?.action}
def sizes = ['string', 'long string']*.size() println sizes
[6, 11]
def x = [2, 3] def y = [0, 1, *x, 4] println y def a = [3 : 'c', 4 : 'd'] def b = [1 : 'a', 2: 'b', * : a, 5 : 'e'] println b
[0, 1, 2, 3, 4] [1:a, 2:b, 3:c, 4:d, 5:e]
class RandomVal { // private def value private Random randomGen = new Random() def next() { this.value = randomGen.nextInt() } RandomVal() { this.value = randomGen.nextInt() } def String toString() { "${this.value}" } } def r = new RandomVal() println(r) r++ println(r)
import groovy.sql.Sql def final ADDRESS = "jdbc:jtds:sqlserver://serverName/dbName" def final USERNAME = "username" def final PASSWD = "password" def final DRIVER = "net.sourceforge.jtds.jdbc.Driver" sql = Sql.newInstance(ADDRESS, USERNAME, PASSWD, DRIVER) sql.eachRow("select * from tableName") { el -> println "${el.id} -- ${el.firstName}" } def firstName = "A" def lastName = "G" sql.execute("insert into tableName (firstName, lastName) " + "values (${firstName}, ${lastName})") sql.execute("insert into tableName (firstName, lastName) " + "values (?, ?)", [firstName, lastName])
import groovy.xml.MarkupBuilder def mb = new MarkupBuilder() mb.html() { head() { title("This is the title") } body() { div("class" : "main") { p("this is the body") } } }
<html> <head> <title>This is the title</title> </head> <body> <div class='main'> <p>this is the body</p> </div> </body> </html>
def fb = new MarkupBuilder(new File("index.html").newPrintWriter())
import groovy.xml.MarkupBuilder import java.io.StringWriter def sw = new StringWriter() def mb = new MarkupBuilder(sw) mb.html() { body() { div("class" : "main") { p("this is the body") } div() { p("this is the body 1") p("this is the body 2") p("this is the body 3") } } } def xml = sw.toString() println xml import groovy.util.XmlParser; def parser = new XmlParser() def doc = parser.parseText(xml) //def doc = parser.parse("index.html") println doc.body.div[1].p[1] // Node println doc.body.div[1].p // , Node println doc.body.div["@class"] // class div
<html> <body> <div class='main'> <p>this is the body</p> </div> <div> <p>this is the body 1</p> <p>this is the body 2</p> <p>this is the body 3</p> </div> </body> </html> p[attributes={}; value=[this is the body 2]] [p[attributes={}; value=[this is the body 1]], p[attributes={}; value=[this is the body 2]], p[attributes={}; value=[this is the body 3]]] [main, null]
<servlet> <servlet-name>GroovyServlet</servlet-name> <servlet-class>groovy.servlet.GroovyServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>GroovyServlet</servlet-name> <url-pattern>*.groovy</url-pattern> </servlet-mapping>
html.html() { body() { div("class" : "main") { p("this is the body") } div() { p("this is the body 1") p("this is the body 2") p("this is the body 3") } } }
Source: https://habr.com/ru/post/122127/
All Articles