I am using lein2. I would like to load some ns by default when the repl starts. Is it possible to either specify in project.clj the ns that should be loaded, when lein2 repl is executed for that project?
Asked
Active
Viewed 3,607 times
2 Answers
13
You will find a lot of answers in the sample project
;; Options to change the way the REPL behaves
:repl-options {;; Specify the string to print when prompting for input.
;; defaults to something like (fn [ns] (str *ns* "=> "))
:prompt (fn [ns] (str "your command for <" ns ">, master? " ))
;; Specify the ns to start the REPL in (overrides :main in
;; this case only)
:init-ns foo.bar
;; This expression will run when first opening a REPL, in the
;; namespace from :init-ns or :main if specified
;; This expression will run when first opening a REPL, in the
;; namespace from :init-ns or :main if specified
:init (println "here we are in" *ns*)
Using a project.clj
I had handy:
(defproject test "1.0.0"
:repl-options { :init-ns test.core
:init (do
(use 'clojure.set)
(println (union #{1 2 3} #{3 4 5}))
(use 'incanter.core)
(println (factorial 5))) }
:dependencies [[org.clojure/clojure "1.4.0"]
[incanter/incanter-core "1.3.0-SNAPSHOT"]])
When I fire up lein repl
$ lein repl
nREPL server started on port 1121
REPL-y 0.1.0-beta10
Clojure 1.4.0
Exit: Control+D or (exit) or (quit)
Commands: (user/help)
Docs: (doc function-name-here)
(find-doc "part-of-name-here")
Source: (source function-name-here)
(user/sourcery function-name-here)
Javadoc: (javadoc java-object-or-class-here)
Examples from clojuredocs.org: [clojuredocs or cdoc]
(user/clojuredocs name-here)
(user/clojuredocs "ns-here" "name-here")
#{1 2 3 4 5}
120.0
test.core=>

noahlz
- 10,202
- 7
- 56
- 75
-
Thanks @noahz. I want to be able to specify the following where the 3 statements in the fn are what I want to execute, how do I do that - :repl-options { :init (fn [_] (use 'ring.util.serve) (use 'mfaiz.routes) (serve mfaiz.routes/my-app))} – murtaza52 Sep 08 '12 at 19:00
-
1You are just defining a function, so add a second set of parenthesis and the desired parameter to call it. `((fn [_] (println _) "foo")` – noahlz Sep 08 '12 at 19:19
-
Did that and this is the error it gives me - CompilerException java.lang.RuntimeException: Unable to resolve symbol: serve in this context, compiling:(NO_SOURCE_PATH:1) – murtaza52 Sep 09 '12 at 01:58
-
1Updated my answer demonstrating that the solution I provided works. Seems that you have moved on to a new problem. Perhaps open a new question and post more of your project.clj? – noahlz Sep 09 '12 at 12:08
-
Thanks @noahz ! Appreciate your help. I hope there was a way to give assign more points to a helpful answer ! – murtaza52 Sep 09 '12 at 14:27
-
Sure, doing that. Also is there a way to make a file or ns automatically reload in the repl on save. Currently I reload it using (use 'sample.ns :reload-all). But can I have it reload it automatically on save ? – murtaza52 Sep 09 '12 at 14:37
-
Please open a new question for that topic. – noahlz Sep 09 '12 at 14:42
-
Sure, opened a new one - http://stackoverflow.com/questions/12340931/how-to-hot-reload-a-namespace-in-repl-on-file-save – murtaza52 Sep 09 '12 at 16:48
5
I sometimes use the :injections
option in project.clj
to load namespaces. The following example will load the
foo.bar
namespace when the lein2
command is executed:
(defproject org.example/sample "0.1.0-SNAPSHOT"
:description "A sample project"
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:injections [(use 'foo.bar)])

tnoda
- 1,534
- 9
- 18