39
Peter Ledbrook Grails in the Enterprise 1 Monday, 30 May 2011

GR8Conf 2011: Grails Enterprise Integration by Peter Ledbrook

  • Upload
    gr8conf

  • View
    1.830

  • Download
    2

Embed Size (px)

Citation preview

Page 1: GR8Conf 2011: Grails Enterprise Integration by Peter Ledbrook

Peter Ledbrook

Grails in the Enterprise

1Monday, 30 May 2011

Page 2: GR8Conf 2011: Grails Enterprise Integration by Peter Ledbrook

Integration Points

• Build• Dependencies• Database• Deployment• Spring

2

2Monday, 30 May 2011

Page 3: GR8Conf 2011: Grails Enterprise Integration by Peter Ledbrook

Integration Points

• Build• Dependencies• Database• Deployment• Spring

3

3Monday, 30 May 2011

Page 4: GR8Conf 2011: Grails Enterprise Integration by Peter Ledbrook

Build

4

4Monday, 30 May 2011

Page 5: GR8Conf 2011: Grails Enterprise Integration by Peter Ledbrook

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

5

5Monday, 30 May 2011

Page 6: GR8Conf 2011: Grails Enterprise Integration by Peter Ledbrook

6

6Monday, 30 May 2011

Page 7: GR8Conf 2011: Grails Enterprise Integration by Peter Ledbrook

Maven

• Maven Grails Plugin: https://github.com/grails/grails-maven

• Use Maven 2 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

7

7Monday, 30 May 2011

Page 8: GR8Conf 2011: Grails Enterprise Integration by Peter Ledbrook

Getting Started

mvn archetype-generate ...

mvn initialize

Set MAVEN_OPTS

Optional: add ‘pom true’ to dependency DSL

e.g. -Xmx256m -XX:MaxPermSize=256m

8

8Monday, 30 May 2011

Page 9: GR8Conf 2011: Grails Enterprise Integration by Peter Ledbrook

Packaging Types

• ‘war’– Must configure execution section– Works with plugins that depend on ‘war’

• ‘grails-app’– Less configuration

• ‘grails-plugin’– For plugins!

9

9Monday, 30 May 2011

Page 10: GR8Conf 2011: Grails Enterprise Integration by Peter Ledbrook

10

10Monday, 30 May 2011

Page 11: GR8Conf 2011: Grails Enterprise Integration by Peter Ledbrook

Maven & Grails Plugins

> grails release-plugin

==

> mvn deploy

11

11Monday, 30 May 2011

Page 12: GR8Conf 2011: Grails Enterprise Integration by Peter Ledbrook

Maven & Grails Plugins

<dependency> <groupId>org.grails.plugins<groupId> <artifactId>hibernate</artifactId> <type>grails-plugin</type></dependency>

Either:

Use ‘mvn deploy’ or Release plugin!

12

And ‘pom: false’

12Monday, 30 May 2011

Page 13: GR8Conf 2011: Grails Enterprise Integration by Peter Ledbrook

Maven & Grails Plugins

grails.project.dependency.resolution = { ... plugins { compile ":hibernate:1.3.6" } ...}

Or:

13

13Monday, 30 May 2011

Page 14: GR8Conf 2011: Grails Enterprise Integration by Peter Ledbrook

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

14

14Monday, 30 May 2011

Page 15: GR8Conf 2011: Grails Enterprise Integration by Peter Ledbrook

What the future holds...

• Grails 2.0 will move to Gradle– More powerful and more flexible– Standard, well documented API– Ant & Maven support

15

15Monday, 30 May 2011

Page 16: GR8Conf 2011: Grails Enterprise Integration by Peter Ledbrook

Integration Points

• Build• Dependencies• Database• Deployment• Spring

16

16Monday, 30 May 2011

Page 17: GR8Conf 2011: Grails Enterprise Integration by Peter Ledbrook

Dependency DSL

grails.project.dependency.resolution = { inherits "global" log "warn" repositories { grailsHome() mavenCentral() mavenRepo "http://localhost:8081/..." } ...}

17

17Monday, 30 May 2011

Page 18: GR8Conf 2011: Grails Enterprise Integration by Peter Ledbrook

Dependency DSL

grails.project.dependency.resolution = { inherits "global" log "warn" ... dependencies { compile "org.tmatesoft.svnkit:svnkit:1.3.3" test "org.gmock:gmock:0.8.1" } ...}

18

18Monday, 30 May 2011

Page 19: GR8Conf 2011: Grails Enterprise Integration by Peter Ledbrook

Integration Points

• Build• Dependencies• Database• Deployment• Spring

19

19Monday, 30 May 2011

Page 20: GR8Conf 2011: Grails Enterprise Integration by Peter Ledbrook

‘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

20

20Monday, 30 May 2011

Page 21: GR8Conf 2011: Grails Enterprise Integration by Peter Ledbrook

class Book { ... static mapping = { table "books" title type: "books" author column: "author_ref" }}

21

Option 1: Custom ORM mapping

21Monday, 30 May 2011

Page 22: GR8Conf 2011: Grails Enterprise Integration by Peter Ledbrook

Option 2: JPA annotations

<?xml version='1.0' encoding='UTF-8'?><!DOCTYPE ...><hibernate-configuration> <session-factory> <mapping class="org.ex.Book"/> <mapping class="org.ex.Author"/> ... </session-factory></hibernate-configuration>

22

22Monday, 30 May 2011

Page 23: GR8Conf 2011: Grails Enterprise Integration by Peter Ledbrook

<?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>

23

Option 3: Hibernate XML Mappings

23Monday, 30 May 2011

Page 24: GR8Conf 2011: Grails Enterprise Integration by Peter Ledbrook

constraints = { title blank: false, unique: true ...}

Constraints

Given domain class:

Then:

org.example.myapp.domain.Book

src/java/org/example/myapp/domain/BookConstraints.groovy

24

24Monday, 30 May 2011

Page 25: GR8Conf 2011: Grails Enterprise Integration by Peter Ledbrook

• GORM layer over JPA• Use your own JPA provider• Useful for cloud services that only work with JPA, not

Hibernate

25

Option 4: GORM JPA Plugin

25Monday, 30 May 2011

Page 26: GR8Conf 2011: Grails Enterprise Integration by Peter Ledbrook

Share your model!

26

26Monday, 30 May 2011

Page 27: GR8Conf 2011: Grails Enterprise Integration by Peter Ledbrook

Database Management

• Hibernate auto-DDL– Good for dev & test– Bad for production!

• Data migration– http://grails.org/plugin/database-migration

• Reverse engineer a model– http://grails.org/plugin/db-reverse-engineer

27

27Monday, 30 May 2011

Page 28: GR8Conf 2011: Grails Enterprise Integration by Peter Ledbrook

Integration Points

• Build• Dependencies• Database• Deployment• Spring

28

28Monday, 30 May 2011

Page 29: GR8Conf 2011: Grails Enterprise Integration by Peter Ledbrook

grails war

• Build properties:– grails.war.copyToWebApp– grails.war.dependencies– grails.war.resources– grails.project.war.file

29

29Monday, 30 May 2011

Page 30: GR8Conf 2011: Grails Enterprise Integration by Peter Ledbrook

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

30

30Monday, 30 May 2011

Page 31: GR8Conf 2011: Grails Enterprise Integration by Peter Ledbrook

Data Source

dataSource { jndiName = "java:comp/env/myDataSource"}

JNDI:

dataSource { url = System.getProperty("JDBC_STRING")}

System property:

31

31Monday, 30 May 2011

Page 32: GR8Conf 2011: Grails Enterprise Integration by Peter Ledbrook

Data Source

grails.config.locations = [ "file:./${appName}-config.groovy", "classpath:${appName}-config.groovy" ]

Config.groovy:

For run-app: ./<app>-config.groovy

For Tomcat: tomcat/lib/<app>-config.groovy

32

32Monday, 30 May 2011

Page 33: GR8Conf 2011: Grails Enterprise Integration by Peter Ledbrook

Integration Points

• Build• Dependencies• Database• Deployment• Spring

33

33Monday, 30 May 2011

Page 34: GR8Conf 2011: Grails Enterprise Integration by Peter Ledbrook

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

34

34Monday, 30 May 2011

Page 35: GR8Conf 2011: Grails Enterprise Integration by Peter Ledbrook

Example

import ...beans = { credentialMatcher(Sha1CredentialsMatcher) { storedCredentialsHexEncoded = true }

sessionFactory(ConfigurableLocalSessionFactoryBean) { dataSource = ref("dataSource") hibernateProperties = [ "hibernate.hbm2ddl.auto": "create-drop", "hibernate.show_sql": true ] }}

35

35Monday, 30 May 2011

Page 36: GR8Conf 2011: Grails Enterprise Integration by Peter Ledbrook

Enterprise Integration

• Spring opens up a world of possibilities– Spring Integration/Camel– Messaging (JMS/AMQP)– ESB– RMI, HttpInvoker, etc.

• Web services & REST

36

36Monday, 30 May 2011

Page 37: GR8Conf 2011: Grails Enterprise Integration by Peter Ledbrook

Grails Plugins

• Routing• JMS, RabbitMQ• CXF, Spring-WS, WS-Client• REST

37

37Monday, 30 May 2011

Page 38: GR8Conf 2011: Grails Enterprise Integration by Peter Ledbrook

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

38

38Monday, 30 May 2011

Page 39: GR8Conf 2011: Grails Enterprise Integration by Peter Ledbrook

Thank you!

Q & A

39

39Monday, 30 May 2011