Question

I used to like to include all of clojure.contrib, and require all the libraries. This makes find-doc useful as a discovery tool.

Nowadays (clojure 1.4) clojure.contrib is split into many sub-libraries. And that rather spoils my scheme, and it also means that I am constantly having to restart the JVM every time I need a new library.

So I'm busy constructing a project.clj file with many lines:

[org.clojure/algo.generic "0.0.6"]
....
[org.clojure/data.xml "0.0.4"]
....

So that I can get leiningen to put every clojure contrib library on the classpath, whether I need them or not.

And I reckon that this is going to be a spectacular pain in the neck, what with the version numbers, and all.

And I wonder if anyone has a better way to do the same thing?

EDIT: Thinking about it, if there's a web page somewhere that has a list of library names and current versions, I can turn that into a project file fairly easily.

Was it helpful?

Solution

You could use pomegranate if you just want to run it in the REPL (which seems like it would be the only appropriate use case, right?). You can have it look up the latest versions using the Maven Central API. I think this is better than maintaining some sort of dependencies project, generated or otherwise.

(require '[cemerick.pomegranate :refer [add-dependencies]])

(add-dependencies
  :coordinates '[[clj-http "0.5.8"]]
  :repositories {"clojars" "http://clojars.org/repo"})

(require '[clj-http.client :as client])

;; contrib project names from https://github.com/clojure
(def contrib ["tools.nrepl" "tools.trace" "tools.namespace" "tools.macro"
              "test.generative" "math.numeric-tower" "core.match" "core.logic"
              "data.priority-map" "core.contracts" "tools.cli" "java.jmx"
              "java.jdbc" "java.classpath" "data.xml" "data.json" "core.unify"
              "core.incubator" "core.cache" "algo.monads" "data.generators"
              "core.memoize" "math.combinatorics" "java.data" "tools.logging"
              "data.zip" "data.csv" "algo.generic" "data.codec"
              "data.finger-tree"])

(defn add-contrib-dependencies
  "look up the latest version of every contrib project in maven central,
   and add them as dependencies using pomegranate."
  [project-names]
  (add-dependencies
   :coordinates
   (map (juxt
         (comp symbol (partial format "org.clojure/%s"))
         (fn [proj]
             (Thread/sleep 100)
             (-> "http://search.maven.org/solrsearch/select?q=%s&rows=1&wt=json"
                 (format proj)
                 (client/get {:as :json})
                 :body :response :docs first :latestVersion)))
        project-names)))

Now you can just invoke this function on the list of project names:

user=> (add-contrib-dependencies contrib)
{[org.clojure/data.zip "0.1.1"] nil,
 [org.clojure/java.classpath "0.2.0"] nil,
 [org.clojure/core.cache "0.6.2"] nil, ...}

UPDATE: as suggested earlier, I had made this answer into a library. It can be used either as nREPL middleware or invoked manually from a running REPL session. The code can be found at https://github.com/rplevy/contrib-repl, where usage instructions can also be found.

OTHER TIPS

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top