{"id":54011,"date":"2021-12-23T10:19:52","date_gmt":"2021-12-23T04:49:52","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=54011"},"modified":"2022-01-13T11:49:34","modified_gmt":"2022-01-13T06:19:34","slug":"groovying-on-template-aid-for-java","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/groovying-on-template-aid-for-java\/","title":{"rendered":"Groovying on Template Aid for Java"},"content":{"rendered":"<p>If you haven&#8217;t combined\u00a0<a href=\"https:\/\/groovy-lang.org\/\">Groovy<\/a> scripts with your Java projects yet, you&#8217;re missing out. It&#8217;s super easy with\u00a0<a href=\"https:\/\/maven.apache.org\/\">Maven<\/a>, and it&#8217;ll make your code more modular and elegant. But most importantly, you&#8217;ll have some fun scripting, and if nothing else, you&#8217;ll most certainly benefit from some of the <em>grooviest<\/em> features, including the magic that is\u00a0<a href=\"https:\/\/groovy-lang.org\/syntax.html#all-strings\">Groovy&#8217;s GString<\/a>!<\/p>\n<p>You can check out the code for this tutorial in GITHUB.<\/p>\n<h4><b>The given environment<\/b><\/h4>\n<p>Let&#8217;s say you have a service for connecting customers to representatives via some sort of messaging service. When the customer first reaches out to the service, he should be responded with a standard message (a template) that includes his name, his queue number, and an informational greeting of some sort.<\/p>\n<h4><b>Groovy scripting<\/b><\/h4>\n<p>Create a file as a template for your messages.<\/p>\n<h3><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-54321 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2021\/12\/Blog4-01-scaled.jpg\" alt=\"\" width=\"2560\" height=\"604\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2021\/12\/Blog4-01-scaled.jpg 2560w, \/blog\/wp-ttn-blog\/uploads\/2021\/12\/Blog4-01-300x71.jpg 300w, \/blog\/wp-ttn-blog\/uploads\/2021\/12\/Blog4-01-1024x242.jpg 1024w, \/blog\/wp-ttn-blog\/uploads\/2021\/12\/Blog4-01-768x181.jpg 768w, \/blog\/wp-ttn-blog\/uploads\/2021\/12\/Blog4-01-1536x363.jpg 1536w, \/blog\/wp-ttn-blog\/uploads\/2021\/12\/Blog4-01-2048x483.jpg 2048w, \/blog\/wp-ttn-blog\/uploads\/2021\/12\/Blog4-01-624x147.jpg 624w\" sizes=\"(max-width: 2560px) 100vw, 2560px\" \/><\/h3>\n<ul>\n<li>The\u00a0<code>def<\/code>\u00a0statements allow us to bind arguments from the shell.<\/li>\n<li>The\u00a0<code>\"\"\"<\/code>\u00a0marks the text as a\u00a0<code>GString<\/code>, which allows us, in this case to:\n<ul>\n<li>Easily incorporate the arguments with no\u00a0<code>String.format<\/code>\u00a0required.<\/li>\n<li>Produce multi-line strings without worrying about line breaks and long lines.<\/li>\n<\/ul>\n<\/li>\n<li>What you might have noticed missing here, is the\u00a0<code>return<\/code>\u00a0statement. With\u00a0<em>Groovy<\/em>\u00a0the last statement\u00a0<strong>is<\/strong>\u00a0the return statement.<\/li>\n<\/ul>\n<p>Now let&#8217;s invoke the script with some intentionally boiler plated\u00a0<em>Java<\/em>\u00a0code:<\/p>\n<h3><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-54322 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2021\/12\/Blog4-02-scaled.jpg\" alt=\"\" width=\"2560\" height=\"1644\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2021\/12\/Blog4-02-scaled.jpg 2560w, \/blog\/wp-ttn-blog\/uploads\/2021\/12\/Blog4-02-300x193.jpg 300w, \/blog\/wp-ttn-blog\/uploads\/2021\/12\/Blog4-02-1024x658.jpg 1024w, \/blog\/wp-ttn-blog\/uploads\/2021\/12\/Blog4-02-768x493.jpg 768w, \/blog\/wp-ttn-blog\/uploads\/2021\/12\/Blog4-02-1536x986.jpg 1536w, \/blog\/wp-ttn-blog\/uploads\/2021\/12\/Blog4-02-2048x1315.jpg 2048w, \/blog\/wp-ttn-blog\/uploads\/2021\/12\/Blog4-02-624x401.jpg 624w\" sizes=\"(max-width: 2560px) 100vw, 2560px\" \/><\/h3>\n<p>Can you see how easy it is to add arguments? Just add them to the script, and add the binding to the invoking class. No more &#8220;scratching your head&#8221; when it comes to concatenating long strings&#8230;<br \/>\nThe real conundrum here is, how do we get the script in our class loader?<\/p>\n<p>It&#8217;s easy with\u00a0<em>Maven<\/em>, let&#8217;s say this is our project layout:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-54324 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2021\/12\/Blog4-03-scaled.jpg\" alt=\"\" width=\"2560\" height=\"604\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2021\/12\/Blog4-03-scaled.jpg 2560w, \/blog\/wp-ttn-blog\/uploads\/2021\/12\/Blog4-03-300x71.jpg 300w, \/blog\/wp-ttn-blog\/uploads\/2021\/12\/Blog4-03-1024x242.jpg 1024w, \/blog\/wp-ttn-blog\/uploads\/2021\/12\/Blog4-03-768x181.jpg 768w, \/blog\/wp-ttn-blog\/uploads\/2021\/12\/Blog4-03-1536x363.jpg 1536w, \/blog\/wp-ttn-blog\/uploads\/2021\/12\/Blog4-03-2048x483.jpg 2048w, \/blog\/wp-ttn-blog\/uploads\/2021\/12\/Blog4-03-624x147.jpg 624w\" sizes=\"(max-width: 2560px) 100vw, 2560px\" \/><\/p>\n<p>First, we need to include the <a href=\"https:\/\/mvnrepository.com\/artifact\/org.codehaus.groovy\/groovy\">groovy dependency<\/a>.<\/p>\n<p>Now we have the appropriate\u00a0<em>Groovy<\/em>\u00a0API classes like\u00a0<em>GroovyShell<\/em>\u00a0and\u00a0<em>Binding<\/em>\u00a0in our class loader.<br \/>\nWe now need to add the following to our\u00a0<code>build<\/code>\u00a0section in our\u00a0<code>pom.xml<\/code>:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-54325 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2021\/12\/Blog4-04-scaled.jpg\" alt=\"\" width=\"2560\" height=\"562\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2021\/12\/Blog4-04-scaled.jpg 2560w, \/blog\/wp-ttn-blog\/uploads\/2021\/12\/Blog4-04-300x66.jpg 300w, \/blog\/wp-ttn-blog\/uploads\/2021\/12\/Blog4-04-1024x225.jpg 1024w, \/blog\/wp-ttn-blog\/uploads\/2021\/12\/Blog4-04-768x169.jpg 768w, \/blog\/wp-ttn-blog\/uploads\/2021\/12\/Blog4-04-1536x337.jpg 1536w, \/blog\/wp-ttn-blog\/uploads\/2021\/12\/Blog4-04-2048x450.jpg 2048w, \/blog\/wp-ttn-blog\/uploads\/2021\/12\/Blog4-04-624x137.jpg 624w\" sizes=\"(max-width: 2560px) 100vw, 2560px\" \/><\/p>\n<p>This will add everything from our\u00a0<code>src\/scripts<\/code>\u00a0folder to our class loader under the folder\u00a0<code>scripts<\/code>.<br \/>\nSo we&#8217;ll be able to find our script like so:\u00a0<code>scripts\/create_message.groovy<\/code>.<\/p>\n<h4><b>Java: Bi-function applying<\/b><\/h4>\n<p>This is fine and good to use too, just that there will be big long constant strings in your code.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-54326 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2021\/12\/Blog4-05-scaled.jpg\" alt=\"\" width=\"2560\" height=\"1161\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2021\/12\/Blog4-05-scaled.jpg 2560w, \/blog\/wp-ttn-blog\/uploads\/2021\/12\/Blog4-05-300x136.jpg 300w, \/blog\/wp-ttn-blog\/uploads\/2021\/12\/Blog4-05-1024x465.jpg 1024w, \/blog\/wp-ttn-blog\/uploads\/2021\/12\/Blog4-05-768x348.jpg 768w, \/blog\/wp-ttn-blog\/uploads\/2021\/12\/Blog4-05-1536x697.jpg 1536w, \/blog\/wp-ttn-blog\/uploads\/2021\/12\/Blog4-05-2048x929.jpg 2048w, \/blog\/wp-ttn-blog\/uploads\/2021\/12\/Blog4-05-624x283.jpg 624w\" sizes=\"(max-width: 2560px) 100vw, 2560px\" \/><\/p>\n<p>&nbsp;<\/p>\n<div class=\"ap-custom-wrapper\"><\/div><!--ap-custom-wrapper-->","protected":false},"excerpt":{"rendered":"<p>If you haven&#8217;t combined\u00a0Groovy scripts with your Java projects yet, you&#8217;re missing out. It&#8217;s super easy with\u00a0Maven, and it&#8217;ll make your code more modular and elegant. But most importantly, you&#8217;ll have some fun scripting, and if nothing else, you&#8217;ll most certainly benefit from some of the grooviest features, including the magic that is\u00a0Groovy&#8217;s GString! You [&hellip;]<\/p>\n","protected":false},"author":1393,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":2},"categories":[446,3917,1994,1],"tags":[9,4844],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/54011"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1393"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=54011"}],"version-history":[{"count":7,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/54011\/revisions"}],"predecessor-version":[{"id":54419,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/54011\/revisions\/54419"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=54011"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=54011"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=54011"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}