Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Groovy & Grails for Java Developers

Groovy & Grails for Java Developers

Groovy is a dynamic language for the JVM with a Java-like syntax, and Grails is a rapid web application framework that uses the language extensively.

Find out how Groovy can improve a Java developer's life and what jobs it's really good for. Also learn about Grails and how it provides more than just a CRUD web framework, with strong enterprise integration features.

pledbrook

May 02, 2012
Tweet

Other Decks in Technology

Transcript

  1. Groovy and Grails for Java developers Peter Ledbrook SpringSource t:

    @pledbrook g: +PeterLedbrook e: pledbrook@vmware.com
  2. What is Groovy? • A dynamic language for the JVM

    – ...with a Java-like syntax – ...and optional static types • Uses and extends the JDK • Write scripts as well as classes • Seamless integration with Java – ...call Java from Groovy – ...or call Groovy from Java – ...all in the same project • Great IDE support in IntelliJ and Eclipse – NetBeans also has Groovy (& Grails support)
  3. 8

  4. 10

  5. 15

  6. 16

  7. 20

  8. 24

  9. 26

  10. 30

  11. 33

  12. 35

  13. 37

  14. 40

  15. 48

  16. 50

  17. 52

  18. 56

  19. 57

  20. 59

  21. Where to use Groovy • Scripts – especially with groovyserv

    – cross-platform • Tests – syntax more expressive – Spock for BDD • Java application plugins/scripting – GroovyShell – GroovyClassLoader • Domain Specific Languages • Higher-level application development
  22. 68 Scripting with XML content Then hook into other Groovy

    features for processing the content
  23. Spock • BDD testing framework • Encourages simple test structure

    • ...and documented tests • given...when...then • + data-driven tests! • Can run via JUnit test runner 76 http://code.google.com/p/spock/ http://meetspock.appspot.com/
  24. • Grails for Swing applications • MVC model • SwingBuilder

    for views 82 application(title: 'DemoConsole', pack: true, locationByPlatform: true) { panel(border: emptyBorder(6)) { borderLayout() scrollPane(constraints: CENTER) { textArea( text: bind(target: model, targetProperty: 'scriptSource'), enabled: bind {model.enabled}, columns: 40, rows: 10) } } } http://griffon.codehaus.org/
  25. 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 83 http://www.gradle.org/
  26. • Parallel processing for Groovy • Actors library making full

    use of closures 84 @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/
  27. 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) 86
  28. Grails What is Grails? 87 Build Web MVC GSP (Views)

    GORM (Data Access) Doc Engine Servlet Container Test Support Database I18n
  29. What is Grails? 89 Web Controllers The Domain Model Business

    Logic Custom View Tags Views & Layouts Libraries (JARs) Additional Sources Web Resources i18n bundles Build Commands Tests
  30. Build 93 • Remember the Grails project structure? – add

    in build events and... Can’t build natively with other build tools! Grails Build System Ant Gradle Maven
  31. Dependency DSL grails.project.dependency.resolution = { inherits "global" log "warn" repositories

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

    dependencies { runtime "mysql:mysql-connector-java:5.1.17" test "org.gmock:gmock:0.8.1" ... } plugins { compile ":spring-security-core:1.2.7" ... } } 95
  33. ‘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 96
  34. • No existing domain model • Schema not too far

    off the beaten track 97 Option 1: Custom ORM mapping class Book { ... static mapping = { table "books" title type: "books" author column: "author_ref" } }
  35. • Existing Java/JPA domain model Option 2: JPA annotations 98

    grails-app/conf/hibernate/hibernate.cfg.xml <?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>
  36. • You have Java model + Hibernate mapping files •

    Schema is way off the beaten track 99 grails-app/conf/hibernate/hibernate.cfg.xml Option 3: Hibernate XML Mappings <?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>
  37. constraints = { title blank: false, unique: true ... }

    Constraints Given domain class: Then: org.example.myapp.domain.Book src/java/org/example/myapp/domain/BookConstraints.groovy 100
  38. • GORM layer over JPA • Use your own JPA

    provider • Useful for cloud services that only work with JPA, not Hibernate 101 Option 4: GORM JPA Plugin
  39. 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 104
  40. Example import ... beans = { credentialMatcher(Sha1CredentialsMatcher) { storedCredentialsHexEncoded =

    true } sessionFactory(ConfigurableLocalSessionFactoryBean) { dataSource = ref("dataSource") hibernateProperties = [ "hibernate.hbm2ddl.auto": "create-drop", "hibernate.show_sql": true ] } } 105
  41. Enterprise Integration • Spring opens up a world of possibilities

    – Spring Integration/Camel – Messaging (JMS/AMQP) – ESB – RMI, HttpInvoker, etc. • Web services & REST 106
  42. 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 108
  43. 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/ 109
  44. Q&A