Jul 4, 2011 0
Creating new projects with Leiningen
Leiningen is the “maven” for clojure. It is a build management tool which you can basically use to compile, package your Clojure projects and manage dependencies. In this introductionary tutorial, i will show you the basics to create clojure projects on an unix environmnent using Leiningen.
Installing Leiningen:
$ pwd /home/bagdemir $ mkdir leiningen $ export PATH=$PATH:/home/bagdemir/leiningen $ cd /home/bagdemir/leiningen $ wget http://github.com/technomancy/leiningen/raw/stable/bin/lein $ chmod +x lein $ lein self-install
Now we can test lein calling
$ lein version Leiningen 1.6.0 on Java 1.6.0_24 Java HotSpot(TM) Client VM
We can now create a new project using new:
$ lein new localwebapp
After calling new, lein creates a structured project folder, project file and a README file for you. If you look into the project file, project.clj, you notice that the project file is also a clojure file not a xml as compared with maven’s pom.xml:
(defproject localwebapp "1.0.0-SNAPSHOT" :description "FIXME: write description" :dependencies [[org.clojure/clojure "1.2.1"]])
defproject macro is followed by a symbol which defines project name and version. The reminder elements are in key-value format like “description” which indicates project description and the dependencies are project dependencies.
Dependency management in lein is like maven’s. You can define your dependencies after :dependencies key in a vector, [ [GROUPID/ARTIFACTID "version"] … ]. Calling
$ lein deps
causes all the dependent libraries to be downloaded from repositories and copied, first, into local repository (on my machine /home/.m2/repository) and then after into the “lib” folder in your project which will be also created by lein.
You can also add some repositories in project.clj like this:
:repositories {"apache-releases" "http://repository.apache.org/content/repositories/releases/"}Some useful commands are:
$ lein clean $ lein compile $ lein jar $ lein uberjar
which deletes all your files created during build including lib folder, performs compilation of clojures into java classes, and packages project as jar and later packages also, but with all dependencies including libraries to create an independent jar – if this jar is an executable one, then you have to introduce a namespace as an entry point defined with :main in project.clj.


