简体   繁体   中英

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.

I want to import this library in my clojure application, but so far I can't figure out how to do so.

I tried setting up a local maven repo, but I don't think I did it right.

lein deps gives me this error:

(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:

: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 .

This is probably the easiest way. Do you have a ~/.m2 directory?

1) Add your dependency in project.clj eg :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/

Note: if a library with the same name and version exists, simply delete the contents downloaded (a jar, pom, etc...)

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/...

This time when you run lein run/test/etc it will work. 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.

Here's a simple and straightforward description:

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
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
: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. 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.

In lein 2, The only way you can import jars is with maven repositories, so you'll need to create a local maven repository. I use the following commands to install a jar file. The commands are UNIX, but the ideas they capture should be doable on any OS with 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:

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:

: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. Notice the last line: it's getting the clj-ga jar with version 0.1.0-SNAPSHOT from the local repository.

[ 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 .

Alternatively, you could checkout lein-localrepo , a plugin for lein to do this kind of stuff.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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