10

Is it possible to download and install previously unspecified Maven dependencies in a running Clojure REPL?

I'm thinking of the fairly common case where you want to quickly pull in a dependency temporarily for some testing or visualisation tools, but don't want to close down your current REPL session.

For example if you wanted to pull in Incanter to draw some pretty charts of data in your current REPL session, you might use something like the following:

(load-dependency "incanter" "incanter" "1.3.0-SNAPSHOT")

;; now do stuff with Incanter......

Presumably you would have to trigger the dependency resoltion / loading in Maven or Leiningen from an appropriate repository but I don't know enough about their internals to know whether this is possible or not at runtime.....

mikera
  • 105,238
  • 25
  • 256
  • 415
  • 1
    There's another way to do this in the works: http://insideclojure.org/2018/05/04/add-lib/ As of 2019-06-07 it's in a branch that hasn't been merged yet (https://github.com/clojure/tools.deps.alpha/compare/add-lib). – bmaddy Jun 07 '19 at 15:53

2 Answers2

6

Alembic is a leiningen plugin that adds this functionality to the repl.

Direct quote from the README:

Alembic is a clojure library that allows you to distill jars onto your classpath in a running JVM instance. You can use it to add dependencies to a running REPL, either in an ad-hoc fashion, or by reloading your project.clj file.

It also adds a (load-project) function that parses the project.clj and adds missing dependencies on the fly.

Community
  • 1
  • 1
clows
  • 332
  • 5
  • 11
6

Java's default classloader behavior makes this difficult, but pomegrenade (actually, pomegranate) claims to be able to do what you want.

Joost Diepenmaat
  • 17,633
  • 3
  • 44
  • 53
  • awesome - this looks like it fits the bill perfectly! amusingly enough the Incanter example is the same one as used in the pomegrenade documentation :-) – mikera Oct 10 '11 at 10:46
  • @mikera You were definitely in luck, this library was released about 5 days ago http://disclojure.org/2011/10/05/today-in-the-intertweets-oct-5th-ed-3/. – ponzao Oct 10 '11 at 14:07
  • I notice that the URL is spelled correctly (pomegranate), but the link is a rather comical pomegrenade. I wonder, is this some kind of weird grenade-oriented joke that I'm not getting? – amalloy Oct 10 '11 at 20:54
  • haha. I think that was my misspelling. A silly mistake on my part. Though granate and grenade are the same word; just that the first is French and the second is English (pommegranate transliterates as grenade apple). I put an annotation in the original post to rectify my mistake – Joost Diepenmaat Oct 10 '11 at 21:05
  • A little plug here, if you want to use a fully clojure based dependency system, github.com/bmillare/dj supports runtime loading dependencies. Also, supports dynamic loading of native dependencies. – bmillare Oct 10 '11 at 22:26