This document discusses how to integrate Grails applications with various enterprise systems and processes. It covers integrating Grails with build tools like Ant and Maven, managing dependencies, connecting to legacy databases, deploying applications, and integrating with Spring frameworks for messaging, web services, and other enterprise features. Grails can work with many external systems through plugins, custom ORM mappings, and by treating Grails as a Spring application. This allows Grails applications to fit into a variety of enterprise environments and development processes.
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook
1. Peter Ledbrook / VMware
Grails in the Java Enterprise
Tuesday, 1 November 11
2. 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)
2
Tuesday, 1 November 11
3. What is Grails?
Grails
Servlet
Web MVC GSP (Views)
Container
GORM
Database I18n
(Data Access)
Build Test Support Doc Engine
3
Tuesday, 1 November 11
5. 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
5
Tuesday, 1 November 11
12. 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
12
Tuesday, 1 November 11
13. Ant Integration
An Ant task built in (grails.ant.GrailsTask)
Template Ant build:
grails integrate-with --ant
• Uses Ivy for dependency management
• Not compatible with Ant 1.8
...or use ‘java’ task to call Grails command
• Grails manages dependencies
• Use ‘grails’ for build
13
Tuesday, 1 November 11
14. Maven
Maven Grails Plugin:
https://github.com/grails/grails-maven
Use Maven 2 or 3 to build Grails projects
Declare dependencies in POM
Works for both applications and plugins!
Integration test framework:
https://github.com/grails/
grails_maven_plugin_testing_tests
14
Tuesday, 1 November 11
15. Getting Started
mvn archetype-generate ...
e.g. -Xmx256m
mvn initialize
-XX:MaxPermSize=256m
Set MAVEN_OPTS
Optional: add ‘pom true’ to dependency DSL
15
Tuesday, 1 November 11
16. Packaging Types
‘war’
• Must configure execution section
• Works with plugins that depend on ‘war’
‘grails-app’
• Less configuration
‘grails-plugin’
• For plugins!
16
Tuesday, 1 November 11
20. Customise the Build
Create new commands in <proj>/scripts
Package the commands in a plugin!
Create <proj>/scripts/_Events.groovy
• Interact with standard build steps
• Add test types
• Configure embedded Tomcat
20
Tuesday, 1 November 11
21. What the future holds...
Grails 3.0 will probably move to Gradle
• More powerful and more flexible
• Standard, well documented API
• Ant & Maven support
21
Tuesday, 1 November 11
22. Integration Points
Build
Dependencies
Database
Deployment
Spring
22
Tuesday, 1 November 11
23. Dependencies are JARs
Use any Java library you like!
Full support for Maven-compatible repositories
Declarative dependencies
Plugins can be declared the same way
23
Tuesday, 1 November 11
26. Integration Points
Build
Dependencies
Database
Deployment
Spring
26
Tuesday, 1 November 11
27. ‘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
27
Tuesday, 1 November 11
28. 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"
}
}
28
Tuesday, 1 November 11
30. 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
30
Tuesday, 1 November 11
31. Constraints
Given domain class:
org.example.myapp.domain.Book
Then:
src/java/org/example/myapp/domain/BookConstraints.groovy
constraints = {
title blank: false, unique: true
...
}
31
Tuesday, 1 November 11
32. 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
32
Tuesday, 1 November 11
33. Option 5: Remote service back-end
Don’t have to use GORM
Use only controllers and services
• Grails services back onto remote services
Web App
SOAP, RMI, HTTP Invoker, etc.
Invoice Log Service
...
33
Tuesday, 1 November 11
39. Integration Points
Build
Dependencies
Database
Deployment
Spring
39
Tuesday, 1 November 11
40. grails war
Build properties:
• grails.war.copyToWebApp
• grails.war.dependencies
• grails.war.resources
• grails.project.war.file
40
Tuesday, 1 November 11
41. Control of JARs
grails.project.dependency.resolution = {
defaultDependenciesProvided true
inherits "global"
log "warn"
...
}
grails war --nojars => WEB-INF/lib/<empty>
=> No Grails JARs in WEB-INF/lib
41
Tuesday, 1 November 11
42. Data Source
JNDI:
dataSource {
jndiName = "java:comp/env/myDataSource"
}
System property:
dataSource {
url = System.getProperty("JDBC_STRING")
}
42
Tuesday, 1 November 11
43. Data Source
Config.groovy:
grails.config.locations = [
"file:./${appName}-config.groovy",
"classpath:${appName}-config.groovy" ]
For run-app: ./<app>-config.groovy
For Tomcat: tomcat/lib/<app>-config.groovy
43
Tuesday, 1 November 11
44. Integration Points
Build
Dependencies
Database
Deployment
Spring
44
Tuesday, 1 November 11
45. 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
45
Tuesday, 1 November 11
47. Enterprise Integration
Spring opens up a world of possibilities
• Spring Integration/Camel
• Messaging (JMS/AMQP)
• ESB
• RMI, HttpInvoker, etc.
Web services & REST
47
Tuesday, 1 November 11
53. 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
53
Tuesday, 1 November 11
54. 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/
54
Tuesday, 1 November 11