grails 2.0 update

43
What’s new in 2.0 t: @pledbrook g: +PeterLedbrook e: [email protected] Peter Ledbrook SpringSource Wednesday, 2 May 12

Upload: peter-ledbrook

Post on 01-Sep-2014

2.895 views

Category:

Technology


0 download

DESCRIPTION

See what's new in Grails 2.0

TRANSCRIPT

Page 1: Grails 2.0 Update

What’s new in 2.0

t: @pledbrookg: +PeterLedbrooke: [email protected]

Peter LedbrookSpringSource

Wednesday, 2 May 12

Page 2: Grails 2.0 Update

The Year in Grails

• Grails 1.3–Plugins in Dependency DSL, Groovy 1.7, Named

Queries etc.• More and more plugins

–Spring Security Core et al.–RabbitMQ–Gemfire–Resources, etc.

• NoSQL–Redis, MongoDB, Riak, etc.

2

Wednesday, 2 May 12

Page 3: Grails 2.0 Update

High Profile Sites

3

Wednesday, 2 May 12

Page 4: Grails 2.0 Update

Grails Continued Growth

4

0

200

400

600

800

2008 2009 2010 2011

Number of Plugins

Wednesday, 2 May 12

Page 5: Grails 2.0 Update

Development Environment Features

Wednesday, 2 May 12

Page 6: Grails 2.0 Update

New Console UI & Interactive Mode

6

Wednesday, 2 May 12

Page 7: Grails 2.0 Update

Better Unit Test Template

7

Wednesday, 2 May 12

Page 8: Grails 2.0 Update

Better Documentation Template

8

Wednesday, 2 May 12

Page 9: Grails 2.0 Update

Enhanced Error Reporting

9

Wednesday, 2 May 12

Page 10: Grails 2.0 Update

H2 Console

• Available at http://localhost:8080/app/dbconsole in development only!

10

Wednesday, 2 May 12

Page 11: Grails 2.0 Update

Plugin Portal Usage Tracking

• Opt-in usage tracking of plugins

11

Wednesday, 2 May 12

Page 12: Grails 2.0 Update

Upgraded Libraries

12

Wednesday, 2 May 12

Page 13: Grails 2.0 Update

New Automatic Reloading

• Reloading in run-app works with–Typed service references–Domain classes–src/groovy, src/java

• Any command with -reloading• Interactive mode and integration tests

13

Wednesday, 2 May 12

Page 14: Grails 2.0 Update

Binary Plugins

• Package pre-compiled plugins into JAR files• Deployable as standard JARs to Maven repositories• Declared as JAR dependencies• Commercial plugins more viable• No special IDE integration needed

14

$ grails package-plugin --binary

Wednesday, 2 May 12

Page 15: Grails 2.0 Update

Web Features

Wednesday, 2 May 12

Page 16: Grails 2.0 Update

Methods as Actions and Binding Arguments

• Actions are now declared as public methods

• Form parameters bound to method arguments

16

def save(String name, int age) { // remaining}

<g:form name="myForm" action="save"> <input name="name" /> <input name="age" /></g:form>

Wednesday, 2 May 12

Page 17: Grails 2.0 Update

HTML5 Scaffolding

17

Wednesday, 2 May 12

Page 18: Grails 2.0 Update

New APIs

• Page Rendering

• Link Generation

18

PageRenderer renderervoid welcomeUser(User user) { def contents = renderer.render(view:"/emails/welcome", model:[user: user]) ...}

LinkGenerator generatordef generateLink() { generator.link(controller:"book", action:"list")}

Wednesday, 2 May 12

Page 19: Grails 2.0 Update

Servlet 3.0 Async

• Servlet 3.0 async API supported for async response rendering

19

def index() { def ctx = startAsync() ctx.start { new Book(title:"The Stand").save() render template:"books", model:[books: Book.list() ] ctx.complete() }}

Wednesday, 2 May 12

Page 20: Grails 2.0 Update

Advanced Static Resource Handling

• Integrated resource plugin into core–http://grails.org/plugin/resources

• Tuning static resources no longer a headache–gzip (http://grails.org/plugin/zipped-resources)–cache (http://grails.org/plugin/cached-resources)–de-duplication–bundling

• New tags to ease integration–img–external– javascript

20

Wednesday, 2 May 12

Page 21: Grails 2.0 Update

Bundling Static Resources

21

modules = { core { dependsOn 'utils' resource url:'/js/core.js', disposition: 'head' resource url:'/js/ui.js' resource url:'/css/main.css', resource url:'/css/branding.css' resource url:'/css/print.css', attrs:[media:'print'] } utils { dependsOn 'jquery' resource url:'/js/utils.js' }}

Wednesday, 2 May 12

Page 22: Grails 2.0 Update

Zipping and Caching

22

$ grails install-plugin cached-resources

$ grails install-plugin zipped-resources

Wednesday, 2 May 12

Page 23: Grails 2.0 Update

Other Web Novelties

• jQuery now the default • Easy Date Parsing

• Customizable URL formats• Filter exclusions

23

def val = params.date('myDate', 'dd-MM-yyyy')

Wednesday, 2 May 12

Page 24: Grails 2.0 Update

Persistence Features

Wednesday, 2 May 12

Page 25: Grails 2.0 Update

GORM API

• Plugins should not assume Hibernate!

25

Wednesday, 2 May 12

Page 26: Grails 2.0 Update

GORM Plugins

• Redis - http://grails.org/plugin/redis-gorm• MongoDB - http://grails.org/plugin/mongodb• Amazon SimpleDB - http://grails.org/plugin/simpledb• Amazon DynamoDB - http://grails.org/plugin/dynamodb• Neo4j - http://grails.org/plugin/neo4j• Riak - http://grails.org/plugin/riak• GORM JPA - http://grails.org/plugin/gorm-jpa• Hibernate - http://grails.org/plugin/hibernate

26

Wednesday, 2 May 12

Page 27: Grails 2.0 Update

Where Queries

• New, compile-time checked query DSL

• Uses native Groovy operators ==, !=, >, <, <=, >= etc

• Aggregate functions supported avg, sum, max, min etc.

27

def query = Person.where { firstName == "Bart"}Person bart = query.find()

def query = Person.where { firstName == "Fred" && !(lastName == 'Simpson')}

def query = Person.where { age > avg(age)}

Wednesday, 2 May 12

Page 28: Grails 2.0 Update

Multiple Data Sources

• Support for defining multiple scoped data sources

• Each data source accessible via static property

28

class ZipCode { String code static mapping = { datasource 'auditing' }}

def zipCode = ZipCode.auditing.get(42)

Wednesday, 2 May 12

Page 29: Grails 2.0 Update

?

SQL Database Migration

29

Hibernate ‘update’+

Production data=

Wednesday, 2 May 12

Page 30: Grails 2.0 Update

SQL Database Migration

• Install the Database Migration plugin:

• Official Docs at:–http://grails-plugins.github.com/grails-database-migration/

30

$ grails install-plugin database-migration

Wednesday, 2 May 12

Page 31: Grails 2.0 Update

SQL Database Migration

31

Pre-production, Hibernate ‘update’ or ‘create-drop’

dbm-generate-changelogdbm-changelog-sync

Change domain model

dbm-gorm-diffdbm-update

Wednesday, 2 May 12

Page 32: Grails 2.0 Update

SQL Reverse Engineering

32

class Person { String name Integer age ...}

$ grails install-plugin db-reverse-engineer$ grails db-reverse-engineer

Wednesday, 2 May 12

Page 33: Grails 2.0 Update

Other GORM Improvements

• Abstract base domain classes–These now result in a table

• findOrCreateWhere()• findOrSaveWhere()

33

def user = User.findByLogin('admin')if (!user) { user = new User(login: 'admin') user.save(failOnError: true)}

def user = User.findOrSaveWhere(login: 'admin')

Wednesday, 2 May 12

Page 34: Grails 2.0 Update

Better Unit Testing

Wednesday, 2 May 12

Page 35: Grails 2.0 Update

Unit Testing 1.x

• mockDomain() had only partial GORM support–always lagged changes in GORM

• Inheritance-based–hierarchy duplicated for Spock–difficult to extend

• Weak support for web-related testing–controllers–tag libraries

35

Wednesday, 2 May 12

Page 36: Grails 2.0 Update

The Mixin Approach

36

@TestFor(MyController)@Mock(Person)class MyControllerUnitTests { void setUp() { new Person(...).save() new Person(...).save() }

void testIndex() { def model = this.controller.index() ... }}

Wednesday, 2 May 12

Page 37: Grails 2.0 Update

Spockified

37

@TestFor(MyController)@Mock(Person)class MyControllerUnitSpec extends Specification { void loadPeople() { new Person(...).save() new Person(...).save() }

def 'Test index action'() { given: 'Some people' loadPeople() when: 'The index action is called' def model = this.controller.index() then: 'The people variable is in the model' model.people != null }}

Wednesday, 2 May 12

Page 38: Grails 2.0 Update

In-Memory GORM

• Full GORM implementation against ConcurrentHashMap• Based on GORM for NoSQL codebase• Support for

–Criteria queries–Where queries–Dynamic finders–Detached criteria

38

Wednesday, 2 May 12

Page 39: Grails 2.0 Update

Support for testing...

• Tag libraries• Command objects• XML & JSON responses• File upload• View and template rendering• Filters• URL mappings• Criteria queries• and more!

39

Wednesday, 2 May 12

Page 40: Grails 2.0 Update

Contributions

• 110+ pull requests on grails-core• 60+ pull requests on grails-docs• More and more plugins• GitHub for the win!

–grails-core–grails-docs–grails-website–grails-maven–and many, many plugins

40

Wednesday, 2 May 12

Page 41: Grails 2.0 Update

Grails in the Cloud

41

Wednesday, 2 May 12

Page 42: Grails 2.0 Update

Roadmap

42

Wednesday, 2 May 12

Page 43: Grails 2.0 Update

43

Thank you!

Questions?

Wednesday, 2 May 12