I find myself slipping back into web development in the new year. I’ve known this was coming for some time, so I’ve had a fair chance to carefully choose my weapons:
- Compojure (on github)
- Enlive for templating
- Jetty (during development, anyway; we’ll see about production)
What has really tied this all together is Maven (and a couple of plugins for it), which has enabled me to fill in a couple of gaps in what is otherwise the most pleasant web development environment I’ve ever used (where Pylons was the prior champ, FWIW).
The biggest gap is in automatic application reloading/redeployment – in concrete terms, when I save a Clojure source file, my application should be reloaded nearly immediately, thereby avoiding any code-build-deploy cycle. To be precise, this capability is built into Jetty (as it is in many other Java-based app servers). The question is, how to most readily utilize it.
I came across this post by Jim Downing, which describes how to set up a Maven project for a Compojure application, enabling development-mode app reloading using the maven-jetty-plugin (the formatting on that post appears to have degraded since it was published; you can check out the project described in the post here). This certainly appears to fit the bill; unfortunately, the setup that Jim describes there doesn’t quite work for me – when I save a source file, the application is automatically redeployed, but no changes are picked up.
Thankfully, the fix is easy. Below is the relevant section of my pom.xml, configuring maven-jetty-plugin to add my Clojure source root as an extra classpath element. This allows Clojure, running in the jetty application server, to find and load any Clojure source files that are newer than their AOT-compiled counterparts in the usual
target/classes directory (note the
<plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>maven-jetty-plugin</artifactId> <version>6.1.15</version> <configuration> <contextPath>/</contextPath> <webAppConfig> <extraClasspath>src/main/clojure</extraClasspath> </webAppConfig> <scanIntervalSeconds>5</scanIntervalSeconds> <connectors> <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector"> <port>8080</port> <maxIdleTime>60000</maxIdleTime> </connector> </connectors> <scanTargetPatterns> <scanTargetPattern> <directory>src/main/clojure</directory> <includes> <include>**/*.clj</include> </includes> </scanTargetPattern> </scanTargetPatterns> </configuration> </plugin>
With that, I’m just a
mvn jetty:run away (or, really, a single click away in NetBeans) from having a development process identical to
paster serve --reload, with the added benefit of Clojurey goodness.
♬♫The more you know…♪♬
(Apologies to those who aren’t familiar with American pop culture.)
If you want to compile Clojure code (and really, if you’re involved in a project of any size or importance, you should be, if only to avoid forcing Clojure to generate bytecode at runtime, which will slow down the sort of rapid development enabled by automatic app redeployment as describe above), do me a favor and use clojure-maven-plugin. (The post I reference above manually invokes the Clojure compiler using ant’s exec task, but that was what you had to do back in July 2009.) It’s a great piece of kit, and additionally serves as a perfect gateway drug to Maven – which, despite the controversy, and my own quibbles with various aspects of it, will eventually save your bacon in any larger project.