Get list of embedded resources in uberjar
Asked Answered
T

1

5

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?

Tallulah answered 12/3, 2014 at 20:25 Comment(0)
F
10

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))
Fatherless answered 12/3, 2014 at 21:0 Comment(1)
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.