Introduction to Leiningen


Leiningen is a powerful way to build your clojure projects. A common drawback working on interpreted languages is that you can’t figure out how to distribute your final work, unless you use a third party software. Leiningen allows you to create compile and distribute your clojure project with ease.

Installation

The first thing to do is to download the self-install script from the github repository:

cd ~/bin
wget http://github.com/technomancy/leiningen/raw/stable/bin/lein
chmod +x lein

where ~/bin is a directory present in your PATH (if not, you have to edit your .bash_profile or .bashrc properly).
Now you are ready to do a self-install:

lein self-install

The self-install script will download for you all the necessary stuff to start building your project.

Creating a Project

Creating a project with Leiningen is pretty easy, just type:

lein new helloworld
Created new project in: helloworld

And leiningen will create the whole project structure for you:

.
|-- project.clj
|-- README
|-- src
| `-- helloworld
| `-- core.clj
`-- test
`-- helloworld
`-- test
`-- core.clj

Two files you must be aware of:

  • project.clj, the most important configuration file
  • core.clj, where we’ll write our code
  • Now we’ll get our hand dirty writing a few lines of code, so open project.clj and you’ll see something like that:

    (defproject helloworld "1.0.0-SNAPSHOT"
    :description "FIXME: write"
    :dependencies [[org.clojure/clojure "1.2.0"]
    [org.clojure/clojure-contrib "1.2.0"]])

    Even if you are a clojure newbie the code is straighforward, clojure 1.2.0 and clojure-contrib 1.2.0 are the only two dependencies of our project. Let’s define where to search for the main class:

    (defproject helloworld "1.0.0-SNAPSHOT"
    :description "FIXME: write"
    :dependencies [[org.clojure/clojure "1.2.0"]
    [org.clojure/clojure-contrib "1.2.0"]]
    :main helloworld.core)

    helloworld.core is the namespace where to search for the main file.
    Now open your core.clj:

    (ns helloworld.core)

    It’s a bit laconic, isn’t it? Let’s add a simple main class:

    (ns helloworld.core
    (:gen-class ))

    (defn -main [& args]
    (println "Hello world!"))

    Now we are ready to compile our project, so go into the project root dir (i.e. helloworld, not the deepest one) and type:

    lein compile
    Copying 2 files to /home/alfredo/Programming/Clojure/Projects/helloworld/lib
    Compiling helloworld.core

    Finally build our standalone jar:

    lein uberjar

    Leiningen will download all the dependencies for us, this mean that you don’t need to have any clojure version installed because Leiningen will download it for you in every single project!
    The last final test:

    java -jar helloworld-1.0.0-SNAPSHOT-standalone.jar
    Hello world!

    And then you go!
    I hope you have found this post useful!
    Stay tuned,
    Alfredo

    One thought on “Introduction to Leiningen

    1. Pingback: Setting up your environment to work with Clojure « Web life between Python and lambda calculus

    Rispondi

    Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

    Logo di WordPress.com

    Stai commentando usando il tuo account WordPress.com. Chiudi sessione /  Modifica )

    Foto Twitter

    Stai commentando usando il tuo account Twitter. Chiudi sessione /  Modifica )

    Foto di Facebook

    Stai commentando usando il tuo account Facebook. Chiudi sessione /  Modifica )

    Connessione a %s...