This document discusses Groovy and Grails for Java developers. It provides an overview of Groovy and Grails, including what Grails is (a rapid web application development framework for the JVM), its model-view-controller structure, and how it handles things like database access and internationalization out of the box. It also addresses how Grails integrates with tools like Gradle and how it can be used with different databases, including legacy databases. The document demonstrates some Groovy concepts and provides resources for learning more.
62. Gradle
• Build tool with built-in dependency management
• Conventions through plugins
• Multi-project support
• Full access to tasks and dependency tree
• Easy to write your own tasks
– either in the build file
– or via Groovy/Java classes
http://www.gradle.org/
62
62
63. • Parallel processing for Groovy
• Actors library making full use of closures
@Grab(group='org.codehaus.gpars', module='gpars', version='0.11')
import groovyx.gpars.GParsPool
GParsPool.withPool {
def animals = ['dog', 'ant', 'cat', 'whale']
println(animals.anyParallel {it ==~ /ant/} ?
'Found an ant' : 'No ants found')
println(animals.everyParallel {it.contains('a')} ?
'All animals contain a' : 'Some animals can live without an a')
}
http://gpars.codehaus.org/
63
63
64. Try it out!
http://groovyconsole.appspot.com/
64
64
65. What is Grails?
• Rapid Web Application Development Framework
– for the JVM
– with first-class Java integration
• Inspired by Ruby on Rails, Django and others
– Convention over Configuration
– Don’t Repeat Yourself (DRY)
65
65
66. What is Grails?
Grails
Servlet
Web MVC GSP (Views)
Container
GORM
Database I18n
(Data Access)
Build Test Support Doc Engine
66
66
68. What is Grails?
Web Controllers
The Domain Model
i18n bundles
Business Logic
Custom View Tags
Views & Layouts
Libraries (JARs)
Build Commands
Additional Sources
Tests
Web Resources
68
68
71. Enterprise requirements
Web App
Messaging JEE
Legacy
Services
Databases
Is this a problem for Grails apps?
71
71
72. Build
• Remember the Grails project structure?
– add in build events and...
Can’t build natively with other build tools!
Ant Maven Gradle
Grails Build System
72
72
75. ‘Legacy’ Databases
• Grails can create a database from your domain model...
• ...but what if you don’t own the database?
– DBA determines structure
– Company conventions
– Existing ‘legacy’ database
75
75
76. Option 1: Custom ORM mapping
• No existing domain model
• Schema not too far off the beaten track
class Book {
...
static mapping = {
table "books"
title type: "books"
author column: "author_ref"
}
}
76
76
78. Option 3: Hibernate XML Mappings
• You have Java model + Hibernate mapping files
• Schema is way off the beaten track
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE ...>
<hibernate-configuration>
<session-factory>
<mapping resource="org.ex.Book.hbm.xml"/>
<mapping resource="org.ex.Author.hbm.xml"/>
...
</session-factory>
</hibernate-configuration>
grails-app/conf/hibernate/hibernate.cfg.xml
78
78
79. Constraints
Given domain class:
org.example.myapp.domain.Book
Then:
src/java/org/example/myapp/domain/BookConstraints.groovy
constraints = {
title blank: false, unique: true
...
}
79
79
80. Option 4: GORM JPA Plugin
• GORM layer over JPA
• Use your own JPA provider
• Useful for cloud services that only work with JPA, not
Hibernate
80
80
81. Database Migration Plugin
Pre-production, Hibernate ‘update’ or ‘create-drop’
dbm-generate-changelog
dbm-changelog-sync
Change domain model
dbm-gorm-diff
dbm-update
81
81
83. Grails is Spring
• Spring MVC under the hood
• Grails provides many useful beans
– e.g. grailsApplication
• Define your own beans!
– resources.xml/groovy
– In a plugin
83
83
85. Summary
• Various options for integrating Grails with:
– Development/build
– Deployment processes
• Works with many external systems
– Solid support for non-Grailsy DB schemas
– Flexible messaging & web service support
85
85
86. More info
• w: http://grails.org/
• f: http://grails.org/Mailing+Lists
• e: pledbrook@vmware.com
• t: pledbrook
• b: http://blog.springsource.com/author/peter-ledbrook/
86
86