It’s all very well having your Clojure application compiled into a series of class files, but sometimes you just want something that’s easy to deploy. Ideally a single jar file with everything ready to go.

There’s a tool called Leiningen which does just this. The tool is good, but I had a couple of problems getting it to run under windows, so I thought I’d document here how I got it working for a simple hello world example.

Leiningen can self-install on other platforms apart from windows. In the Windows case, you need the lein.bat script from the Installation section on the github page. You’ll need to the 1.1.0 version of Leiningen – the script comments leads you to the 1.0.1 version of Leiningen, but this didn’t work for me. You’ll also need a version of Clojure – I used 1.1.0 which I already had on my machine. You need to point some environment variables at these jar files.

set LEIN_JAR=C:UsersCliveDesktopleinleiningen-1.1.0-standalone.jar

set CLOJURE_JAR=C:/Users/Clive/Desktop/lein/clojure.jar

I set up a directory structure – I made a helloworld directory, and into src/test/hello.clj I put the code

(ns test.hello

(:gen-class))

(defn -main [& args]

(println "Hello world!"))

We can now define a project file that specifies our entry point, and the jars that we depend upon.

(defproject test.hello "1.0"

:dependencies [[org.clojure/clojure "1.1.0-master-SNAPSHOT"]]

:main test.hello)

Leiningen offers a number of command line arguments. The first important one is “compile” to build the project.

C:UsersCliveDesktopleinhelloworld>..lein.bat compile

[copy] Copying 1 file to C:UsersCliveDesktopleinhelloworldlib

Compiling test.hello

We can then run a repl with the compiled code on the class path.

C:UsersCliveDesktopleinhelloworld>..lein.bat repl

Clojure 1.1.0

user=> (use ‘test.hello)

nil

user=> (-main)

Hello world!

nil

But better still, we can build a jar of the project, and we can also build an uberjar that includes the project code and the code for Clojure i.e an easily deployed solution.

C:UsersCliveDesktopleinhelloworld>..lein.bat uberjar

All :namespaces already compiled.

Created C:UsersCliveDesktopleinhelloworld/test.hello.jar

Including test.hello.jar

Including clojure-1.1.0-master-20091231.150150-10.jar

C:UsersCliveDesktopleinhelloworld>dir

Directory of C:UsersCliveDesktopleinhelloworld

11/04/2010 09:09 <DIR> .

11/04/2010 09:09 <DIR> ..

11/04/2010 09:07 <DIR> classes

11/04/2010 09:07 <DIR> lib

10/04/2010 22:03 143 project.clj

10/04/2010 21:58 <DIR> src

11/04/2010 09:09 1,876,304 test.hello-standalone.jar

11/04/2010 09:09 5,493 test.hello.jar

We can easily run this using the command line.

C:UsersCliveDesktopleinhelloworld>java -cp test.hello-standalone.jar test.hello

Hello world!