Fun with Grape and Ratpack, Part 1
I’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
Grape
Grape 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’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.
Ratpack
Inspired by Sinatra , Ratpack 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.
Grape + Ratpack = (Mostly) Awesome!
By now, you should see where I’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.
However, to get this near perfect world, we need to do a little bit of work.
Ratpack is very new, and not available in any public Maven repository yet. We’ll get around that by installing Ratpack into our local Maven repository. Ratpack does require Gradle ( another awesome project I hope to talk about soon ), so make sure you have that installed
Unfortunately, Grape by default doesn’t utilize local Maven repositories, so we’ll have to update the grapeConfig.xml file.
Neither of these steps are that complicated, so we’ll run right through them
Build Ratpack and Install Locally
- clone Ratpack and build it
aadid$ git clone git@github.com:cilquirm/Ratpack.git
aadid$ cd Ratpack
aadid$ gradle build
- While still in the Ratpack project directory, install the Ratpack jar into your local maven directory
aadid$ mvn install:install-file -Dfile=build/lib/Ratpack-0.1.jar -DgroupId=com.bleedingwolf -DartifactId=Ratpack -Dversion=0.1 -Dpackaging=jar -DgeneratePom=true
Update Grape’s Configuration
Updating Grape’s configuration is just a matter of adding a line to your ~/.groovy/grapeConfig.xml . Unfortunately for me, I didn’t have a grapeConfig.xml, but a quick search led me to the default grapeConfig.xml here . I’ve replicated it here ( with the necessary line to check local repos ):
<ivysettings>
<settings defaultResolver="downloadGrapes"/>
<resolvers>
<chain name="downloadGrapes">
<filesystem name="cachedGrapes">
<ivy pattern="${user.home}/.groovy/grapes/[organisation]/[module]/ivy-[revision].xml"/>
<artifact pattern="${user.home}/.groovy/grapes/[organisation]/[module]/[type]s/[artifact]-[revision].[ext]"/>
</filesystem>
<ibiblio name="local" root="file:${user.home}/.m2/repository" m2compatible="true"/>
<ibiblio name="codehaus" root="http://repository.codehaus.org/" m2compatible="true"/>
<ibiblio name="ibiblio" m2compatible="true"/>
<ibiblio name="java.net2" root="http://download.java.net/maven/2/" m2compatible="true"/>
</chain>
</resolvers>
</ivysettings>
If you do have a grapeConfig.xml, then all you need is this line in your <chain> and you're good to go
<ibiblio name="local" root="file:${user.home}/.m2/repository" m2compatible="true"/>
A first web script
Now that we’ve gotten all that rigamarole out of the way, we can get to writing a script. Here’s a simple “Hello World” script that I’ll break down right after :
@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("/") { "Hello World!" }
}
RatpackServlet.serve(app)
The first @Grab should be self-explanatory — 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.
I’m not actually sure what the @GrabConfig does, but I get the feeling that it lets my script work seamlessly.
The rest of the script is cribbed almost directly from the Ratpack github site, so you should head over there for possibilities
Conclusion
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.
Grape is one of the best features I’ve seen come for any language/framework. It’s usefulness cannot be understated — it makes Groovy a first-class language for scripting, and really claim its place as “enterprise glue.”
Ratpack is nascent, but already fun and useful. It’s a welcome addition to the Groovy web frameworks ecosystem, and I’m looking forward to seeing where it goes.
Oh, and next time, I’ll actually do something useful with this setup.