Unk*_*own 5 clojure leiningen clojurescript
我在cljc文件中有以下代码:
(ns proj
#?(:cljs
(:require-macros
[proj :refer [define-project-version]])))
(declare project-version)
#?(:clj
(defmacro define-project-version []
`(def ~'project-version ~(first (drop 2 (read-string (slurp "project.clj")))))))
(define-project-version)
Run Code Online (Sandbox Code Playgroud)
当在clj文件中使用和启动REPL时,这确实有效.但是,当我制作一个uberjar并尝试运行它时,我得到一个关于project.clj的例外 - "没有这样的文件或目录." 错误来自ClojureScript部分.uberjar编译得很好.
为什么代码试图加载project.clj?是不是应该在编译时运行宏?
To access files from a Clojure application in an Uberjar, the files should be places in a resources directory that is on the classpath. In a typical Leiningen project, this would be resources in the root of the project.
A different path can be explicitly defined by adding :resource-paths ["src/main/resource"] to the project.clj project configuration file.
Files can then be accessed using the clojure.java.io/resource function, for example a file located at resources/hello.txt
; Use clojure.java.io/resource to read resources from the classpath:
(ns rescue.core
(:require [clojure.java.io :as io] ))
(def data-file (io/resource
"hello.txt" ))
(defn -main []
(println (slurp data-file)) )
Run Code Online (Sandbox Code Playgroud)
This can be checked opening a terminal in the root of the project and running the command java -jar uberjar-name.jar