[英]How to add directory to Clojure's classpath?
I have installed the libraries with Maven to the ~/.m2/repository/ directory.我已将带有 Maven 的库安装到 ~/.m2/repository/ 目录。 I would like to add that path to the default Clojure classpath.
我想将该路径添加到默认的 Clojure 类路径中。 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: class 路径默认为:
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
Leiningen really makes this process a lot less painful by keeping the setting of the classpath associated with the project, and more importantly leads to a repeatable build process .
通过保持与项目关联的类路径的设置,Leiningen确实使这个过程变得更加痛苦,更重要的是导致可重复的构建过程 。 where you can come back to the project years later and still get a repl.
你可以在几年后回到这个项目,仍然可以获得一个repl。 A general outline of using leiningen in these cases:
在这些情况下使用leiningen的概述:
this is assuming that the library you are using is not already part of or available from a package in a maven repo, which many are. 这假设你正在使用的库不是maven仓库中的一个包的一部分或可用,很多都是。
The non-painful, popular method is to not mess with maven and classpaths and the JRE directly and use leiningen: https://github.com/technomancy/leiningen/ 非痛苦,流行的方法是不要直接使用maven和类路径以及JRE,并使用leiningen: https : //github.com/technomancy/leiningen/
Otherwise, you can modify whatever is in clj
and add/set the classpath in whatever ways java likes. 否则,你可以修改
clj
任何内容,并以java喜欢的方式添加/设置类路径。 See for example Setting multiple jars in java classpath 请参阅例如在java类路径中设置多个jar
It should be noted that you also have the option of adding classpaths at runtime with the library pomegranate https://github.com/cemerick/pomegranate 应该注意的是,您还可以选择在运行时使用库石榴添加类路径https://github.com/cemerick/pomegranate
This lets you do like: 这可以让你这样做:
(require '[cemerick.pomegranate :as pom])
(pom/add-classpath "/home/user/~.m2/....")
I assume that clj
is a script to start Clojure REPL. 我假设
clj
是一个启动Clojure REPL的脚本。 Take a look into this script and find line similar to this: 看一下这个脚本,找到与此类似的行:
java -cp /path/to/clojure.jar clojure.main
Here you start class clojure.main
having "clojure.jar" on your classpath. 在这里,您可以在类路径上启动类
clojure.main
,其中包含“clojure.jar”。 To add more jars just add them to the end of -cp
option values. 要添加更多jar,只需将它们添加到
-cp
选项值的末尾即可。 Eg on Linux: 例如在Linux上:
java -cp /path/to/clojure.jar:/path/to/mylib.jar clojure.main
(use ;
instead of :
on Windows) (使用
;
代替:
在Windows上)
However, very soon you'll get tired of this way and will look for project management tool. 但是,很快你会厌倦这种方式并寻找项目管理工具。 So it makes sense to start using it right now.
所以现在开始使用它是有道理的。 Take a look at Leiningen - it manages dependencies for you based on Maven (so it will be extremely easy to add new jar) and has REPL.
看看Leiningen - 它基于Maven为你管理依赖(因此添加新jar非常容易)并且有REPL。
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.另一种选择是在您计划运行 REPL 或保存项目文件的文件夹中创建一个
deps.edn
文件。
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该文件用于通知 Clojure 有关依赖项、源文件、执行配置文件等...它在您运行 REPL 时加载(但还有其他用例),它由 Clojure 的核心支持,官方记录在Z5E7BA56C50BADEZ0804C上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.在您的情况下,您可能只想放置以下内容,以声明您要下载的依赖项并将其放在 Java 类路径中。
{
:deps {
the.dependency/you-want {:mvn/version "1.0.0"}
}
}
In deps.edn
you can specify:在
deps.edn
,您可以指定:
Note that the dependencies will be downloaded and cached in .cpcache/
folder, beside the deps.edn
itself.请注意,依赖项将被下载并缓存在
deps.edn
本身旁边的.cpcache/
文件夹中。 I'm not sure if you can instruct it to use the global ~/.m2
instead.我不确定您是否可以指示它使用全局
~/.m2
来代替。
You may find the dependency coordinates (name and latest version) on clojars.org您可以在clojars.org上找到依赖坐标(名称和最新版本)
deps.edn
is "lighter", part of the core Clojure, if less powerful than leiningen; deps.edn
是“更轻的”,是核心 Clojure 的一部分,如果不如 leiningen 强大的话; so maybe suited for setting up an environment for casual/exploratory coding at the REPL or CLI.因此可能适合在 REPL 或 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.您还可以在
~/.clojure/deps.edn
中拥有一个全局deps.edn
,您可能希望在其中定义通用配置、依赖项等以在不同项目中使用。 Specific configurations can be invoked/overridden using options on the command line.可以使用命令行上的选项调用/覆盖特定配置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.