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!

Advertisements