How to use a dependency from clojure repl without starting a lein project?

You don’t really need any plugins to do this, this is the syntax for adding a dependency from the shell

lein update-in :dependencies conj '[org.clojure/math.combinatorics "0.1.4"]' -- repl

Lately I’ve started adding a add-dependency helper based on Pomegranate to user.clj in my projects, so it’s easy to load an extra dependency to try out without restarting your REPL. You could make this available to any lein repl by adding it to your ~/.lein/profiles.clj.

{:user {:dependencies
        [[com.cemerick/pomegranate "0.4.0"]]

        :incjections
        [(defn add-dependency [dep-vec]
           (require 'cemerick.pomegranate)
           ((resolve 'cemerick.pomegranate/add-dependencies)
            :coordinates [dep-vec]
            :repositories (merge @(resolve 'cemerick.pomegranate.aether/maven-central)
                                 {"clojars" "https://clojars.org/repo"})))]}}

It loads pomegranate upon first usage, so it doesn’t add to startup time.

% lein repl
nREPL server started on port 36519 on host 127.0.0.1 - nrepl://127.0.0.1:36519

user=> (add-dependency '[org.clojure/math.combinatorics "0.1.4"])
{[org.clojure/math.combinatorics "0.1.4"] #{[org.clojure/clojure "1.7.0"]}, [org.clojure/clojure "1.7.0"] nil}
user=> (require '[clojure.math.combinatorics :as comb])
nil
user=> (comb/combinations [1 2 3 4 5] 2)
((1 2) (1 3) (1 4) (1 5) (2 3) (2 4) (2 5) (3 4) (3 5) (4 5))

9 Likes