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!

About these ads

7 Responses to “Compojure + Maven”


  1. Wow – thanks for the concise info. You didn’t mention about web.xml but thankfully it is in your source :)

  2. Chas Emerick Says:

    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!

  3. chetan Says:

    Just curious, why not just use leiningen ( http://zef.me/2470/building-clojure-projects-with-leiningen)
    It hides & wraps the maven ugliness

  4. jimdowning Says:

    @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!

  5. MelWinkelk Says:

    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

  6. jimdowning Says:

    @MelWinkelk I agree – this is out of date – better to use the clojure-maven-plugin these days.


  7. […] came across this post by Jim Downing, which describes how to set up a Maven project for a Compojure application, enabling […]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 84 other followers

%d bloggers like this: