Question

I'm working with leiningen and Clojure to create an uberjar so I have a single file for deployment. I'm also embedding some static files using the default resource paths for leiningen, the names and number of which will vary from build to build. Is there an idiomatic way to get a list of embedded resources from Clojure? I know I can use clojure.java.io/resource to access a known file name, but how can I get a list of files?

Was it helpful?

Solution

The following list-resources function returns a list of all files in the jar that's in path. running-jar holds the path to the currently running jar if it is in fact a jar.

(def ^:private running-jar 
  "Resolves the path to the current running jar file."
  (-> :keyword class (.. getProtectionDomain getCodeSource getLocation getPath)))

(defn list-resources [path]
  (let [jar (java.util.jar.JarFile. path)  
        entries (.entries jar)]
    (loop [result  []]
      (if (.hasMoreElements entries)
        (recur (conj result (.. entries nextElement getName)))
        result))))

(take 10 (list-resources running-jar))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top