<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Ad Arbitrium</title>
	<atom:link href="http://blog.adarbitrium.com/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://blog.adarbitrium.com</link>
	<description>thoughts in service to me</description>
	<lastBuildDate>Thu, 26 Aug 2010 07:39:22 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Fun with Grape and Ratpack, Part 1</title>
		<link>http://blog.adarbitrium.com/?p=49</link>
		<comments>http://blog.adarbitrium.com/?p=49#comments</comments>
		<pubDate>Thu, 26 Aug 2010 07:37:30 +0000</pubDate>
		<dc:creator>cilquirm</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.adarbitrium.com/?p=49</guid>
		<description><![CDATA[Putting Grape and Ratpack together makes for a nice, fast, web prototyping environment]]></description>
			<content:encoded><![CDATA[<p><em>I&#8217;m excited to be kicking off my new, more technically oriented blog with this little post.  I also hope that I manage to keep it going :-p</em></p>

<h3>Grape</h3>

<p><a href="http://groovy.codehaus.org/Grape">Grape</a> is one of the most exciting and useful functions bundled with Groovy.  In a nutshell, Grape leverages the Maven/Ivy repository infrastructure to download necessary libraries and add them to the running script&#8217;s classpath so that you can leverage them in your scripts without having to muck with command-line switches or having to download a mess of jars.</p>

<h3>Ratpack</h3>

<p>Inspired by <a href="http://sinatrarb.com">Sinatra</a> , <a href="http://github.com/bleedingwolf/Ratpack">Ratpack</a> is simple, fun framework/DSL for creating web applications fast. Ratpack allows you to compose a full web application practically in one file ( though it does have support for necessary features like static resources and external templates ).  Ratpack is an early beta, so obviously the usual caveats apply.  </p>

<h3>Grape + Ratpack = (Mostly) Awesome!</h3>

<p>By now, you should see where I&#8217;m going with this.  Putting Ratpack and Grape together means that, with a little boilerplate, you can kick off a mini web project with practically nothing but an empty directory and a dream.</p>

<p>However, to get this near perfect world, we need to do a little bit of work.  </p>

<p>Ratpack is very new, and not available in any public Maven repository yet.  We&#8217;ll get around that by installing Ratpack into our local Maven repository. Ratpack does require <a href="http://www.gradle.org">Gradle</a> ( another awesome project I hope to talk about soon ), so make sure you have that installed</p>

<p>Unfortunately, Grape by default doesn&#8217;t utilize local Maven repositories, so we&#8217;ll have to update the grapeConfig.xml file.</p>

<p>Neither of these steps are that complicated, so we&#8217;ll run right through them</p>

<h3>Build Ratpack and Install Locally</h3>



<ul>
<li>clone Ratpack and build it</li>
</ul>




<pre><code>aadid$ git clone git@github.com:cilquirm/Ratpack.git
aadid$ cd Ratpack 
aadid$ gradle build</code></pre>




<ul>
<li>While still in the Ratpack project directory, install the Ratpack jar into your local maven directory</li>
</ul>



<pre><code>aadid$ mvn install:install-file -Dfile=build/lib/Ratpack-0.1.jar -DgroupId=com.bleedingwolf -DartifactId=Ratpack -Dversion=0.1 -Dpackaging=jar -DgeneratePom=true</code></pre>

<h3>Update Grape&#8217;s Configuration</h3>

<p>Updating Grape&#8217;s configuration is just a matter of adding a line to your <code>~/.groovy/grapeConfig.xml</code> . Unfortunately for me, I didn&#8217;t have a grapeConfig.xml, but a quick search led me to the default grapeConfig.xml <a href="http://groovy.codehaus.org/Grape#Grape-CustomizeIvysettings">here</a> . I&#8217;ve replicated it here ( with the necessary line to check local repos ):</p>

<pre><code>&lt;ivysettings&gt;
  &lt;settings defaultResolver=&quot;downloadGrapes&quot;/&gt;
  &lt;resolvers&gt;
    &lt;chain name=&quot;downloadGrapes&quot;&gt;
      &lt;filesystem name=&quot;cachedGrapes&quot;&gt;
        &lt;ivy pattern=&quot;${user.home}/.groovy/grapes/[organisation]/[module]/ivy-[revision].xml&quot;/&gt;
        &lt;artifact pattern=&quot;${user.home}/.groovy/grapes/[organisation]/[module]/[type]s/[artifact]-[revision].[ext]&quot;/&gt;
      &lt;/filesystem&gt;
      <!-- todo add 'endorsed groovy extensions' resolver here -->
      &lt;ibiblio name=&quot;local&quot;     root=&quot;file:${user.home}/.m2/repository&quot; m2compatible=&quot;true&quot;/&gt;
      &lt;ibiblio name=&quot;codehaus&quot; root=&quot;http://repository.codehaus.org/&quot; m2compatible=&quot;true&quot;/&gt;
      &lt;ibiblio name=&quot;ibiblio&quot; m2compatible=&quot;true&quot;/&gt;
      &lt;ibiblio name=&quot;java.net2&quot; root=&quot;http://download.java.net/maven/2/&quot; m2compatible=&quot;true&quot;/&gt;
    &lt;/chain&gt;
  &lt;/resolvers&gt;
&lt;/ivysettings&gt;

If you do have a grapeConfig.xml, then all you need is this line in your &lt;chain&gt; and you're good to go</code></pre>

<pre><code>&lt;ibiblio name=&quot;local&quot; root=&quot;file:${user.home}/.m2/repository&quot; m2compatible=&quot;true&quot;/&gt;</code></pre>

<h3>A first web script</h3>

<p>Now that we&#8217;ve gotten all that rigamarole out of the way, we can get to writing a script.  Here&#8217;s a simple &#8220;Hello World&#8221; script that I&#8217;ll break down right after :</p>

<pre><code>@Grapes([
    @Grab(group='com.bleedingwolf', module='Ratpack', version='0.1'),
    @Grab(group='org.mortbay.jetty', module='jetty', version='6.1.25'),
    @GrabConfig(systemClassLoader=true)
])

import com.bleedingwolf.ratpack.Ratpack
import com.bleedingwolf.ratpack.RatpackServlet

def app = Ratpack.app {

    get(&quot;/&quot;) { &quot;Hello World!&quot; }

}

RatpackServlet.serve(app)</code></pre>

<p>The first @Grab should be self-explanatory &#8212; it references the Ratpack framework.  Underneath the hood, Ratpack uses Jetty as the application container for the script, so we need to reference the Jetty libraries, which is what the second @Grab does.  </p>

<p>I&#8217;m not actually sure what the @GrabConfig does, but I get the feeling that it lets my script work seamlessly.  </p>

<p>The rest of the script is cribbed almost directly from the Ratpack github site, so you should head over there for possibilities</p>

<h3>Conclusion</h3>

<p>I hope this post starts opening up the possibilities of Grape and Ratpack.  Eventually, I hope that Ratpack makes into the right Ivy repositories and most of this is unnecessary.  </p>

<p>Grape is one of the best features I&#8217;ve seen come for any language/framework.  It&#8217;s usefulness cannot be understated &#8212; it makes Groovy a first-class language for scripting, and really claim its place as &#8220;enterprise glue.&#8221;</p>

<p>Ratpack is nascent, but already fun and useful.  It&#8217;s a welcome addition to the Groovy web frameworks ecosystem, and I&#8217;m looking forward to seeing where it goes.</p>

<p><em>Oh, and next time, I&#8217;ll actually do something useful with this setup.</em></p>]]></content:encoded>
			<wfw:commentRss>http://blog.adarbitrium.com/?feed=rss2&amp;p=49</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
