Your SlideShare is downloading. ×
0
Modularizing Your                    Grails Application                   with Private Plugins                            ...
About Me                  • Web development with Java since                            JDK 1.0.2 (applets, anyone?)       ...
About IEEE                  • Institute of Electrical and Electronics                            Engineers                ...
About IEEE Spectrum               • Flagship publication of IEEE               • General interest technology magazine     ...
IEEE Spectrum website                  • http://spectrum.ieee.org                  • ~1.5M PV/mo                  • Freque...
Agenda                  • Grails plugins overview                  • Grails plugin development basics                  • A...
To fork or not to fork?                  • “We need a new website...”                  • “You must use the standard corpor...
From “big ball of mud” to                plugin architecture                                  Where do we go from here?   ...
Agenda                  • Grails plugins overview                  • Grails plugin development basics                  • A...
Plugin systems and                             component models                  • Plugin systems are now commonplace     ...
Grails plugins in a                                   nutshell                  • Tightly integrated into Grails system   ...
Grails plugins in a                                 nutshell                  • Central public plugin repository          ...
Grails - plugins inside        • Many parts of Grails               itself are built as plugins        • Some default plug...
Public Grails plugins                  • 850 public plugins in the central                            plugins repository  ...
Selected Grails plugins                    Spring Security - user authentication                    Cache - caching using ...
What can go into a                                  plugin?                  • Really, anything                  • Standar...
Agenda                  • Grails plugins overview                  • Grails plugin development basics                  • A...
Creating a new plugin                    grails create-plugin mypluginWednesday, October 17, 12
Creating a new plugin                   Same structure                   as a Grails                   application, but   ...
Plugin Descriptor             class MypluginGrailsPlugin {               // the plugin version               def version =...
Excluded plugin files                                     /grails-app/conf/BootStrap.groovy                                ...
Running a plugin                  • A plugin is a Grails app!                  • grails run-app from plugin dir           ...
Packaging a plugin                  • grails package-plugin                  • creates a zip file that can be installed    ...
Installing a plugin        from local directory:        grails install-plugin /path/to/grails-example-0.1.zip        from ...
Plugin Development                  • Easy: create Controllers, Services,                            Views, Taglibs, etc. ...
Agenda                  • Grails plugins overview                  • Grails plugin development basics                  • A...
Public vs. Private plugins                              Public plugins             • Highly cohesive             • Maximiz...
Public vs. Private plugins                              Private Plugins              • Proprietary code              • App...
From “big ball of mud” to                plugin architecture                  • Grails applications tend to become        ...
From “big ball of mud” to                plugin architecture                    Spectrum application before refactoring   ...
Reusable domain model                                                         DB        • Reuse a set of               Dom...
Refactored with plugin              Spectrum Application                 Spectrum Core Plugin                    Front End...
Different web sites,                     common backend code                                   spectrum-core              ...
Same Controller,                               different Views                            FrontendArticleController#show()...
Same Controller,                                different Views                  • Define common Controller in plugin      ...
Plugin UrlMappings                  • UrlMappings.groovy excluded from plugin                            build            ...
Config.groovy                  • Config.groovy is excluded from plugin                  • Might want to fix certain config    ...
Extending plugin                                 domain model                               Content class                 ...
Publishing private plugins                  • Publish to internal corporate Maven                            repo - e.g. A...
Declaring private plugin                     dependencies                                 BuildConfig.groovy              ...
Day-to-day development                  • Use “in-place” plugin (exploded plugin) in                            your local...
Refactoring to plugin                  • Determine candidates for refactoring                  • Create new empty plugin &...
Refactoring to plugin                  • Start small - don’t create a whole                            bunch of plugins fr...
Super-project with plugins            One application with several in-place plugins                            /myapplicat...
Public website,                                  private back end                        Public                  Private  ...
Vertical slices / mini                             Grails applications                  • Plugins can be self contained   ...
Plugins as a platform                   Data Capture       Data Cleaning      Data Validation                      plugin ...
Sensitive code protection                                  DB                                                          Spl...
Agenda                  • Grails plugins overview                  • Grails plugin development basics                  • A...
Domain/Schema                                management                  • Changes to domain model need to be             ...
Config.groovy changes                          over time                  • Easy to add a new entry to                     ...
Maven/Ivy/build issues                  • Maven/Ivy hell > JAR hell                  • SNAPSHOT resolution error-prone    ...
Plugin versioning and                             branching                  • Define version numbering strategy           ...
Code Rigidity                  • Inflexibility to change/afferent                            coupling                  • Av...
Continuous Integration                  • Use Jenkins to trigger builds of                            applications that de...
Plugin namespace                                   conflicts                  • Plugin artifacts all share same            ...
Plugin resources                  • Plugin static resources (images, CSS, JS,                            etc.) packaged in...
Component interfaces                  • Grails plugins don’t have any kind of                            external “interfa...
Success!               Release strategy for 2013 website redesign                                     spectrum-core Plugin...
Takeaway                  • Plugins are easy to create and use                  • Private plugins are a safe, robust way  ...
Additional resources                  • @grailsplugins - Grails plugin updates - https://twitter.com/                     ...
Questions?Wednesday, October 17, 12
Thanks!                    feedback is appreciated.                    @kenliu                    k.a.liu@ieee.orgWednesda...
Upcoming SlideShare
Loading in...5
×

Modularizing your Grails Application with Private Plugins - SpringOne 2GX 2012

14,319

Published on

Published in: Technology
0 Comments
23 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
14,319
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
170
Comments
0
Likes
23
Embeds 0
No embeds

No notes for slide

Transcript of "Modularizing your Grails Application with Private Plugins - SpringOne 2GX 2012"

  1. 1. Modularizing Your Grails Application with Private Plugins Ken Liu IEEE SpringOne 2GX 2012 17 Oct 2012Wednesday, October 17, 12
  2. 2. About Me • Web development with Java since JDK 1.0.2 (applets, anyone?) • Groovy/Grails since 2009 • Lead developer for IEEE Spectrum • Always been a closet fan of dynamic languages (starting with Perl)Wednesday, October 17, 12
  3. 3. About IEEE • Institute of Electrical and Electronics Engineers • Widely known for standards - 802.11 • World’s largest technology professional association - 400,000 engineers in 160 countries • Publishes nearly a third of the world’s technical literature in EE/CSWednesday, October 17, 12
  4. 4. About IEEE Spectrum • Flagship publication of IEEE • General interest technology magazine with focus on EE topics • 2012 National Magazine Award winnerWednesday, October 17, 12
  5. 5. IEEE Spectrum website • http://spectrum.ieee.org • ~1.5M PV/mo • Frequent referrals from HN, Reddit, /. • 100% Groovy/Grails since 2009 - both front-end and CMS • Akamai/Weblogic/Oracle stack • Award-winning coverage of ROBOTS!Wednesday, October 17, 12
  6. 6. Agenda • Grails plugins overview • Grails plugin development basics • Application architecture with private plugins • Lessons learnedWednesday, October 17, 12
  7. 7. To fork or not to fork? • “We need a new website...” • “You must use the standard corporate Enterprise CMS system.” • “Can’t you guys just make a copy of the existing code and tweak it?” • DANGER!Wednesday, October 17, 12
  8. 8. From “big ball of mud” to plugin architecture Where do we go from here? Front End Domain classes CMS Controllers/Views/ (e.g Article, Controllers/ Services BlogPost) Views/Services (e.g. article, blog pages) Utility classes UrlMappings Spectrum Taglib (src/**) Quartz Jobs Front End & CMS (e.g. sitemap Test classes images, CSS, JS generation)Wednesday, October 17, 12
  9. 9. Agenda • Grails plugins overview • Grails plugin development basics • Application architecture with private plugins • Lessons learnedWednesday, October 17, 12
  10. 10. Plugin systems and component models • Plugin systems are now commonplace • Firefox, Eclipse, Jenkins, JEdit • Package managers, OSGi, Ruby on Rails Engines • Component-oriented software? • Coarse-grained high-level reuse, separation of concernsWednesday, October 17, 12
  11. 11. Grails plugins in a nutshell • Tightly integrated into Grails system • Can implement common functionality in plugins that is overridden by application • Can create new Artifact types • Interact with Spring context • Integrated with build systemWednesday, October 17, 12
  12. 12. Grails plugins in a nutshell • Central public plugin repository • Version management • Dependency managementWednesday, October 17, 12
  13. 13. Grails - plugins inside • Many parts of Grails itself are built as plugins • Some default plugins can even be swapped out • Tomcat -> Jetty • GORM/Hibernate -> Redis GORMWednesday, October 17, 12
  14. 14. Public Grails plugins • 850 public plugins in the central plugins repository • Search on plugin portal http://grails.org/plugins/ • Quickly bootstrap new applications • Spend some time now save some time later • Follow @grailsplugins for updatesWednesday, October 17, 12
  15. 15. Selected Grails plugins Spring Security - user authentication Cache - caching using annotations Quartz - job scheduling (like cron) DB Migration - controlled schema changes Mail - send email with JavaMail Resources - web performance optimization Console - in-app interactive console CodeNarc - static code analysis Spock - enable Spock unit testing frameworkWednesday, October 17, 12
  16. 16. What can go into a plugin? • Really, anything • Standard Grails artifacts: GSPs, Controllers, Services, i18n, TagLibs - just like in app • Define custom Grails artifact types: see Quartz plugin “Jobs” • Plugins can depend on other pluginsWednesday, October 17, 12
  17. 17. Agenda • Grails plugins overview • Grails plugin development basics • Application architecture with private plugins • Lessons learnedWednesday, October 17, 12
  18. 18. Creating a new plugin grails create-plugin mypluginWednesday, October 17, 12
  19. 19. Creating a new plugin Same structure as a Grails application, but with an extra plugin descriptor fileWednesday, October 17, 12
  20. 20. Plugin Descriptor class MypluginGrailsPlugin { // the plugin version def version = "0.1" // the version or versions of Grails the plugin is designed for def grailsVersion = "2.1 > *" // the other plugins this plugin depends on def dependsOn = [:] def doWithDynamicMethods = { ctx -> // TODO Implement registering dynamic methods to classes (optional) } def doWithApplicationContext = { applicationContext -> // TODO Implement post initialization spring config (optional) }Wednesday, October 17, 12
  21. 21. Excluded plugin files /grails-app/conf/BootStrap.groovy /grails-app/conf/BuildConfig.groovy /grails-app/conf/Config.groovy Grails excludes /grails-app/conf/DataSource.groovy certain files (and any other *DataSource.groovy) /grails-app/conf/UrlMappings.groovy from packaging /grails-app/conf/spring/resources.groovy /web-app/WEB-INF/** /web-app/plugins/** /test/**Wednesday, October 17, 12
  22. 22. Running a plugin • A plugin is a Grails app! • grails run-app from plugin dir • excluded files loaded during run-app • useful for testing (test-app works too)Wednesday, October 17, 12
  23. 23. Packaging a plugin • grails package-plugin • creates a zip file that can be installed & distributedWednesday, October 17, 12
  24. 24. Installing a plugin from local directory: grails install-plugin /path/to/grails-example-0.1.zip from URL: grails install-plugin http://myserver.com/plugins/grails- example-0.1.zip in BuildConfig.groovy (“in-place” plugin): // Uncomment this only for local development - do not check in! grails.plugin.location.spectrum-core = ../spectrum-coreWednesday, October 17, 12
  25. 25. Plugin Development • Easy: create Controllers, Services, Views, Taglibs, etc. • Specify resource paths <g:resource> • Can alter Spring context • Hook into build system, reload eventsWednesday, October 17, 12
  26. 26. Agenda • Grails plugins overview • Grails plugin development basics • Application architecture with private plugins • Lessons learnedWednesday, October 17, 12
  27. 27. Public vs. Private plugins Public plugins • Highly cohesive • Maximize reuse • Sometimes provide “horizontal slices” - e.g. Spring security core • Most don’t depend on other pluginsWednesday, October 17, 12
  28. 28. Public vs. Private plugins Private Plugins • Proprietary code • Application or domain-specific • Code reuse within an organization • Can be coarse-grained, heavyweight • Private forks of public plugins • Enable interesting options for application architectureWednesday, October 17, 12
  29. 29. From “big ball of mud” to plugin architecture • Grails applications tend to become unmanageable over time • Convention-based directory trees (e.g. /grails-app/views ) • Rapid development/scaffolding • Need “separation of concerns”Wednesday, October 17, 12
  30. 30. From “big ball of mud” to plugin architecture Spectrum application before refactoring Front End Domain classes CMS Controllers/Views/ (e.g Article, Controllers/ Services BlogPost) Views/Services (e.g. article, blog pages) Utility classes UrlMappings Spectrum Taglib (src/**) Quartz Jobs Front End & CMS (e.g. sitemap Test classes images, CSS, JS generation)Wednesday, October 17, 12
  31. 31. Reusable domain model DB • Reuse a set of Domain classes between applications • Different applications, same DB instance Domain Model Domain Model • Hibernate not plugin plugin included in plugins by API endpoint Primary web application application default • Consider Hibernate Domain Model plugin caching issues Reporting applicationWednesday, October 17, 12
  32. 32. Refactored with plugin Spectrum Application Spectrum Core Plugin Front End Core Domain Utility classes Controllers/Views classes (src/**) Front End CMS Common Front UrlMappings Controllers/ End Controllers Views/Services Front End Taglib Core Services CMS test classes Test classes CoreUrlMappings CMS Taglib Front End CMS Quartz Jobs images, CSS, JS images, CSS, JS Depends on other plugins: spring-security-core, quartz, searchable, ckeditor, and othersWednesday, October 17, 12
  33. 33. Different web sites, common backend code spectrum-core Plugin Spectrum The Institute website website 10.4+ 3.0Wednesday, October 17, 12
  34. 34. Same Controller, different Views FrontendArticleController#show() Spectrum application The Institute application show.gsp show.gspWednesday, October 17, 12
  35. 35. Same Controller, different Views • Define common Controller in plugin • View resolution: application first, then plugin • Can specify plugin using <g:render plugin=”myplugin”> • The promise of MVC fulfilled!Wednesday, October 17, 12
  36. 36. Plugin UrlMappings • UrlMappings.groovy excluded from plugin build • *UrlMappings.groovy merged into application mappings • Application can override plugin mappings • Enables “mini application” in plugin • Can’t remove unwanted mappings, only overrideWednesday, October 17, 12
  37. 37. Config.groovy • Config.groovy is excluded from plugin • Might want to fix certain config properties - won’t need them in application Config • Manually merge config in doWithSpring • application.config.merge(new ConfigSlurper().parse(application.classLoader.loadClass(MyPluginConfig )))Wednesday, October 17, 12
  38. 38. Extending plugin domain model Content class Domain model plugin Domain classes in plugins can be extended in application Webinar extends Content ApplicationWednesday, October 17, 12
  39. 39. Publishing private plugins • Publish to internal corporate Maven repo - e.g. Artifactory or Nexus • Release plugin - installed by default • Old-style svn-based repos supported, but not recommended for Grails 2 • Customize plugin metadata with internal Maven groupId • Publish from JenkinsWednesday, October 17, 12
  40. 40. Declaring private plugin dependencies BuildConfig.groovy (Maven/Ivy dependencies) repositories { grailsPlugins() /* other default repos here */ /* point to internal corporate Maven repo */ mavenRepo http://mvn.ieee.org/nexus/content/repositories/spectrum/ } plugins { /* other plugin dependencies here */ compile org.ieee.spectrum.plugins:spectrum-core:2.0-SNAPSHOT }Wednesday, October 17, 12
  41. 41. Day-to-day development • Use “in-place” plugin (exploded plugin) in your local dev workspace (BuildConfig.groovy) • Private plugins tend to be updated along with application features • Beware of potential breaking changes (in other applications) • Define deprecation strategy and use @deprecated javadocWednesday, October 17, 12
  42. 42. Refactoring to plugin • Determine candidates for refactoring • Create new empty plugin & plugin descriptor • Move Grails artifacts (e.g. Controllers, Services, Views, etc.) into new plugin • Move dependent files from /src • Move related test files into new plugin • Remove hard coded paths to resources • Run your functional test suiteWednesday, October 17, 12
  43. 43. Refactoring to plugin • Start small - don’t create a whole bunch of plugins from the outset • Consider migrating version historyWednesday, October 17, 12
  44. 44. Super-project with plugins One application with several in-place plugins /myapplication /grails-app ... /pluginA /grails-app /pluginB /pluginC In BuildConfig.groovy: grails.plugin.location.pluginA = pluginA // path to plugin grails.plugin.location.pluginB = pluginB grails.plugin.location.pluginC = pluginCWednesday, October 17, 12
  45. 45. Public website, private back end Public Private Internet intranet Deploy public Core Core application application plugin and private plugin Business- versions of Public-facing application facing application with application shared code DBWednesday, October 17, 12
  46. 46. Vertical slices / mini Grails applications • Plugins can be self contained • UrlMappings + Controllers + Domains + Views + Services = Grails application • Remove views -> “white label” application • Embed a small application within another application • Vertically slice your application into pluginsWednesday, October 17, 12
  47. 47. Plugins as a platform Data Capture Data Cleaning Data Validation plugin plugin plugin Super-plugin to combine plugin Clinical Data Management dependencies into Platform plugin a platform or domain-specific framework Clinical data Clinical data review management server application applicationWednesday, October 17, 12
  48. 48. Sensitive code protection DB Split application into separate Data Ingestion Data Analysis parts to isolate plugin plugin “secret sauce” key algorithms from external vendor Shell application Full application (external team)Wednesday, October 17, 12
  49. 49. Agenda • Grails plugins overview • Grails plugin development basics • Application architecture with private plugins • Lessons learnedWednesday, October 17, 12
  50. 50. Domain/Schema management • Changes to domain model need to be carefully managed • Avoid dbCreate=update • Adopt DDL file naming convention: spectrum-core-2.1.sql spectrum-core-2.2.sql • Bootstrap-triggered migrations • DB Migrations plugin (Liquibase)Wednesday, October 17, 12
  51. 51. Config.groovy changes over time • Easy to add a new entry to Config.groovy and then forget about it • Group together plugin config properties into a closure • Don’t forget about external config files and differences in various environmentsWednesday, October 17, 12
  52. 52. Maven/Ivy/build issues • Maven/Ivy hell > JAR hell • SNAPSHOT resolution error-prone • See JIRA for bugs related to plugins • Avoid creating many small plugins • Strange problems? Look at your build.Wednesday, October 17, 12
  53. 53. Plugin versioning and branching • Define version numbering strategy • Be prepared to branch/merge • Be sure to update plugin versions and tag your code consistently • Just use Git - merging & cherry- picking are sweetWednesday, October 17, 12
  54. 54. Code Rigidity • Inflexibility to change/afferent coupling • Avoid breaking changes (duh) • Solution: automate your tests, catch breaking changes earlyWednesday, October 17, 12
  55. 55. Continuous Integration • Use Jenkins to trigger builds of applications that depend on your private plugin • Run Plugin test suite with plugin build • Publish snapshots to Maven repo from JenkinsWednesday, October 17, 12
  56. 56. Plugin namespace conflicts • Plugin artifacts all share same namespace with each other • Potential for class name collisions, especially with “conventions” • Planned enhancement for Grails 2.2Wednesday, October 17, 12
  57. 57. Plugin resources • Plugin static resources (images, CSS, JS, etc.) packaged in a path corresponding to plugin name & version - /plugins/ myplugin-1.0/* • path changes between dev mode and WAR - <g:resource> handles correctly • May need to redeploy with each plugin version bump • Don’t accidentally share sensitive files!Wednesday, October 17, 12
  58. 58. Component interfaces • Grails plugins don’t have any kind of external “interface” or contract besides Plugin Metadata • Up to you to make the rules (not a bad thing)Wednesday, October 17, 12
  59. 59. Success! Release strategy for 2013 website redesign spectrum-core Plugin 2.0, 2.1, 2.2 The Institute Spectrum Spectrum website website website 3.x 10.8, 10.9, 4.0 - 2013 10.10 releaseWednesday, October 17, 12
  60. 60. Takeaway • Plugins are easy to create and use • Private plugins are a safe, robust way to modularize Grails applications • Private plugins enable coarse-grained reuse of application components • Configuration management and OO principles are important (as always)Wednesday, October 17, 12
  61. 61. Additional resources • @grailsplugins - Grails plugin updates - https://twitter.com/ grailsplugins • Grails reference docs chapter on plugins - http://grails.org/doc/ latest/guide/plugins.html • Inside Grails: The Build System and Plugin Management - http:// www.slideshare.net/skillsmatter/grails-internals-ggug-dec-2009 • GGUG: Grails Plugins - Lessons to Learn http://skillsmatter.com/ podcast/java-jee/grails-pluginsWednesday, October 17, 12
  62. 62. Questions?Wednesday, October 17, 12
  63. 63. Thanks! feedback is appreciated. @kenliu k.a.liu@ieee.orgWednesday, October 17, 12
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×