简体   繁体   中英

How can new clojure libraries be loaded in the repl

So I have this workflow problem:

I'm happily typing away on my clojure project repl and realise that i need another library that is not in my project.clj , say in this case, i needed the tools.cli library.

I open up project.clj in my editor and add in an entry to the :dependencies

     [org.clojure/tools.cli "0.2.1"]

Then, within the project directory, I type lein deps in the shell to pull in the necessary libraries

After the project dependencies are pulled, technically all the class files are already there ready to be loaded, but if I go back to my repl and type:

> (use 'tools.cli)

I get this:

=>> FileNotFoundException Could not locate tools/cli__init.class
  or tools/cli.clj on classpath:   clojure.lang.RT.load (RT.java:432)

So I would have to restart my repl, wasting a whole heap of time reconfiguring the state of the repl to where I was before I needed the library.

Is there a way to just load in the library dynamically? eg, after I run lein deps I just go back to the repl and type:

> (load-library "tools.cli") 
> (use 'tools.cli)

Thanks in advance

Pomegranate is for you:

https://github.com/cemerick/pomegranate

It supports download and addition of new dependencies at runtime, eg:

(add-dependencies :coordinates '[[incanter "1.2.3"]]
                  :repositories (merge cemerick.pomegranate.aether/maven-central
                                       {"clojars" "http://clojars.org/repo"}))

Will something like this work for you?

https://groups.google.com/d/msg/clojure/AJXqbpGMQw4/0-7-3pXRwGkJ

There is also clojure.core/add-classpath, but it's deprecated.

http://clojuredocs.org/clojure_core/clojure.core/add-classpath

You can try out one library in the repl using lein-try .

~/.lein/profiles.clj :

{:user {:plugins [[lein-try "0.4.3"]]}}

command line :

$ lein try clj-time "0.5.1"
Fetching dependencies... (takes a while the first time)
lein-try loaded [clj-time "0.5.1"]

nREPL server started on port 57036
REPL-y 0.2.0
Clojure 1.5.1
    Docs: (doc function-name-here)
          (find-doc "part-of-name-here")
  Source: (source function-name-here)
 Javadoc: (javadoc java-object-or-class-here)
    Exit: Control+D or (exit) or (quit)

user=>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM