15

我正在使用 lein2。我想在 repl 启动时默认加载一些 ns 。当为该项目执行 lein2 repl 时,是否可以在 project.clj 中指定应该加载的 ns?

4

2 回答 2

13

你会在示例项目中找到很多答案

;; 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*)

使用project.clj我得心应手的:

(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"]])

当我点燃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=>
于 2012-09-08T13:23:51.747 回答
5

我有时会使用:injectionsin 选项project.clj 来加载命名空间。以下示例将 在执行命令foo.bar时加载命名空间:lein2

(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)])
于 2012-09-08T13:31:44.553 回答