Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get list of embedded resources in uberjar

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?

like image 702
WesleyJohnson Avatar asked Dec 26 '22 12:12

WesleyJohnson


1 Answers

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))
like image 81
juan.facorro Avatar answered Jan 05 '23 10:01

juan.facorro