简体   繁体   English

如何在clojure中导入本地java库? (雷音)

[英]How do I import a local java library in clojure? (lein)

I'm trying to use a java library (jar file) called DragonConsole that is not on maven central or clojars. 我正在尝试使用一个名为DragonConsole的java库(jar文件),它不在maven central或clojars上。

I want to import this library in my clojure application, but so far I can't figure out how to do so. 我想在我的clojure应用程序中导入这个库,但到目前为止我无法弄清楚如何这样做。

I tried setting up a local maven repo, but I don't think I did it right. 我尝试建立一个当地的maven回购,但我认为我做得不对。

lein deps gives me this error: lein deps给了我这个错误:

(Retrieving dragonconsole/dragonconsole/3.0.0/dragonconsole-3.0.0.pom from local)
(Could not transfer artifact dragonconsole:dragonconsole:pom:3.0.0 from/to local)
(file:/home/michael/clj/enclojed/maven_repository/): no supported algorithms found)

project.clj: project.clj:

:dependencies [[org.clojure/clojure "1.6.0"]
               [clojure-lanterna "0.9.4"]
               [dragonconsole "3.0.0"]]
:repositories [["local" {:url ~(str (.toURI (java.io.File. "maven_repository")))}]]

project folder: 项目文件夹:

maven_repository/DragonConsolev3.jar
maven_repository/dragonconsole/dragonconsole/maven-metadata-local.xml
maven_repository/dragonconsole/dragonconsole/3.0.0/dragonconsole-3.0.0.pom
doc/...
src/...
test/...
resources/...
project.clj

If there's any other files you need to see, check the git page . 如果您需要查看其他任何文件,请查看git页面

This is probably the easiest way. 这可能是最简单的方法。 Do you have a ~/.m2 directory? 你有一个〜/ .m2目录吗?

1) Add your dependency in project.clj eg :dependencies [[dragonconsole "3.0.0"]] 1)在project.clj添加你的依赖项,例如:dependencies [[dragonconsole "3.0.0"]]

When you run lein run/test/etc it will attempt to pull the library from maven central and clojars, and it will create a path for you under ~/.m2, such as ~/.m2/repository/dragonconsole/dragonconsole/3.0.0/ 当你运行lein run/test/etc ,它将尝试从maven central和clojars中拉出库,它将在〜/ .m2下为你创建一个路径,例如~/.m2/repository/dragonconsole/dragonconsole/3.0.0/

Note: if a library with the same name and version exists, simply delete the contents downloaded (a jar, pom, etc...) 注意:如果存在具有相同名称和版本的库,只需删除下载的内容(jar,pom等...)

2) Create a symbolic link to your jar under the dragonconsole directory in ~/.m2, eg ln -s /path/to/project/dragonconsole-3.0.0.jar ~/.m2/repository/dragonconsole/... 2)在〜/ .m2的dragonconsole目录下创建一个指向jar的符号链接 ,例如ln -s /path/to/project/dragonconsole-3.0.0.jar ~/.m2/repository/dragonconsole/...

This time when you run lein run/test/etc it will work. 这次当你运行lein run/test/etc它会起作用。 This is the easiest and cleanest approach I've found although I haven't had much time to poke around. 这是我发现的最简单,最干净的方法,尽管我没有太多时间去探索。 I like it because your code and build process doesn't change any more than it needs to (just a single line to add the dependency). 我喜欢它,因为您的代码和构建过程不会发生任何变化(只需要一行来添加依赖项)。

For production I'd look for a way to add a lein repository "source", so it looks in my custom repo, then maven central, then clojars. 为了生产我会寻找一种方法来添加一个lein存储库“source”,所以它看起来是我的自定义仓库,然后是maven central,然后是clojars。

Here's a simple and straightforward description: 这是一个简单明了的描述:

https://www.pgrs.net/2011/10/30/using-local-jars-with-leiningen/ https://www.pgrs.net/2011/10/30/using-local-jars-with-leiningen/

I think the best solution is in a comment: 我认为最好的解决方案是评论:

  1. Use deploy to replace install 使用deploy替换安装
mvn deploy:deploy-file -Dfile=jnotify-0.94.jar -DartifactId=jnotify -Dversion=0.94 -DgroupId=jnotify -Dpackaging=jar -Durl=file:/home/xxx/maven_repository/
  1. Add repo to project.clj 将repo添加到project.clj
:repositories {“local” “file:/home/xxx/maven_repository”}

DISCLAIMER: This answer details how to install local jars as dependencies using leiningen, but I haven't tried it with native java jars. 免责声明:这个答案详细说明了如何使用leiningen将本地jar作为依赖项安装,但我还没有尝试使用本机java jar。 The directions should be the exact same, though; 但是,方向应该完全相同; you should just be able to use the library through interop forms, and as far as lein is concerned you're just installing another jar file. 你应该只能通过互操作形式使用库,就lein而言,你只是安装另一个jar文件。

In lein 2, The only way you can import jars is with maven repositories, so you'll need to create a local maven repository. 在lein 2中,导入jar的唯一方法是使用maven存储库,因此您需要创建一个本地maven存储库。 I use the following commands to install a jar file. 我使用以下命令安装jar文件。 The commands are UNIX, but the ideas they capture should be doable on any OS with java/maven. 这些命令是UNIX,但它们捕获的想法应该适用于任何带有java / maven的操作系统。

First, I make a directory for the local repository: 首先,我为本地存储库创建一个目录:

mkdir -p ${HOME}/.local/var/lein_repo

Then I use this command to install the jar file into the repository: 然后我使用此命令将jar文件安装到存储库中:

mvn deploy:deploy-file \                                                        
    -Dfile=${name}-${version}.jar \                                             
    -DartifactId=${name} \                                                      
    -Dversion=${version} \                                                      
    -DgroupId=${name} \                                                         
    -Dpackaging=jar \                                                           
    -Durl=file:/home/djhaskin987/.local/var/lein_repo

Finally, I add this line to my local lein file: 最后,我将此行添加到我的本地lein文件中:

:repositories {"local" ~(str (.toURI (java.io.File. "/home/djhaskin987/.local/var/lein_repo")))}

And then I run lein deps to make sure it all works. 然后我运行lein deps以确保一切正常。 Notice the last line: it's getting the clj-ga jar with version 0.1.0-SNAPSHOT from the local repository. 注意最后一行:它从local存储库获取版本为0.1.0-SNAPSHOTclj-ga jar。

[ djhaskin987@localhost:~/Workspace/ulam ]$ lein deps
Retrieving org/clojure/clojure/1.5.1/clojure-1.5.1.pom from central
...
Retrieving clj-ga/clj-ga/0.1.0-SNAPSHOT/clj-ga-0.1.0-20140718.032154-1.jar from local

I figured out how to do this from places like this github gist . 我想出了如何从像这个github gist这样的地方做到这一点

Alternatively, you could checkout lein-localrepo , a plugin for lein to do this kind of stuff. 或者,您可以查看lein-localrepolein的插件来做这种事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM