Compojure + Maven
July 30, 2009
Update: Chas Emerick has written a much more accurate, up-to-date and comprehensive guide.
Being a terse guide to getting mavenic web app development with compojure. The code is available at http://bitbucket.org/jimdowning/hello-mvn-clj/.
1. Download, build and install clojure-contrib
git clone git://github.com/richhickey/clojure-contrib.git
cd clojure-contrib
ant
mvn install -DpomFile=pom.xml -Dfile=clojure-contrib.jar
2. Download, build and install compojure
git clone git://github.com/weavejester/compojure.git
cd compojure
ant deps
ant
mvn install:install-file -DgroupId=org.clojure -DartifactId=compojure \
-Dversion=1.0-SNAPSHOT -Dfile=compojure.jar -Dpackaging=jar
3. Create your Compojure app
In src/main/clj/example/MyApp.clj:
;; MyApp (ns example.MyApp (:use compojure) (:gen-class :extends javax.servlet.http.HttpServlet)) ;; Router (defroutes my-app (GET "/" (html [:h1 "Hello World!"])) (ANY "*" (page-not-found))) (defservice my-app)
4. Get Maven to compile your servlet
In pom.xml in the build section:
org.codehaus.mojo
exec-maven-plugin
1.1
compile
exec
java
-Dclojure.compile.path=target/classes
-classpath
clojure.lang.Compile
example.MyApp
5. Make sure your clojure files get copied on to the file path
In pom.xml in the build section:
${project.basedir}/src/main/clj
6. Configure the jetty plugin
Again, in the build section of pom.xml:
org.mortbay.jetty
maven-jetty-plugin
6.1.15
5
8080
60000
src/main/clj
**/*.clj
7. Fix up the rest of the dependencies and the rest of the mvn stuff
The usual boilerplate – grab it from bitbucket: http://bitbucket.org/jimdowning/hello-mvn-clj/src/tip/pom.xml.
8. GO!
mvn jetty:run
Will run your application up on a localhost server, and any mods to the clojure will trigger an application redeployment. Ace!
December 5, 2009 at 6:34 am
Wow – thanks for the concise info. You didn’t mention about web.xml but thankfully it is in your source 🙂
January 8, 2010 at 12:39 pm
Thanks for the pointer — the configuration of the jetty plugin only needed a little tweak to get this working for me. Looks like comments with URLs are being blocked right now, so go to muckandbrass.com if you’re interested.
Thanks again!
January 8, 2010 at 5:43 pm
Just curious, why not just use leiningen ( http://zef.me/2470/building-clojure-projects-with-leiningen)
It hides & wraps the maven ugliness
January 9, 2010 at 12:16 pm
@chetan A few reasons: –
1/ At the moment leiningen isn’t doing everything maven gives me (even in this brief example).
2/ I’m not too keen on the leiningen’s default dependency building mechanism of copying jars to lib (although I can see that having this facility would be useful in some circumstances)
3/ We’ve got a lot of other maven projects and a build environment (CI, nexus etc) that works well with maven. A move to leiningen would likely be a lot of hassle for a bit of neat syntax!
April 3, 2010 at 11:18 am
Thanks for this tutorial! This saved a lot of time.
IMHO it’s better to replace the maven-exec-plugin with
the clojure-maven-plugin (http://github.com/talios/clojure-maven-plugin) for clojure compilation. Less config, more flexibility.
Example:
com.theoryinpractise
clojure-maven-plugin
1.3.1
src/main/clj
src/test/clj
April 3, 2010 at 5:36 pm
@MelWinkelk I agree – this is out of date – better to use the clojure-maven-plugin these days.
September 6, 2010 at 1:03 pm
[…] came across this post by Jim Downing, which describes how to set up a Maven project for a Compojure application, enabling […]