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. 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)
  2. 8

  3. 10

  4. 15

  5. 16

  6. 20

  7. 24

  8. 26

  9. 30

  10. 33

  11. 35

  12. 37

  13. 40

  14. 48

  15. 50

  16. 52

  17. 56

  18. 57

  19. 59

  20. 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
  21. 68 Scripting with XML content Then hook into other Groovy

    features for processing the content
  22. 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/
  23. • 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/
  24. 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/
  25. • 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/
  26. 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
  27. Grails What is Grails? 87 Build Web MVC GSP (Views)

    GORM (Data Access) Doc Engine Servlet Container Test Support Database I18n
  28. 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
  29. 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
  30. Dependency DSL grails.project.dependency.resolution = { inherits "global" log "warn" repositories

    { grailsHome() mavenCentral() mavenRepo "http://localhost:8081/..." } ... } 94
  31. 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
  32. ‘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
  33. • 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" } }
  34. • 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>
  35. • 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>
  36. 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
  37. • 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
  38. 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
  39. Example import ... beans = { credentialMatcher(Sha1CredentialsMatcher) { storedCredentialsHexEncoded =

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

    – Spring Integration/Camel – Messaging (JMS/AMQP) – ESB – RMI, HttpInvoker, etc. • Web services & REST 106
  41. 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
  42. More info • w: http://grails.org/ • f: http://grails.org/Mailing+Lists • e:

    [email protected] • t: pledbrook • b: http://blog.springsource.com/author/peter-ledbrook/ 109
  43. Q&A