Data Sorcery with Clojure

Building Incanter applications with Leiningen and Clojars.org

November 20, 2009 · 17 Comments

[NOTE: A recent update of Java on OS X prevents Leiningen from successfully building this example on Macs. The error is 'Cannot load com.apple.laf.AquaLookAndFeel'. David Nolen has fixed this problem in his fork on Leiningen, which can be found here. Hopefully this will be included in the stable branch of Leiningen soon.]


Technomancy’s Leiningen build tool and Alex Osborne’s Clojars.org repository are very cool.

I’ve uploaded Incanter and it’s dependencies to Clojars.org, and this is a quick guide to using Leiningen to build applications that use Incanter, based on the very useful post by Zef Hemel, “Building Clojure Projects with Leiningen“.

(Note: the following tutorial uses Unix shell commands (i.e. Mac OS X or Linux), but Roland Sadowski has created a Leiningen PowerShell script for Windows that works with this example: lein.ps1)

First get a copy of Leiningen’s self-installing script:

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

and run the self-installer.

$ lein self-install

Now create a project directory called ‘incanter-helloword’ and give it a ’src’ subdirectory.

$ mkdir incanter-helloworld
$ mkdir incanter-helloworld/src
$ cd incanter-helloworld

Next create the following project.clj file, which includes Incanter in the dependencies section:

(defproject incanter-helloworld "0.1.0"
  :description "The Incanter version of hello world."
  :dependencies [[incanter/incanter "1.0-master-SNAPSHOT"]]
  :main hello)

The project will have a single source file called hello.clj, which will contain a -main function that will be called when the jar file is executed. Create it in the incanter-helloworld/src directory:

(ns hello
  (:gen-class)
  (:use (incanter core stats charts)))

(defn -main [& args]
  (view (histogram (sample-normal 1000))))

The program will display a histogram of sample data from a standard normal distribution.

Now download and install all the project’s dependencies, including Incanter, using Leiningen’s ‘deps’ command:

$ lein deps

Compile the project:

$ lein compile

And build an ‘uberjar’ that includes all the Incanter jar files:

$ lein uberjar

And finally, run incanter-helloworld,

$ java -jar incanter-helloworld-standalone.jar

which displays the following histogram.

You can also start a Clojure REPL with a CLASSPATH that includes all your project’s dependencies, including Incanter and its dependencies, using the following Leiningen command:

$ lein repl

I think Leiningen and Clojars.org are already incredibly useful, despite the fact that they are both brand new projects, and I expect they will get even better with time.

Categories: Incanter · Leiningen · clojars.org

17 responses so far ↓

  • Bjorn Arild Maland // November 20, 2009 at 10:09 pm | Reply

    This is really slick – thanks for the example!

  • Failed for me // December 5, 2009 at 1:19 am | Reply

    1 required artifact is missing.

    for artifact:
    org.apache.maven:super-pom:jar:2.0

    from the specified remote repositories:
    clojars (http://clojars.org/repo/),
    clojure-snapshots (http://build.clojure.org/snapshots),
    central (http://repo1.maven.org/maven2)

    • liebke // December 5, 2009 at 1:44 am | Reply

      What part of the build did this error message appear? I can’t reproduce it.

      I have added the above simple project example to the Github repository under examples/blog/projects/incanter-helloworld. Follow the instructions in the README file in the project directory and let me know if this error occurs again.

      David

    • Ben // December 10, 2009 at 11:11 pm | Reply

      I think this may be because clojure-alpha is now clojure-master…

      • liebke // December 11, 2009 at 12:04 am

        Good catch! I actually think the original problem occurred before the renaming, but now there is a new problem. I think the renaming is going to cause problems for every project on Clojars that depends on clojure-alpha until new poms can be uploaded.

        I just sent an email to the Clojure list to see if we can have the old name linked to clojure-master as a short term fix.

  • Failed for me // December 5, 2009 at 1:47 am | Reply

    Thanks! I’ll try again. The command was “lein deps”

    I’m new to java so the complexity of build and deployment is kind of daunting.

    • liebke // December 5, 2009 at 1:57 am | Reply

      I’ve forgotten to do ‘lein deps’ or the ‘ant deps’ more than once myself :)

      JVM builds and deployments can be daunting, but Leiningen has been a great improvement. Good luck.

  • Kevin // December 14, 2009 at 4:19 am | Reply

    This failed for me on OSX Snow Leopard. I pulled the example code from the incanter github @ examples/blog/projects/incanter-helloworld. The same error occurred.

    lein deps works fine.

    > lein compile
    Downloading… (x3)
    Compiling hello
    [null] java.lang.Error: Cannot load com.apple.laf.AquaLookAndFeel (hello.clj:1)

    • liebke // December 14, 2009 at 11:38 am | Reply

      Yep, that’s a recently introduced bug with Leiningen that, I think, was caused by the recent Java update on Mac OS X. The Leiningen community is aware of it, and hopefully will have a fix out soon.

      Here’s a thread on the Leiningen Google group discussing the bug, and some possible fixes, in the context of Swank.

  • Chris Brew // January 8, 2010 at 3:14 pm | Reply

    Works beautifully on Leopard, if you

    - correct a formatting error that puts & in the source code when & would be better
    - follow the HACKING guideline for the MacOSX Leiningen fork

  • Felix // February 5, 2010 at 9:53 pm | Reply

    When I try the above, lein uberjar fails with

    “Wrong number of arguments to task uberjar.”

    What additional arguments does uberjar need? How can I fix this? I am using leiningen 1.0.1 on Windows.

    • liebke // February 5, 2010 at 10:00 pm | Reply

      Are you doing the following lein commands:
      $ lein deps
      $ lein compile
      $ lein uberjar
      $ java -jar incanter-helloworld-standalone.jar

      I didn’t get any errors.

      • Felix // February 5, 2010 at 10:12 pm

        Yes. lein deps and lein compile went fine. lein uberjar and lein jar both complain about a wrong number of arguments. Interestingly an incanter-helloworld.jar is created nonetheless. But this jar contains only META-INF and nothing else.

        I have to add that I am not very experienced in building Java applications, so may be I am missing something… Could this be a Windows issue?

    • liebke // February 5, 2010 at 10:37 pm | Reply

      I’m trying to reproduce your error, but I’m not having any luck. Can you post the exact command and error message?

      Although, I am running into problems when I try to run the app, after creating the uberjar. Leiningen has been undergoing a lot of changes lately, and that may be the cause of both of our problems.

      • Felix // February 5, 2010 at 10:50 pm

        Thanks for your help!

        e:\Users\felix\Documents\Clojure>mkdir incanter-helloworld
        mkdir incanter-helloworld

        e:\Users\felix\Documents\Clojure>cd incanter-helloworld
        cd incanter-helloworld

        e:\Users\felix\Documents\Clojure\incanter-helloworld>mkdir src
        mkdir src

        I now created project.clj and src/hello.clj exactly described in the blog post. Then:

        e:\Users\felix\Documents\Clojure\incanter-helloworld>lein deps
        lein deps
        [copy] Copying 17 files to e:\Users\felix\Documents\Clojure\incanter-helloworld\lib

        e:\Users\felix\Documents\Clojure\incanter-helloworld>lein compile
        lein compile
        Compiling hello

        e:\Users\felix\Documents\Clojure\incanter-helloworld>lein uberjar
        lein uberjar
        Wrong number of arguments to task uberjar.

    • liebke // February 5, 2010 at 11:00 pm | Reply

      Interesting, it looks like you’re on Windows, are you using the lein Bash script with Cygwin, or are you using the Python or PowerShell versions of it?

      Have you tried the lein example at Zef’s site (http://zef.me/2470/building-clojure-projects-with-leiningen), it is even simpler and may help eliminate variables to debug.

      If you can’t run that example, I would try to use the latest version of lein from github (it takes a bit more work to set up than the stable version, which can use self-install).

      If that doesn’t help, the best place to get Leiningen help is its Google Group: http://groups.google.com/group/leiningen

      David

Leave a Comment