I don't have a good example, but the right way to do is with resources
which are basically just files that live on the classpath:
* Put the files in a folder on your classpath. If your using
leiningen, the resources/ directory does this by default.
* Get a URL to the file with clojure.java.io/resource. If your file is
root/resources/my-project/my-resource.txt, you'd use (resource
"my-project/my-resource.txt").
* Read the contents of the file by passing the resource URL to
clojure.java.io/reader or one of its friends.
When you jar up your app the resource files will be included in the
jar and "just work".
Hope this helps.
Dave
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clo...@googlegroups.com
> Note that posts from new members are moderated - please be patient with your first post.
> To unsubscribe from this group, send email to
> clojure+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
One idiomatic way to do this in Clojure is:
- store the files within a directory named resources at the top level of your project folder,
- arrange for that folder to be in your classpath at runtime,
- obtain a reference to the files at runtime using .getResource with a relative path.
If you use lieningen, the resources folder will be in your class path automatically and the files/directories it contains will be copied to the top level of your jar file if you make one.
Here's an example:
(defn resource [path]
(when path
(-> (Thread/currentThread) .getContextClassLoader (.getResource path))))
(require '[clojure.java.io :as io])
(slurp (io/file (resource "js/boo.js")))
I did this in a leiningen project called scratch. It returned the contents of the file
scratch/resources/js/boo.js
because scratch/resources was on the classpath.
See also http://alexott.net/en/clojure/ClojureLein.html .
--Steve