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?
Get list of embedded resources in uberjar
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))
Excellent, thank you. We ended up scrapping the idea entirely and doing something without embedded resources. That said, I tested this beforehand and it worked out perfectly. Thanks again. –
Tallulah
© 2022 - 2024 — McMap. All rights reserved.