{"id":333,"date":"2009-03-18T20:13:19","date_gmt":"2009-03-18T23:13:19","guid":{"rendered":"https:\/\/devkico.itexto.com.br\/?p=333"},"modified":"2009-03-29T11:57:55","modified_gmt":"2009-03-29T14:57:55","slug":"grails-and-legacy-code-making-old-code-groovy-again","status":"publish","type":"post","link":"https:\/\/devkico.itexto.com.br\/?p=333","title":{"rendered":"Grails and legacy code: making old code groovy again!"},"content":{"rendered":"<p><a href=\"https:\/\/devkico.itexto.com.br\/wp-content\/uploads\/2009\/03\/grails-logo.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-301\" style=\"margin: 10px;\" title=\"grails-logo\" src=\"https:\/\/devkico.itexto.com.br\/wp-content\/uploads\/2009\/03\/grails-logo.png\" alt=\"\" width=\"285\" height=\"148\" \/><\/a><\/p>\n<p>Code reuse with Grails is amazingly easy. When working with this framework, it&#8217;s really cool to see the huge improvement in productivity that we can achieve. But if you have a large legacy code behind you, this productivity will simply disappear. And this is where Grails really shines and become a real choice for a Java developer.<\/p>\n<p>This week I needed to start a new project <span class=\"__mozilla-findbar-search\" style=\"padding: 0pt; background-color: yellow; color: black; display: inline; font-size: inherit;\">which<\/span> in reality was just a web interface for one of our legacy projects. So, I had some options in front of me: I could start with Struts, JSF or even plain servlets (I was out of my mind to just cogitate this third option!). At\u00a0 first, I choosed JSF to interact with our legacy code. After all, it was plain Java to deal with Java code, right?<\/p>\n<p>Then I started a new JSF project using Netbeans, added the jars <span class=\"__mozilla-findbar-search\" style=\"padding: 0pt; background-color: yellow; color: black; display: inline; font-size: inherit;\">which<\/span> contained my legacy code + all the other jars <span class=\"__mozilla-findbar-search\" style=\"padding: 0pt; background-color: yellow; color: black; display: inline; font-size: inherit;\">which<\/span> it depends on (basically all the Spring + Hibernate dependencies). Some time after that, I started to face some problems with Tomcat, <span class=\"__mozilla-findbar-search\" style=\"padding: 0pt; background-color: yellow; color: black; display: inline; font-size: inherit;\">which<\/span> was having problems reading my faces configuration file (again). At this moment something hit me: <span style=\"color: #ff0000;\"><strong>a Grails application doesn&#8217;t need domain classes!<\/strong><\/span><\/p>\n<p>In this moment I started my experiment:<strong> what if this web interface I needed was entirely wrote in Grails interacting with my legacy code? <\/strong>So I started a new Grails project and used only controllers (just ignored GORM entirely) and my legacy code. And it worked pretty well!<\/p>\n<p>Here is the procedure I took:<\/p>\n<p><strong>1. Start a new Grails project that only uses controller classes<br \/>\n<\/strong><\/p>\n<p>In this state, the only thing I needed to do was to change my DataSource.groovy file to look like the one below:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n\r\ndataSource {\r\n\r\n}\r\nhibernate {\r\n\r\n}\r\n\/\/ environment specific settings\r\nenvironments {\r\ndevelopment {\r\ndataSource {\r\n}\r\n}\r\ntest {\r\ndataSource {\r\n}\r\n}\r\nproduction {\r\ndataSource {\r\n}\r\n}\r\n}\r\n\r\n<\/pre>\n<p>This step is optional, but is a good move to do so. After all, it&#8217;s not a good idea to have a instance of HSQLDB floating around without any use, right?<\/p>\n<p><strong>2. Copy all your jar files to the lib directory of your Grails application<br \/>\n<\/strong><\/p>\n<p>In this case, you only need to copy those jar files <span class=\"__mozilla-findbar-search\" style=\"padding: 0pt; background-color: yellow; color: black; display: inline; font-size: inherit;\">which<\/span> contain your legacy code. And here things get really exciting: as Grails already comes with a full stack, basically you&#8217;ll only need to copy your legacy code and few libraries your application relies on.<\/p>\n<p>If your code already uses some of the libraries above, you don&#8217;t need to copy their jars to your application. Grails will add them for you: after all, they are the basic components of Grails!. Well, here is the list: Sitemesh, Hibernate, Sprng, Log4J, JUnit, Ant, Commons (several Apache Commons are used by Grails), Xalan, Xerces, Hsqldb.<\/p>\n<p>As you can see, in many cases the only library you&#8217;ll need is your JDBC driver. In my case, I only needed to copy the MySQL Connector and JExcel! Neat!<\/p>\n<p>Tip: in your Grails installation directory, browse the contents of the lib folder. Maybe you&#8217;ll find some library I just forget to mention.<\/p>\n<p><strong>3. There&#8217;s no step 3!!!<\/strong><\/p>\n<p>At the end of this process, all you have to do consist in work only with controllers classes and it&#8217;s respective views. And believe-me: it&#8217;s far more productive than using JSF managed beans or actions of Struts!<\/p>\n<p>Notice: in this case, the best thing to do is: <strong>forget about GORM!<\/strong><\/p>\n<h2>Main advantages of this approach compared to the use of &#8220;traditional&#8221; frameworks<\/h2>\n<p><a href=\"https:\/\/devkico.itexto.com.br\/wp-content\/uploads\/2009\/03\/jsf.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-331\" title=\"jsf\" src=\"https:\/\/devkico.itexto.com.br\/wp-content\/uploads\/2009\/03\/jsf.jpg\" alt=\"\" width=\"180\" height=\"180\" srcset=\"https:\/\/devkico.itexto.com.br\/wp-content\/uploads\/2009\/03\/jsf.jpg 180w, https:\/\/devkico.itexto.com.br\/wp-content\/uploads\/2009\/03\/jsf-150x150.jpg 150w\" sizes=\"(max-width: 180px) 100vw, 180px\" \/><\/a><\/p>\n<ul>\n<li><strong>100% code reuse:<\/strong> did you noticed that in any moment I had to recompile any piece of code? All I needed was my jars! That&#8217;s because Grails is based on Groovy <span class=\"__mozilla-findbar-search\" style=\"padding: 0pt; background-color: yellow; color: black; display: inline; font-size: inherit;\">which<\/span>, at the end, generates bytecode. The same bytecode we are used to work with Java!<\/li>\n<li><strong>Almost zero configuration:<\/strong> did you ever struggled with JSF or Stuts configuration? What about web.xml? Aren&#8217;t those files a pain in the ass? In Grails, the only configuration file we had to change was DataSource.groovy!<\/li>\n<li><strong>It&#8217;s Groovy Code:<\/strong> I must admit that I&#8217;m tired of working only with Java code. Sometimes is really nice to write in another language. In my case, Groovy is one of the nicests languages I know.<\/li>\n<li><strong>Easy to deploy:<\/strong> I don&#8217;t have to fight agains a huge amount of jar files. I just need to execute grails war and it&#8217;s done. All I have to do is send the war file to my server.<\/li>\n<li><strong>Easy to write (and understand) controllers: <\/strong>just compare a Grails controller to a JSF managed bean or a Struts Action and you&#8217;ll know what I mean. It&#8217;s not necessary to configure anything. All I need to do is write some closures. That&#8217;s nice.<\/li>\n<li><strong>Filters in a easy way:<\/strong> I&#8217;ll not need to implement interface, extend classes or write configuration files. I just need to implement filters the way Grails does. And it works!<\/li>\n<li><span style=\"color: #ff0000;\">It&#8217;s Grails:<\/span> this is the most important point for me. I&#8217;ll have all the productivity gains I expect from Groovy and Grails using my legace code. Your legacy code will feel refreshed!<\/li>\n<\/ul>\n<p>In the end, I achieved a paradoxal conclusion: if you are just like me, and have tons of legacy code on your back, Grails without GORM is even more powerfull than Grails with GORM. That&#8217;s because unfortunately Grails code isn&#8217;t easy to reuse. But your legacy code, when wrapped on jar files is!<\/p>\n<h2>Another possibility<\/h2>\n<p>After finishing this project another possibility of code reuse with Grails showed up: if you have your source code, you don&#8217;t even need your jar files. All you have to is to copy your source code to the src folder inside your grails application. It&#8217;ll be compiled when you start or deploy your application<\/p>\n<p>(but I still prefer jar files :) )<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Code reuse with Grails is amazingly easy. When working with this framework, it&#8217;s really cool to see the huge improvement in productivity that we can achieve. But if you have a large legacy code behind you, this productivity will simply disappear. And this is where Grails really shines and become a real choice for a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""}},"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3,7,6],"tags":[],"class_list":["post-333","post","type-post","status-publish","format-standard","hentry","category-grails","category-hibernate","category-java"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v22.8 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Grails and legacy code: making old code groovy again! - \/dev\/Kico<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/devkico.itexto.com.br\/?p=333\" \/>\n<meta property=\"og:locale\" content=\"pt_BR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Grails and legacy code: making old code groovy again! - \/dev\/Kico\" \/>\n<meta property=\"og:description\" content=\"Code reuse with Grails is amazingly easy. When working with this framework, it&#8217;s really cool to see the huge improvement in productivity that we can achieve. But if you have a large legacy code behind you, this productivity will simply disappear. And this is where Grails really shines and become a real choice for a [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/devkico.itexto.com.br\/?p=333\" \/>\n<meta property=\"og:site_name\" content=\"\/dev\/Kico\" \/>\n<meta property=\"article:published_time\" content=\"2009-03-18T23:13:19+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2009-03-29T14:57:55+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/www.itexto.net\/devkico\/wp-content\/uploads\/2009\/03\/grails-logo.png\" \/>\n<meta name=\"author\" content=\"Kico (Henrique Lobo Weissmann)\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@loboweissmann\" \/>\n<meta name=\"twitter:label1\" content=\"Escrito por\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kico (Henrique Lobo Weissmann)\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. tempo de leitura\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutos\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/devkico.itexto.com.br\/?p=333\",\"url\":\"https:\/\/devkico.itexto.com.br\/?p=333\",\"name\":\"Grails and legacy code: making old code groovy again! - \/dev\/Kico\",\"isPartOf\":{\"@id\":\"https:\/\/devkico.itexto.com.br\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/devkico.itexto.com.br\/?p=333#primaryimage\"},\"image\":{\"@id\":\"https:\/\/devkico.itexto.com.br\/?p=333#primaryimage\"},\"thumbnailUrl\":\"https:\/\/devkico.itexto.com.br\/wp-content\/uploads\/2009\/03\/grails-logo.png\",\"datePublished\":\"2009-03-18T23:13:19+00:00\",\"dateModified\":\"2009-03-29T14:57:55+00:00\",\"author\":{\"@id\":\"https:\/\/devkico.itexto.com.br\/#\/schema\/person\/502ab8892631bb005d6da2269fe5a3a7\"},\"breadcrumb\":{\"@id\":\"https:\/\/devkico.itexto.com.br\/?p=333#breadcrumb\"},\"inLanguage\":\"pt-BR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/devkico.itexto.com.br\/?p=333\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"pt-BR\",\"@id\":\"https:\/\/devkico.itexto.com.br\/?p=333#primaryimage\",\"url\":\"https:\/\/devkico.itexto.com.br\/wp-content\/uploads\/2009\/03\/grails-logo.png\",\"contentUrl\":\"https:\/\/devkico.itexto.com.br\/wp-content\/uploads\/2009\/03\/grails-logo.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/devkico.itexto.com.br\/?p=333#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/devkico.itexto.com.br\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Grails and legacy code: making old code groovy again!\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/devkico.itexto.com.br\/#website\",\"url\":\"https:\/\/devkico.itexto.com.br\/\",\"name\":\"\/dev\/Kico\",\"description\":\"Desenvolvendo software\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/devkico.itexto.com.br\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"pt-BR\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/devkico.itexto.com.br\/#\/schema\/person\/502ab8892631bb005d6da2269fe5a3a7\",\"name\":\"Kico (Henrique Lobo Weissmann)\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"pt-BR\",\"@id\":\"https:\/\/devkico.itexto.com.br\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/dd6973d86a689bc63122b2e603f25be3?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/dd6973d86a689bc63122b2e603f25be3?s=96&d=mm&r=g\",\"caption\":\"Kico (Henrique Lobo Weissmann)\"},\"sameAs\":[\"https:\/\/x.com\/loboweissmann\"],\"url\":\"https:\/\/devkico.itexto.com.br\/?author=1\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Grails and legacy code: making old code groovy again! - \/dev\/Kico","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/devkico.itexto.com.br\/?p=333","og_locale":"pt_BR","og_type":"article","og_title":"Grails and legacy code: making old code groovy again! - \/dev\/Kico","og_description":"Code reuse with Grails is amazingly easy. When working with this framework, it&#8217;s really cool to see the huge improvement in productivity that we can achieve. But if you have a large legacy code behind you, this productivity will simply disappear. And this is where Grails really shines and become a real choice for a [&hellip;]","og_url":"https:\/\/devkico.itexto.com.br\/?p=333","og_site_name":"\/dev\/Kico","article_published_time":"2009-03-18T23:13:19+00:00","article_modified_time":"2009-03-29T14:57:55+00:00","og_image":[{"url":"http:\/\/www.itexto.net\/devkico\/wp-content\/uploads\/2009\/03\/grails-logo.png"}],"author":"Kico (Henrique Lobo Weissmann)","twitter_card":"summary_large_image","twitter_creator":"@loboweissmann","twitter_misc":{"Escrito por":"Kico (Henrique Lobo Weissmann)","Est. tempo de leitura":"5 minutos"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/devkico.itexto.com.br\/?p=333","url":"https:\/\/devkico.itexto.com.br\/?p=333","name":"Grails and legacy code: making old code groovy again! - \/dev\/Kico","isPartOf":{"@id":"https:\/\/devkico.itexto.com.br\/#website"},"primaryImageOfPage":{"@id":"https:\/\/devkico.itexto.com.br\/?p=333#primaryimage"},"image":{"@id":"https:\/\/devkico.itexto.com.br\/?p=333#primaryimage"},"thumbnailUrl":"https:\/\/devkico.itexto.com.br\/wp-content\/uploads\/2009\/03\/grails-logo.png","datePublished":"2009-03-18T23:13:19+00:00","dateModified":"2009-03-29T14:57:55+00:00","author":{"@id":"https:\/\/devkico.itexto.com.br\/#\/schema\/person\/502ab8892631bb005d6da2269fe5a3a7"},"breadcrumb":{"@id":"https:\/\/devkico.itexto.com.br\/?p=333#breadcrumb"},"inLanguage":"pt-BR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/devkico.itexto.com.br\/?p=333"]}]},{"@type":"ImageObject","inLanguage":"pt-BR","@id":"https:\/\/devkico.itexto.com.br\/?p=333#primaryimage","url":"https:\/\/devkico.itexto.com.br\/wp-content\/uploads\/2009\/03\/grails-logo.png","contentUrl":"https:\/\/devkico.itexto.com.br\/wp-content\/uploads\/2009\/03\/grails-logo.png"},{"@type":"BreadcrumbList","@id":"https:\/\/devkico.itexto.com.br\/?p=333#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/devkico.itexto.com.br\/"},{"@type":"ListItem","position":2,"name":"Grails and legacy code: making old code groovy again!"}]},{"@type":"WebSite","@id":"https:\/\/devkico.itexto.com.br\/#website","url":"https:\/\/devkico.itexto.com.br\/","name":"\/dev\/Kico","description":"Desenvolvendo software","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/devkico.itexto.com.br\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"pt-BR"},{"@type":"Person","@id":"https:\/\/devkico.itexto.com.br\/#\/schema\/person\/502ab8892631bb005d6da2269fe5a3a7","name":"Kico (Henrique Lobo Weissmann)","image":{"@type":"ImageObject","inLanguage":"pt-BR","@id":"https:\/\/devkico.itexto.com.br\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/dd6973d86a689bc63122b2e603f25be3?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/dd6973d86a689bc63122b2e603f25be3?s=96&d=mm&r=g","caption":"Kico (Henrique Lobo Weissmann)"},"sameAs":["https:\/\/x.com\/loboweissmann"],"url":"https:\/\/devkico.itexto.com.br\/?author=1"}]}},"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/devkico.itexto.com.br\/index.php?rest_route=\/wp\/v2\/posts\/333"}],"collection":[{"href":"https:\/\/devkico.itexto.com.br\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devkico.itexto.com.br\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devkico.itexto.com.br\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/devkico.itexto.com.br\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=333"}],"version-history":[{"count":3,"href":"https:\/\/devkico.itexto.com.br\/index.php?rest_route=\/wp\/v2\/posts\/333\/revisions"}],"predecessor-version":[{"id":358,"href":"https:\/\/devkico.itexto.com.br\/index.php?rest_route=\/wp\/v2\/posts\/333\/revisions\/358"}],"wp:attachment":[{"href":"https:\/\/devkico.itexto.com.br\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=333"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devkico.itexto.com.br\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=333"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devkico.itexto.com.br\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=333"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}