-
Create new project
lein new app [appname]
-
(optional) Search for a module you want to add to your project
lein search [modulename]
-
Reference the module in project.clj under the
:dependencies
by its identifier which is given in the form of[group-id/artifact-id version-string]
. In the following example we add the[org.clojure/data.json "0.2.5"]
dependency.:dependencies [[org.clojure/clojure "1.6.0"] [org.clojure/data.json "0.2.5"]]
-
(optional) You can make lein explicitly download the dependencies by
lein deps
Next time you do a lein repl
or M-x cider-jack-in
lein will download [org.clojure/data.json "0.2.5"]
and start the java REPL with [org.clojure/data.json "0.2.5"]
loaded.
###Usage from REPL
When you start lein REPL from the project directory lein will then look for the :dependencies
key in project.clj
, sees [org.clojure/data.json "0.2.5"]
and appends the path for the [org.clojure/data.json "0.2.5"]
library to the classpath when starting the java process for the REPL.
- Start the REPL
lein repl
or in emacs: M-x cider-jack-in
-
Load the namespace of the dependency in lein repl by referencing the namespace of the library. here the the namespace for
[org.clojure/data.json "0.2.5"]
isclojure.data.json
.(require '[clojure.data.json])
Now it is possible to use functions from
clojure.data.json
:(clojure.data.json/read-str "{\"a\": [1,2,3]}") => {"a" [1 2 3]}
Alternatively you can alias the namespace
(require '[clojure.data.json :as json])
Usage:
(json/read-str "{\"a\": [1,2,3]}") => {"a" [1 2 3]}
-
At the top of the clj file, you have a namespace specification like (ns myproj.core) add to this namespace the
:require
key to reference a namespace.(ns myproj.core (:require [clojure.data.json]))
Alternatively you can use a shorter name.
```clojure
(ns myproj.core
(:require [clojure.data.json :as json]))
```
- http://blog.8thlight.com/colin-jones/2010/12/05/clojure-libs-and-namespaces-require-use-import-and-ns.html
- http://www.unexpected-vortices.com/clojure/brief-beginners-guide/libs-management-and-use.html
- http://blog.8thlight.com/colin-jones/2010/11/26/a-leiningen-tutorial.html
- http://stackoverflow.com/questions/9810841/clojure-loading-dependencies-at-the-repl
- http://stackoverflow.com/questions/7658981/how-to-reload-a-clojure-file-in-repl
- https://github.com/technomancy/leiningen/blob/stable/doc/TUTORIAL.md