如何为lein run和lein repl定义project.clj工作?

时间:2013-03-11 12:50:48

标签: clojure

我是Clojure的新手,我不太清楚如何编写我的project.clj,因此它适用于lein repllein run。这是(整个路径:~/my-project/project.clj):

(defproject my-project "1.0.0-SNAPSHOT"
  :description "FIXME: write description"
  :dependencies [[org.clojure/clojure "1.3.0"]]
  :main my-project.core/hello
)

然后我有我的~/my-project/src/my_project/core.clj文件

(ns my-project.core)

(defn hello []
  (println "Hello world!")
)

lein run工作得很好,但在运行FileNotFoundException时我得到lein repl

~/my-project$ lein run
Hello world!
~/my-project$ lein repl
REPL started; server listening on localhost port 42144
FileNotFoundException Could not locate hello__init.class or hello.clj on classpath:   clojure.lang.RT.load (RT.java:430)
clojure.core=>

如何编辑project.clj来解决此问题?或者我是否必须以不同的方式致电lein repl

提前致谢。

编辑:尝试使用lein deplein compile,但仍然出现同样的错误

~/my-project$ lein version
Leiningen 1.7.1 on Java 1.6.0_27 OpenJDK Client VM
~/my-project$ lein deps
Copying 1 file to /home/yasin/Programming/Clojure/my-project/lib
~/my-project$ lein compile
No namespaces to :aot compile listed in project.clj.
~/my-project$ lein repl
REPL started; server listening on localhost port 41945
FileNotFoundException Could not locate hello__init.class or hello.clj on classpath:   clojure.lang.RT.load (RT.java:430)

1 个答案:

答案 0 :(得分:14)

要使其发挥作用,您可以做的一件事就是将core.clj更改为:

(ns my-project.core
  (:gen-class))

(defn hello []
  (println "Hello world!"))

(defn -main []
  (hello))

并将project.clj编辑为:

(defproject my-project "1.0.0-SNAPSHOT"
  :description "FIXME: write description"
  :dependencies [[org.clojure/clojure "1.3.0"]]
  :main my-project.core)

(:gen-class)将告诉编译器为命名空间生成Java类,:main中的project.clj指令将告诉lein run运行主方法class,由-main给出。为什么lein repl找不到my-project.core/hello的原因我不清楚,但我对leiningen的内部结构并不了解。