Installing jar files locally for Leiningen 2

I use a fair number of libraries for my Clojure project that are not integrated into the Maven ecosystem by Clojars and Leiningen. For as long as I was using Leiningen 1, I was able to get around this using the hack of putting all my local/native libraries in the ‘libs’ directory. Leiningen 2 encourages standardizing on using the user’s local Maven repository, and eschewing the ‘libs’ hack is one part of that process. Not only is jumping on board with Leiningen v2 a good thing, but I needed v2 in order to open SWT windows in a REPL on Mac OS X for the first time ever. So the following are the instructions that I followed to ultimately get my local / native lib jars installed with my local Maven repo for Leiningen 2 to pick up automatically. Let me know if they work for you!

What almost worked

The following links almost worked for me, but not quite. I’m providing them for reference, and maybe they’ll work for you.

When I tried the commands listed in the above links, sometimes the jars would be copied into my local Maven repo in ~/.m2 according to the folder structure as indicated by the artifactId, groupId, and version, but if they did, Leiningen didn’t successfully pull them in to the project.

What worked for me

Ultimately, what worked for me a is a modification of the comment by Stuart Sierra in this Gist.

What it looks like, given the example of installing the Jar for Apache Commons Exec to the local Maven repo (commons-exec-1.1.jar), looks like this:

cd ~
mvn install:install-file -Durl=file:repo -DgroupId=local -DartifactId=commons-exec -Dversion=1.1 -Dpackaging=jar -Dfile=path/to/commons-exec-1.1.jar

Afterwards, the file is installed at

~/.m2/repository/local/commons-exec/1.1

In the Lein 2 project.clj, the :dependencies looks like:

:dependencies [[org.clojure/clojure "1.3.0"]
               ...
               [local/commons-exec "1.1"]
               ...
               ]

And the :repositories in project.clj looks like

    :repositories {"project" "file:repo"}

You could improve on this example by using “apache” as the groupId so that the deps key-val pair in the lein project.clj looks like [apache/commons-exec "1.1"].

My setup details: My computer is a Macbook Air running Mac OS X 10.7. My version of Maven comes from Macports. I upgraded my Leiningen v1 to v2 according to the instructions on the Leiningen website (but I originally installed v1 through Macports).

Let me know if this works for you and/or if there’s something I’m missing.

Update (5/15/13): If you do follow the steps above to install jar files locally in your Maven repository, in the mvn install:install-file command, I have found that a full path should be provided in the -Dfile= option. Using a relative path (Ex: ~user/…) will make the install command appear to work without the jar file actually being properly installed.
And, on another note, be sure to check Clojars the Maven Central Repository to see if the library (and version of it) you want exists before taking this manual approach to things. As a disclaimer, I plead ignorance of the Maven ecosystem… for now.

  • Phil

    Why are you using a local install of commons-exec 1.1 when it’s available from Central? http://search.maven.org/#artifactdetails%7Corg.apache.commons%7Ccommons-exec%7C1.1%7Cjar

    • Elango

      Oops, you’re right.  I never really learned Maven and wanted to minimize learning Maven to the parts I need for Leiningen & any other Clojure tools.  I mostly use the local install for OS-specific SWT libs, if that helps.

      I’ll go ahead and start learning Maven, though.  If there’s anything I can do to contribute documentation anywhere, let me know.

  • Christian Jauvin

    Thanks a lot! I wanted to use the latest Jena API with Clojure, and this has been very helpful.

  • Vladimir Sekissov

    Don’t forget to add POM file to command line if your jar dependends from other libs:

    -DpomFile=pom.xml

  • Pingback: Local dependencies in Leiningen without creating a Maven repo?

  • Henry

    Thanks so much! I was able to use this process to include the FTDI serial driver in a Clojure Android app. I was running into the same issues – other techniques ‘almost worked’, but this actually did the trick.

  • Dhaya

    From here: https://gist.github.com/stuartsierra/3062743

    To get the Lein 2 equivalent of @stuartsierra’s command above without using Maven:

    Add to project.clj:

    :repositories [["project" {:url "file:repo" username "" :password ""}]]

    Run lein deploy [repo] [artifact] [version] [jar-path]

    lein deploy project local/bar 1.0.0 bar.jar

    Then you can add the dependency to project.clj as:

    [local/bar "1.0.0"]