How to add directory to Clojure's classpath?

Viewed 19288

I have installed the libraries with Maven to the ~/.m2/repository/ directory. I would like to add that path to the default Clojure classpath. I could not find the documentation how to do that.

Any hints?

Cheers!

clj
Clojure 1.4.0
user=> (require '[clojure.java.jmx :as jmx])
FileNotFoundException Could not locate clojure/java/jmx__init.class or clojure/java/jmx.clj on classpath:   clojure.lang.RT.load (RT.java:432)

The class path by default is:

user=> (println (seq (.getURLs (java.lang.ClassLoader/getSystemClassLoader))))
(#<URL file:/Users/myuser/cljmx/> #<URL file:/usr/local/Cellar/clojure/1.4.0/clojure-1.4.0.jar> #<URL file:/Users/myuser/cljmx/>)
nil
5 Answers

Another option is to create a deps.edn file in the folder where you plan to run the REPL or where you keep your project files.

This file is used to inform Clojure about dependencies, source files, execution profiles, etc… It's loaded when you run a REPL (but there are other use cases) and it's supported by the core of Clojure and it's officially documented on https://clojure.org/reference/deps_and_cli

In your case you may just want to put something like the following, to declare what dependencies you want to download and put on the Java classpath.

{
   :deps {
      the.dependency/you-want {:mvn/version "1.0.0"}
   }
}

In deps.edn you can specify:

  • third party dependencies (eg JARs) that can be already saved locally, or hosted on a Maven repository, or on Git repository…
  • source paths, where your source code resides, if any

Note that the dependencies will be downloaded and cached in .cpcache/ folder, beside the deps.edn itself. I'm not sure if you can instruct it to use the global ~/.m2 instead.

You may find the dependency coordinates (name and latest version) on clojars.org

deps.edn is "lighter", part of the core Clojure, if less powerful than leiningen; so maybe suited for setting up an environment for casual/exploratory coding at the REPL or CLI.

You can also have a global deps.edn in ~/.clojure/deps.edn where you may want to define common configurations, dependencies, etc. to be used across different projects. Specific configurations can be invoked/overridden using options on the command line.

Related