Groovy Introduction
Groovy Introduction
Groovy Introduction
http://manning.com/
http://apress.com/
About Me
Jeff Brown
Open Source
Others...
JavaScript
Ruby
Python
App Servers
Servlet Containers
In Recent Weeks...
Download Release
http://groovy.codehaus.org/
Set GROOVY_HOME
Give It A Spin...
$ groovy -version
Groovy Version: 1.0-RC-01 JVM: 1.4.2_13-b06
$ groovy -e " println 'Hello From Groovy' "
Hello From Groovy
$ groovy -e "a=10;b=4;c=a*b;println c"
40
Groovy Shell
$ groovysh
Let's get Groovy!
================
Version: 1.0-RC-01 JVM: 1.4.2_13-b06
Type 'exit' to terminate the shell
Type 'help' for command help
Type 'go' to execute the statements
groovy> width = 5
groovy> height = 12
groovy> area = width * height
groovy> println area
groovy> go
60
Groovy Console
$ groovyConsole
Groovy Scripts
// mygroovyscript.groovy
println 'Hello From My Groovy Script'
groovy mygroovyscript.groovy
Hello From My Groovy Script
Groovy Classes
// MyGroovyTest.groovy
class MyGroovyTest {
def sayHello() {
println 'Hello From MyGroovyTest'
}
static void main(args) {
def mgt = new MyGroovyTest()
mgt.sayHello()
}
}
groovy MyGroovyTest.groovy
Hello From MyGroovyTest
groovyc
CLASSPATH
groovy-all-[version].jar
in $GROOVY_HOME/embeddable/
groovyc MyGroovyTest.groovy
java MyGroovyTest
Hello From MyGroovyTest
Some Language Basics
Everything Is An Object
GString
Closures
Collections
Categories
Ranges
Groovy Beans
Builders
Meta Programming
The following slides are a bunch
of code snippets run in groovysh
and groovyConsole to accompany
live demo and discussion.
Everything Is An Object
Groovy Strings
Known As GStrings
expressions surrounded by ${ }
like charAt(i)
A Block Of Code
Multiple Arguments
groovy> myMap = [name:'Jeff', location:'St. Louis']
groovy> myMap.each { key, value ->
println "${key} is ${value}"
}
groovy> go
location is St. Louis
name is Jeff
Collections
Adding To A List
groovy> albums = ['Rush']
groovy> albums << 'Fly By Night'
groovy> albums += 'Caress Of Steel'
groovy> albums.add '2112'
groovy> println albums
groovy> go
["Rush", "Fly By Night", "Caress Of Steel", "2112"]
Collections
Map Manipulation
groovy> myMap = [bass:'Geezer', drums:'Bill']
groovy> myMap['vocals'] = 'Ozzy'
groovy> myMap.guitar = 'Tony'
groovy> println myMap
groovy> go
["drums":"Bill", "vocals":"Ozzy",
"bass":"Geezer", "guitar":"Tony"]
Groovy Categories
Groovy Categories
Groovy Categories
Ranges
Ranges
Ranges
Groovy Beans
Similar To POJOs
name = cardinals.teamName
name = cardinals.getTeamName()
cardinals.setTeamName('Saint Louis')
Groovy Beans
private field
public getter/setter
No 'package' Level
MarkupBuilder
builder = new groovy.xml.MarkupBuilder()
builder.baseball {
league(name:"National") {
team("Cardinals")
team("Cubs")
team("Mets")
}
league(name:"American") {
team("Angels")
team("Yankees")
team("Royals")
}
}
MarkupBuilder
<baseball>
<league name='National'>
<team>Cardinals</team>
<team>Cubs</team>
<team>Mets</team>
</league>
<league name='American'>
<team>Angels</team>
<team>Yankees</team>
<team>Royals</team>
</league>
</baseball>
MarkupBuilder
builder = new groovy.xml.MarkupBuilder()
builder.html() {
head() {
title('Markup Builder Demo')
}
body {
h1('Bands')
ul {
li('Rush')
li('King Crimson')
li('Opeth')
}
}
}
MarkupBuilder
<html>
<head>
<title>Markup Builder Demo</title>
</head>
<body>
<h1>Bands</h1>
<ul>
<li>Rush</li>
<li>King Crimson</li>
<li>Opeth</li>
</ul>
</body>
</html>
Meta Programming
class MyGroovyThing {
Object invokeMethod(String name, Object args) {
// do something cool
}
}
Live Meta Programming Demo...
Calling Groovy From Java
GroovyShell
GroovyClassLoader
http://groovy.codehaus.org/
http://grails.org/
Groovy Portal
http://aboutgroovy.com/
My Blog
http://javajeff.blogspot.com/
http://www.ociweb.com/jnb/
The End
Thank You For Coming!
Q&A