How are `user.clj` files loaded?

Thanks for the explanation, Alex. Knowing that, I found a way to load multiple user.clj files:

  • ~/.clojure/deps.edn:
{:aliases {:user {:extra-paths ["/path/to/.clojure"]}}}
  • ~/.clojure/user.clj:
(defn- static-classpath-dirs
  []
  (mapv #(.getCanonicalPath  %) (classpath/classpath-directories)))

(defn user-clj-paths
  []
  (->> (static-classpath-dirs)
    (map #(io/file % "user.clj"))
    (filter #(.exists %))))

(defn load-user!
  [f]
  (try
    (prn (str "Loading " f))
    (load-file (str f))
    (catch Exception e
      (binding [*out* *err*]
        (printf "WARNING: Exception while loading %s\n" f)
        (prn e)))))

(defn load-all-user!
  []
  (let [paths (user-clj-paths)]
    (prn (str "Load " (first paths)))
    (doall
      (map load-user! (rest paths)))))

(load-all-user!)

Now I can start clojure from the command line with clj -A:user:other-alias. My ~/.clojure/user.clj is the first in the classpath, and will take care of loading all the other user.clj on the classpath

3 Likes