Clojure 如何使用leningen将.clj文件作为脚本运行?
这是第二个问题 例如,我有一个文件Clojure 如何使用leningen将.clj文件作为脚本运行?,clojure,leiningen,Clojure,Leiningen,这是第二个问题 例如,我有一个文件hello\u world.clj,我可以使用 java-cp clojure.jar clojure.main hello\u world.clj 既然lein已经包含Clojure(因为我可以直接运行lein-repl),有没有类似的方法 lein script hello\u world.cljlein?leiningen可以为您创建一个具有所有依赖项的“uberjar” lein uberjar 将在目标子目录中为您创建一个jar。jar将包含proj
hello\u world.clj
,我可以使用
java-cp clojure.jar clojure.main hello\u world.clj
既然lein已经包含Clojure(因为我可以直接运行lein-repl
),有没有类似的方法
lein script hello\u world.clj
lein?leiningen可以为您创建一个具有所有依赖项的“uberjar”
lein uberjar
将在目标子目录中为您创建一个jar。jar将包含project.clj
中列出的所有依赖项,因此您无需担心构造复杂的类路径来调用代码
您可以在java类路径中以正常方式引用这个uberjar作为单个条目,或者在project.clj
中指定一个主类,将其作为可执行jar调用
e、 g.aproject.clj
如下:
(defproject clj-scratch "1.0.0-SNAPSHOT"
:description "FIXME: write description"
:dependencies [[org.clojure/clojure "1.4.0"]
:main clj-scratch.core)
将调用clj-scratch.core命名空间中的-main
函数
如果您运行:
java -jar target/clj-scratch-1.0.0-SNAPSHOT-standalone.jar
使用插件,例如readme.md(更新为“lein”而不是“lein2”)
使用lein repl
有几种方法:
- 非常慢:
cat your_file.clj|lein repl
- 慢速:
echo'(加载文件“your_file.clj”)| lein repl
- 快速:
lein repl
(加载文件“your_file.clj”)
- 重复
2
我喜欢用它
#!/usr/bin/env inlein
'{:dependencies [[org.clojure/clojure "1.8.0"]
[com.hypirion/primes "0.2.1"]]}
(require '[com.hypirion.primes :as p])
(when-not (first *command-line-args*)
(println "Usage:" (System/getProperty "$0") "prime-number")
(System/exit 1))
(-> (first *command-line-args*)
(Long/parseLong)
(p/get)
println)
然后只需chmod+xscript.clj
并运行它
或者,您也可以在这里inlein script.clj my args
。您是说lein run吗@georgek可能不是因为lein run
需要创建的项目。clj
创建的“快速”项目不适合我。不得不改为使用“(clojure.main/load-script“test.clj”)”
#!/usr/bin/env inlein
'{:dependencies [[org.clojure/clojure "1.8.0"]
[com.hypirion/primes "0.2.1"]]}
(require '[com.hypirion.primes :as p])
(when-not (first *command-line-args*)
(println "Usage:" (System/getProperty "$0") "prime-number")
(System/exit 1))
(-> (first *command-line-args*)
(Long/parseLong)
(p/get)
println)