jugl 2009 - introduction groovy/grails

45
Introduction Groovy / Grails JUG Lausanne Jeudi 10 décembre 2009

Upload: cyrilpicat

Post on 30-Jun-2015

184 views

Category:

Technology


1 download

DESCRIPTION

Introduction to Groovy/Grails held at the Java User Group Lausanne in 2009

TRANSCRIPT

Page 1: JUGL 2009 - Introduction Groovy/Grails

Introduction Groovy / Grails

JUG Lausanne Jeudi 10 décembre 2009

Page 2: JUGL 2009 - Introduction Groovy/Grails

Copyright © 2009 – OCTO Technology – Licence CC – Creative Commons 2.0 France – Paternité – Pas d'Utilisation Commerciale – Partage des Conditions Initiales à l'Identique 2

Présentateur

§  Cyril PICAT §  Architecte chez OCTO Technology §  [email protected] §  http://www.octo.com

Page 3: JUGL 2009 - Introduction Groovy/Grails

Copyright © 2009 – OCTO Technology – Licence CC – Creative Commons 2.0 France – Paternité – Pas d'Utilisation Commerciale – Partage des Conditions Initiales à l'Identique 3

Agenda

§  L’éco-système Groovy (GR3)

§  Présentation de Groovy

§  Présentation de Grails

§  Synthèse : Groovy / Grails pour ?

Page 4: JUGL 2009 - Introduction Groovy/Grails

Copyright © 2009 – OCTO Technology – Licence CC – Creative Commons 2.0 France – Paternité – Pas d'Utilisation Commerciale – Partage des Conditions Initiales à l'Identique

L’ECO-SYSTÈME GROOVY (GR3)

4

Page 5: JUGL 2009 - Introduction Groovy/Grails

Copyright © 2009 – OCTO Technology – Licence CC – Creative Commons 2.0 France – Paternité – Pas d'Utilisation Commerciale – Partage des Conditions Initiales à l'Identique

Qu’est-ce que Groovy ?

§  Groovy est un langage dynamique pour la machine virtuelle Java §  « Interprété » ou compilé §  Génère du bytecode

§  Grammaire dérivée du langage Java §  Et inspiré d’autres langages : Python, Ruby, SmallTalk

§  Son but est de simplifier la vie des développeurs

5

Page 6: JUGL 2009 - Introduction Groovy/Grails

Copyright © 2009 – OCTO Technology – Licence CC – Creative Commons 2.0 France – Paternité – Pas d'Utilisation Commerciale – Partage des Conditions Initiales à l'Identique

Qu’est-ce que Grails ?

§  Grails est un framework de développement d’applications Web §  Inspiré par Ruby on Rails, Django, Turbo Gears

§  Pour la plateforme Java §  Basé sur Groovy §  S’appuie sur des briques Open Source reconnues (Spring,

Hibernate etc…)

§  Son but est de simplifier le développement tout en permettant la ré-utilisation de l’existant

6

Page 7: JUGL 2009 - Introduction Groovy/Grails

Copyright © 2009 – OCTO Technology – Licence CC – Creative Commons 2.0 France – Paternité – Pas d'Utilisation Commerciale – Partage des Conditions Initiales à l'Identique

Un peu d’histoire…

§  Groovy §  Lancé en 2003 §  Version 1.0 en janvier 2007 §  Branche stable : 1.6.7 datée de décembre 2009 §  JSR-223 en 2006 : langages de script sur la JVM

§  Grails §  Lancé en 2005 §  Version 1.0 en février 2008 §  Branche stable : 1.1.2 datée de novembre 2009

7

Page 8: JUGL 2009 - Introduction Groovy/Grails

Copyright © 2009 – OCTO Technology – Licence CC – Creative Commons 2.0 France – Paternité – Pas d'Utilisation Commerciale – Partage des Conditions Initiales à l'Identique

Eco-système

§  Griffon §  Framework MVC pour construire des applications Swing –

similaire à Grails pour le RDA

§  Gradle §  Nouveau système de build inspiré de Ant, Maven et Ivy

§  Easyb §  Behaviour Driven Development (BDD). Utilise du langage

naturel pour décrire les tests et donc documenter le code

8

Page 9: JUGL 2009 - Introduction Groovy/Grails

Copyright © 2009 – OCTO Technology – Licence CC – Creative Commons 2.0 France – Paternité – Pas d'Utilisation Commerciale – Partage des Conditions Initiales à l'Identique

PRÉSENTATION DE GROOVY

9

Page 10: JUGL 2009 - Introduction Groovy/Grails

Copyright © 2009 – OCTO Technology – Licence CC – Creative Commons 2.0 France – Paternité – Pas d'Utilisation Commerciale – Partage des Conditions Initiales à l'Identique

Langage

§  Dérivé de Java §  À quelques exceptions près… §  Un programme Java compile en Groovy

§  Surcouche à Java §  En terme de syntaxe §  En terme d’API

§  Apports §  Simplification du code §  Expressivité, lisibilité du code §  Simplification de l’écriture de scripts

10

Page 11: JUGL 2009 - Introduction Groovy/Grails

Copyright © 2009 – OCTO Technology – Licence CC – Creative Commons 2.0 France – Paternité – Pas d'Utilisation Commerciale – Partage des Conditions Initiales à l'Identique

Ex. d’utilisation - DSL

§  DSL = Domain Specific Language

§  Grails fait un usage intensif de ces fonctionnalités

§  ex. GORM, l’API de persistence de Grails

11

Page 12: JUGL 2009 - Introduction Groovy/Grails

Copyright © 2009 – OCTO Technology – Licence CC – Creative Commons 2.0 France – Paternité – Pas d'Utilisation Commerciale – Partage des Conditions Initiales à l'Identique

Outils

§  Shell et console: groovysh and groovyConsole §  Tâche Ant, plugin Maven (GMaven) §  Un compilateur joint groovyc

§  Compile Groovy et Java simultanémenent

§  Support dans les IDE

12

Page 13: JUGL 2009 - Introduction Groovy/Grails

Copyright © 2009 – OCTO Technology – Licence CC – Creative Commons 2.0 France – Paternité – Pas d'Utilisation Commerciale – Partage des Conditions Initiales à l'Identique

DEMO: HELLOWORLD EN GROOVY

13

Page 14: JUGL 2009 - Introduction Groovy/Grails

Copyright © 2009 – OCTO Technology – Licence CC – Creative Commons 2.0 France – Paternité – Pas d'Utilisation Commerciale – Partage des Conditions Initiales à l'Identique

Intéropérabilité avec Java

§  Groovy génère des .class - bytecode Java §  Mêmes chaînes de caractères et expressions régulières §  Mêmes APIs — JDK, collections, 3rd party §  Même modèle de sécurité, même threading §  Même modèle de programmation Orienté Objet §  Compilation “jointe”

14

Page 15: JUGL 2009 - Introduction Groovy/Grails

Copyright © 2009 – OCTO Technology – Licence CC – Creative Commons 2.0 France – Paternité – Pas d'Utilisation Commerciale – Partage des Conditions Initiales à l'Identique

Listes, maps, ranges, regex...

§  Listes § 

§  Maps — dictionnaires

§  Ranges

§  Regex

15

def list = ["Groovy", "Grails"] println list[0] list[2] = "Griffon"

def states = [CA: "California", TX: "Texas"] println states["CA"]; println states.CA States["NY"] = "New York"

def range = 1..10; def range = 'a'..'d' for (i in 1..10) { … }

(myString =~ /Hello/).each { … }

Page 16: JUGL 2009 - Introduction Groovy/Grails

Copyright © 2009 – OCTO Technology – Licence CC – Creative Commons 2.0 France – Paternité – Pas d'Utilisation Commerciale – Partage des Conditions Initiales à l'Identique

GDK

§  GDK = Groovy Development Kit §  Groovy « décore » le JDK existant

§  étend Object §  étend des classes final comme java.lang.String ou java.io.File

§  Exemples :

16

new File(“f.txt”).eachLine { println it }

(1..100).findAll { it % 2 == 1 }

speakers.groupBy { it.lastname }

“123”.padLeft(5, ‘0’)

Thread.start { /* code to be executed */ }

Page 17: JUGL 2009 - Introduction Groovy/Grails

Copyright © 2009 – OCTO Technology – Licence CC – Creative Commons 2.0 France – Paternité – Pas d'Utilisation Commerciale – Partage des Conditions Initiales à l'Identique

DEMO: REST EN GROOVY

17

Page 18: JUGL 2009 - Introduction Groovy/Grails

Copyright © 2009 – OCTO Technology – Licence CC – Creative Commons 2.0 France – Paternité – Pas d'Utilisation Commerciale – Partage des Conditions Initiales à l'Identique

Et bien plus...

§  Surcharge d’opérateurs §  Multi-méthodes §  API pour SQL, Ant, XML, templates, Swing, JMX, WS §  Groovy Truth §  GPath avancé §  Groovy Builders §  Paramètres nommés §  Meta-programming statique (AST) et dynamique (MOP) §  Autres mécanismes d’intégration avec Spring, Java etc…

18

Page 19: JUGL 2009 - Introduction Groovy/Grails

Copyright © 2009 – OCTO Technology – Licence CC – Creative Commons 2.0 France – Paternité – Pas d'Utilisation Commerciale – Partage des Conditions Initiales à l'Identique

PRÉSENTATION DE GRAILS

19

Page 20: JUGL 2009 - Introduction Groovy/Grails

Copyright © 2009 – OCTO Technology – Licence CC – Creative Commons 2.0 France – Paternité – Pas d'Utilisation Commerciale – Partage des Conditions Initiales à l'Identique

Vision très haut-niveau

§  Grails est un framework MVC action-based

§  Grails utilise les principes rendus populaires par Rails §  CoC: Convention over Configuration §  DRY: Don’t Repeat Yourself §  Scaffolding

§  Productivité et simplicité de développement Web §  Tout en protégeant votre investissement sur la plateforme Java

20

Page 21: JUGL 2009 - Introduction Groovy/Grails

Copyright © 2009 – OCTO Technology – Licence CC – Creative Commons 2.0 France – Paternité – Pas d'Utilisation Commerciale – Partage des Conditions Initiales à l'Identique

Vision haut-niveau

21

Page 22: JUGL 2009 - Introduction Groovy/Grails

Copyright © 2009 – OCTO Technology – Licence CC – Creative Commons 2.0 France – Paternité – Pas d'Utilisation Commerciale – Partage des Conditions Initiales à l'Identique

Vision rapprochée

§  Grails est bâti sur des technologies éprouvées §  la JVM, les specs Java EE, les serveurs d’app §  Spring : IoC, DI, Spring MVC, Spring WebFlow §  Hibernate : Object-Relational Mapping §  SiteMesh : structure de page et composition §  Quartz : séquencement de tâches §  Jetty and HSQLDB : pour des cycles de développement rapide

§  Grails est fourni comme un ensemble intégré et est productif immédiatemment

22

Page 23: JUGL 2009 - Introduction Groovy/Grails

Copyright © 2009 – OCTO Technology – Licence CC – Creative Commons 2.0 France – Paternité – Pas d'Utilisation Commerciale – Partage des Conditions Initiales à l'Identique

DEMO: CRÉATION D’UNE APPLICATION GRAILS VIDE

23

Page 24: JUGL 2009 - Introduction Groovy/Grails

Copyright © 2009 – OCTO Technology – Licence CC – Creative Commons 2.0 France – Paternité – Pas d'Utilisation Commerciale – Partage des Conditions Initiales à l'Identique

Arborescence et nommage

24

Page 25: JUGL 2009 - Introduction Groovy/Grails

Copyright © 2009 – OCTO Technology – Licence CC – Creative Commons 2.0 France – Paternité – Pas d'Utilisation Commerciale – Partage des Conditions Initiales à l'Identique

Outils

§  Les mêmes outils que pour Groovy §  Sauf pour Eclipse, pas de support direct de Grails

§  Support de Grails dans Eclipse via SpringSource Tools Suite (STS)

25

Page 26: JUGL 2009 - Introduction Groovy/Grails

Copyright © 2009 – OCTO Technology – Licence CC – Creative Commons 2.0 France – Paternité – Pas d'Utilisation Commerciale – Partage des Conditions Initiales à l'Identique

DEMO: DOMAINE / CONTROLLEUR / VUE / SERVICE

26

Page 27: JUGL 2009 - Introduction Groovy/Grails

Copyright © 2009 – OCTO Technology – Licence CC – Creative Commons 2.0 France – Paternité – Pas d'Utilisation Commerciale – Partage des Conditions Initiales à l'Identique

Avez-vous remarqué ?

§  Où sont les fichiers de configuration ? §  Pas besoin d’écrire du XML ? §  Où sont mes DAOs ? §  Où sont mes fichiers de mapping ? §  Pas besoin de configurer une base de

données ? §  Pas besoin d’installer et de configurer un

serveur d’app ? §  Pas besoin de télécharger des tonnes de

jar depuis des repositories Maven ? => Il y a de la magie dans l’air :-)

27

Page 28: JUGL 2009 - Introduction Groovy/Grails

Copyright © 2009 – OCTO Technology – Licence CC – Creative Commons 2.0 France – Paternité – Pas d'Utilisation Commerciale – Partage des Conditions Initiales à l'Identique

Synthèse demo

§  Explication des conventions §  Domaine §  Controlleur / Actions §  Vue §  Service

§  CRUD en Grails

28

Page 29: JUGL 2009 - Introduction Groovy/Grails

Copyright © 2009 – OCTO Technology – Licence CC – Creative Commons 2.0 France – Paternité – Pas d'Utilisation Commerciale – Partage des Conditions Initiales à l'Identique

DEMO: WEB LAYER

29

Page 30: JUGL 2009 - Introduction Groovy/Grails

Copyright © 2009 – OCTO Technology – Licence CC – Creative Commons 2.0 France – Paternité – Pas d'Utilisation Commerciale – Partage des Conditions Initiales à l'Identique

Synthèse demo

§  GSP §  Support de la syntaxe JSP/ASP <% %> §  GSP tags : variable, logique, search, filter, links, forms §  Tags

•  fonctions groovy standard utilisables en <g:xx> ou ${g.xx} §  Création de librairies de Tag

§  Templates

§  Layout de site avec SiteMesh

30

Page 31: JUGL 2009 - Introduction Groovy/Grails

Copyright © 2009 – OCTO Technology – Licence CC – Creative Commons 2.0 France – Paternité – Pas d'Utilisation Commerciale – Partage des Conditions Initiales à l'Identique

DEMO: DOMAINE - GORM

31

Page 32: JUGL 2009 - Introduction Groovy/Grails

Copyright © 2009 – OCTO Technology – Licence CC – Creative Commons 2.0 France – Paternité – Pas d'Utilisation Commerciale – Partage des Conditions Initiales à l'Identique

Synthèse demo

§  DSL GORM (Grails ORM) §  Contraintes / Validation §  Association / Composition / Héritage

§  Classes du domaine décorées §  save / delete §  count / get / getAll / list

•  pagination & sorting §  dynamic finders : findBy / findAllBy

•  pagination & sorting

32

Page 33: JUGL 2009 - Introduction Groovy/Grails

Copyright © 2009 – OCTO Technology – Licence CC – Creative Commons 2.0 France – Paternité – Pas d'Utilisation Commerciale – Partage des Conditions Initiales à l'Identique

DEMO: CONTROLLER & BINDINGS

33

Page 34: JUGL 2009 - Introduction Groovy/Grails

Copyright © 2009 – OCTO Technology – Licence CC – Creative Commons 2.0 France – Paternité – Pas d'Utilisation Commerciale – Partage des Conditions Initiales à l'Identique

Synthèse demo

§  GORM §  Controller

§  Bindings : simple / multiple / association

§  URL Mappings

34

Page 35: JUGL 2009 - Introduction Groovy/Grails

Copyright © 2009 – OCTO Technology – Licence CC – Creative Commons 2.0 France – Paternité – Pas d'Utilisation Commerciale – Partage des Conditions Initiales à l'Identique

Plugins

§  Grails est bâti sur un système de plugins évolué

§  Le coeur même de Grails est constitué de plugins §  Hibernate plugin §  Tomcat plugin

§  Les plugins rajoutent de la fonctionnalité §  Domaine, service, controlleur, tag libs et même conventions !

35

Page 36: JUGL 2009 - Introduction Groovy/Grails

Copyright © 2009 – OCTO Technology – Licence CC – Creative Commons 2.0 France – Paternité – Pas d'Utilisation Commerciale – Partage des Conditions Initiales à l'Identique

DEMO: GOOGLE CHART PLUGIN

36

Page 37: JUGL 2009 - Introduction Groovy/Grails

Copyright © 2009 – OCTO Technology – Licence CC – Creative Commons 2.0 France – Paternité – Pas d'Utilisation Commerciale – Partage des Conditions Initiales à l'Identique

Plugins disponibles

§  100+ plugins disponibles

§  Build : Maven §  Tests : Webtest, Easyb, Selenium, jsUnit, Fitnesse… §  Rich client / AJAX : Yahoo, Ext-JS, GWT, jQuery, iUI… §  Web Services : XFire, remoting, Axis2, Metro… §  Sécurité : Spring Security, Jsecurity… §  Search : Compass integration…

37

Page 38: JUGL 2009 - Introduction Groovy/Grails

Copyright © 2009 – OCTO Technology – Licence CC – Creative Commons 2.0 France – Paternité – Pas d'Utilisation Commerciale – Partage des Conditions Initiales à l'Identique

Déploiement

§  Environnements §  Par défaut développement / test / production §  Possibilité de créer ses propres environnements

§  Déploiement §  Commandes ‘grails run-app/run-war/war’

§  Serveurs d’application supportés §  Tomcat, Jetty, GlassFish, JBoss, Weblogic etc…

38

Page 39: JUGL 2009 - Introduction Groovy/Grails

Copyright © 2009 – OCTO Technology – Licence CC – Creative Commons 2.0 France – Paternité – Pas d'Utilisation Commerciale – Partage des Conditions Initiales à l'Identique

Ce que l’on a pas vu…

§  Advanced Query : Hibernate HQL, Criteria §  Spring Web Flow §  Internationalisation §  Architectures type REST §  Tests en Grails §  GORM avancé

§  eager/lazy fetching, pessimistic/optimistic locking §  utilisation de schémas existants, de mapping Hibernate ou de

configuration Spring existantes

§  Filtres / intercepteurs

39

Page 40: JUGL 2009 - Introduction Groovy/Grails

Copyright © 2009 – OCTO Technology – Licence CC – Creative Commons 2.0 France – Paternité – Pas d'Utilisation Commerciale – Partage des Conditions Initiales à l'Identique

SYNTHÈSE : GROOVY / GRAILS POUR ?

40

Page 41: JUGL 2009 - Introduction Groovy/Grails

Copyright © 2009 – OCTO Technology – Licence CC – Creative Commons 2.0 France – Paternité – Pas d'Utilisation Commerciale – Partage des Conditions Initiales à l'Identique

Synthèse

§  Parfait pour développer rapidement des applications innovantes

§  Adapté à un développement Agile des applications §  Permet de montrer très rapidement des écrans fonctionnels

§  Également adapté aux développement de nouvelles fonctionnalités en s’interfaçant avec du legacy §  Architecture type REST

§  Permet de profiter de toutes ces nouveautés toute de suite §  Pas d’impact en production

§  Permet une transition facile de la zone d’innovation vers la zone rationalisée §  Si besoin

41

Page 42: JUGL 2009 - Introduction Groovy/Grails

Copyright © 2009 – OCTO Technology – Licence CC – Creative Commons 2.0 France – Paternité – Pas d'Utilisation Commerciale – Partage des Conditions Initiales à l'Identique

Bibliographie

§  Livres §  Groovy

•  Groovy in Action §  Grails

•  The Definitive Guide to Grails, 2nd edition •  Grails in Action

§  Ressources : §  Groovy User Guide : http://groovy.codehaus.org/User+Guide §  Groovy GDK : http://groovy.codehaus.org/groovy-jdk §  Grails User Guide : http://www.grails.org/doc/latest

42

Page 43: JUGL 2009 - Introduction Groovy/Grails

Copyright © 2009 – OCTO Technology – Licence CC – Creative Commons 2.0 France – Paternité – Pas d'Utilisation Commerciale – Partage des Conditions Initiales à l'Identique

Sources

§  « Introduction à Groovy », Guillaume Laforge, Paris JUG, septembre 2008

§  « Groovy & Grails in Action! », Guillaume Laforge, Devoxx, 2008

43

Page 44: JUGL 2009 - Introduction Groovy/Grails

Copyright © 2009 – OCTO Technology – Licence CC – Creative Commons 2.0 France – Paternité – Pas d'Utilisation Commerciale – Partage des Conditions Initiales à l'Identique 44

Licence

Creative Commons Contrat Paternité

Pas d'Utilisation Commerciale Partage des Conditions Initiales à l'Identique

2.0 France

http://creativecommons.org/licenses/by-nc-sa/2.0/fr/

Page 45: JUGL 2009 - Introduction Groovy/Grails

Copyright © 2009 – OCTO Technology – Licence CC – Creative Commons 2.0 France – Paternité – Pas d'Utilisation Commerciale – Partage des Conditions Initiales à l'Identique 45

Q&A