> I'd like to bundle a collection of (JSON) datafiles with a Clojure
> project source tree so that Clojure functions can reliably find and
> open those datafiles.
> 
> What's the idiomatic way of going about this?

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

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@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+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to