6
votes

I'm learning Clojure, but I'm not really building whole projects for each little code snippet, I just drop them into a REPL. Occasionally code snippets I'm exploring require a dependency (usually something that is/was in clojure.contrib).

The only way I know how to get those dependencies onto my computer is to have an empty leiningen project, add the dependency to project.clj and run lein deps.

Is there any way I can download libraries globally, outside of a project? If it's that I really really don't want to, why?

3
1) download library jar 2) java -cp clojure.jar:some-lib-clj.jar clojure.main 3) What's so bad about having a scratch Leiningen project? - noahlz
it's not exactly what you want I think but maybe this will make it easier, github.com/mtyaka/lein-oneoff - georgek
Seems to me that this is exactly what he wants. - noahlz

3 Answers

2
votes

I have a small project that I use for testing code snippets and answering SO questions, and am also constantly adding dependencies. The project.clj for this project includes Pomegranate as a dependency which then makes dynamically loading other dependencies as easy as:

(use '[cemerick.pomegranate :only (add-dependencies)])
(add-dependencies :coordinates '[[my-dependency "1.2.3"]])
2
votes

Give lein-try a go. It's a leiningen plugin I wrote that lets you say something like lein try [my-dependency 1.0.0] or even lein try my-dependency at the command line and drop into a REPL with the dependency available.

1
votes

If you are using lein-exec as your way of running one-off scripts, you can now use a little snippet at the top of the script. Add:

(use '[leiningen.exec :only (deps)])
(deps '[[clj-time "0.8.0"]])

to the top of your clj. Now running lein exec [example.clj] will automatically pull down the requirement.

If you are new to lein exec, just add {:user {:plugins [[lein-exec "0.3.4"]]}} to your ~/.lein/profiles.clj and you can begin running lein exec on your clj files. It is a great and quick way to run code without a project.