{"id":1970,"date":"2010-10-21T17:06:23","date_gmt":"2010-10-21T11:36:23","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=1970"},"modified":"2016-12-19T15:06:11","modified_gmt":"2016-12-19T09:36:11","slug":"execute-groovy-script-in-grails","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/execute-groovy-script-in-grails\/","title":{"rendered":"Execute Groovy script in Grails"},"content":{"rendered":"<p>Working with groovy is really a cake walk. Whenever I get some free time I try to know more about it. Today I was just wondering on the net and I found some cool tips to execute groovy file in grails. There are so many other ways available to run groovy script in grails, I will share one of the way to do it.<\/p>\n<p>To know it we will take a very simple example like we have a groovy script CapitalizeWords.groovy which capitalize all the words of the arguments. The script is like this <\/p>\n<p>[java]<br \/>\nList words=[]<br \/>\nthis.args.each{arg-&gt;<br \/>\n\targ.split().each{<br \/>\n\t\twords.add(it.toUpperCase())<br \/>\n\t}<br \/>\n}<\/p>\n<p>return words<br \/>\n[\/java]<\/p>\n<p>Now we will call this script in one of our action like this <\/p>\n<p>[java]<br \/>\ndef someAction ={<br \/>\n     File file = new File(&quot;${servletContext.getRealPath(&quot;\/&quot;)}CapitalizeWords.groovy&quot;)<br \/>\n     GroovyShell groovyShell = new GroovyShell()<br \/>\n     def result = groovyShell.run(file, [&quot;Hello &quot;, &quot;this is &quot;, &quot;uday &quot;, &quot;saying Hii to you&quot;])<br \/>\n     render &quot;&#8212;&#8212;${result}&#8212;&#8212;&quot;<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>The output for this would be<br \/>\n[java]<br \/>\n&#8212;&#8212;[HELLO, THIS, IS, UDAY, SAYING, HII, TO, YOU]&#8212;&#8212;<br \/>\n[\/java]<\/p>\n<p>In run method of GroovyShell class you pass the file that need to execute and the list of arguments.<br \/>\n<\/p>\n<p>Hope it helps<br \/>\n<a href=\"http:\/\/www.tothenew.com\/blog\/author\/uday\/\">Uday Pratap Singh<\/a><br \/>\n<a href=\"mailto:uday@intelligrape.com\">uday@intelligrape.com<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Working with groovy is really a cake walk. Whenever I get some free time I try to know more about it. Today I was just wondering on the net and I found some cool tips to execute groovy file in grails. There are so many other ways available to run groovy script in grails, I [&hellip;]<\/p>\n","protected":false},"author":11,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":0},"categories":[7],"tags":[440,439],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/1970"}],"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\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=1970"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/1970\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=1970"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=1970"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=1970"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}