clojure

How to require a .clj fle from another project on my local machine?


Let's say you create two projects with Leiningen (side by side in a folder called projects) with lein new ping and lein new pong. Then you implement a little function in ping\src\ping\core.clj:

(defn plus 
  [x y]
  (+ x y))

My question is: How can "import" that plus function in the REPL for the pong project? This does not work:

 (require '[ping.core :as ping]) 
 ; Execution error (FileNotFoundException) [...] 
 ; Could not locate ping/core__init.class, ping/core.clj or ping/core.cljc on classpath.

I modified the CLASSPATH environment variable (e.g., set CLASSPATH=..\ping\src\ping\core.clj) but, alas, to no avail.

Is it necessary to make a JAR file out of ping to use its functions in pong?


Solution

  • Leiningen has the concept of checkout dependencies, which let you use other "projects" as libraries without them having to be packaged as a .jar first.

    deps.edn has this a bit cleaner via :local/root dependencies.

    Both options are much more reliable then manually messing with the CLASSPATH env variable. I'm not even sure lein even respects that.