Search This Blog

Saturday, February 2, 2013

Runtime Require: Downloading a New Dependency and Adding it to the Classpath at Runtime with Pomegranate

Everything that I don't like about Clojure is really something I don't like about the JVM. Here is one tiny blow for freedom:



;; How to pull in a library at runtime

;; dependencies [[org.clojure/clojure "1.4.0"]
;;               [com.cemerick/pomegranate "0.0.13"]]


;; When working at the REPL, it's often a pain to stop clojure, add a library to your dependencies, and restart.

;; Behold the solution, due to Chas Emerick (on whom blessings)

(require 'cemerick.pomegranate)

;; We don't, I think, even need to download the library. Pomegranate will go off and get it if it needs to:

(cemerick.pomegranate/add-dependencies 
 :coordinates '[[ring "1.1.7"]]
 :repositories {"clojars" "http://clojars.org/repo" } )

;; It takes a while, but once it's done :

(require 'ring.adapter.jetty)

(defonce server (ring.adapter.jetty/run-jetty (fn[x]{:body"<h1>yo</h1>"}) {:port 8080 :join? false}))

;; And it works!

;; I am told that there are reasons not to use this in production
;; code, only at the REPL, which is a terrible shame, since otherwise
;; programs could specify their own dependencies and versions rather
;; than having to exile them to a separate file.

;; Still, this is a huge time-saver, and takes a lot of the pain out of trying a new library.

3 comments:

  1. I have no idea what the implicit complaint has to do with the JVM.

    ReplyDelete
    Replies
    1. Fixed classpath on startup so you can't load a new library. Obviously this isn't actually true, since you can, but it doesn't make it easy. Compare 'import lib' in python.

      Delete
  2. From my ~/.lein/init.clj: http://pastebin.com/jU59t8vQ

    Marek

    ReplyDelete

Followers