Why are Leiningen download dependencies in ~/.m2/repository directory? - clojure

I have created a new project using Leiningen. When I added a new dependency and ran lein deps, the lein downloaded the dependencies in ~/.m2/repository directory.
Shouldn't the project specific dependencies be downloaded in a directory that is local to the project and not into the global directory like ~/.m2/repository.
For example - While I used npm, it gave me 2 options, i.e., to download the dependency locally (in project's node_modules directory) or globally.

Unlike npm, lein uses maven and maven dependencies are stored in a globally shared directory, defaulting to ~/.m2/repository.

As previous pointed lein uses maven and maven's "local repository" is by default in the ${user.home}/.m2/repository folder.
You can control/change this from your maven (user) settings file ${user.home}/.m2/settings.xml or (global) settings file ${maven.home}/conf/settings.xml by defining the entry:
<localRepository>${user.home}/.m2/repository</localRepository>
for more details check: maven settings reference

Related

lein uberjar doesn't pack the jar file under resource into the final jar

I defined :resource-paths in profile.clj to include some special jar (vertica jdbc) file. and then I run lein uberjar: trying to pack that jar file into the standalone jar file, but after I run it, and uncompressed it, I can't find that vertica jdbc file.
Leiningen can create jar files, but the only place it picks them up from is your maven repository (under an .m2 directory on your machine). Once your special jar is in maven you need to refer to it as a dependency in your project.clj file.
If your jar file was in clojars this would be quite easy - just put in a dependency - lein will fetch it into your maven repository. On the other hand if it was your own source code you would first need to lein install from the directory of that source code project.
However I'm guessing your file is just a jar file you have, which is not an artifact in clojars. In this case you can install it into your maven repository using this maven command (here assuming my-deps.jar is your file):
mvn install:install-file -Dfile=./my-deps.jar -DgroupId=my-deps -DartifactId=my-deps -Dversion=1.0.0 -Dpackaging=jar
You can then refer to it as a dependency in the uberjar project, and then lein uberjar.
Edit - if you don't want to use raw maven commands then consider using this leiningen library that abstracts them away for you: https://github.com/kumarshantanu/lein-localrepo.
An alternative is to add to project.clj:
:repositories {"local" "file:lib"}
Create a lib directory in your project and add the jar there.
Add the lib reference in the :dependencies tag in project.clj.
This lib directory has the same structure as .m2/repository but is inside your project and local to your project

How to use a local repository for a Clojure library during initial development?

I have a question about developing a Clojure library which is not answered in the suggested workflow for Library Development and Distribution as described here: http://clojure-doc.org/articles/ecosystem/libraries_authoring.html
I am developing a library and want to test this in a clojure project. In this project I will have to add the library under development as a dependency. Is there an alternative for 'lein deploy clojars' which will deploy my library to a local repository? If so how would I setup :dependencies for this in the test project? Note that I will be using libraries in clojars as well in the project which I use to test the library under development.
So where should I deploy a Clojure library in development to, a local repository perhaps, such that it can be used by projects which alpha test the library. How should the test projects address this. I would like to know how this affects 1) the project.clj file for the library development project and 2) the project.clj file for the project which tests the library in development.
lein install does the job:
$ lein install -h
Install jar and pom to the local repository; typically ~/.m2.
In your library project execute lein install and your library jar and pom files will be installed under the ~/.m2 directory.
After that when you build another project that depends on your library, lein will find its binaries in ~/.m2.
~/.m2 is a default location of the local Maven repository which is one of the locations used by lein during dependency resolution. It also works as a cache for remote repositories where artifacts downloaded from Maven Central or Clojars are stored.
What you are looking for is Leigningen's hard to find unless you know what to look for "checkouts" features.
Documentation: https://github.com/technomancy/leiningen/blob/master/doc/TUTORIAL.md#checkout-dependencies

Using lein project with /lib doesn't work

When using lein 2.2, trying to put jar files in /lib does not work.
I tried and it doesn't seems to work but plenty of docs out there says this way still works.
The lib directory functionality was removed in Leiningen v2.0, in favor of a repository (repeatability).
To add free floating jars to a project, you will need to either deploy your dependency to Clojars or a Maven repository. The Maven repository can be as simple as a directory in your project folder.
Please see the answer to this question if you need the jar in a project local folder:
How do I use checked-in jars with leiningen
You do not need to use maven to access a local jar file in Leiningen v2.0
Just use syntax like this in your project.clj:
:resource-paths [ "local-jars/*" ]
and then place your *.jar files in the local-jars subdirectory of your project.
Please see this blog: http://www.troyastorino.com/tidbits/lein2-local-jars

Does lein include a maven executable?

I need to perform a mvn install. From what I know, Leiningen (which I already have) is built on Maven so perhaps I don't need to install Maven separately - or do I?
Leiningen uses pomegranate, a Clojure wrapper for Eclipse Aether, a library for working with artifact repositories. Since there were many more products that needed repository access, Aether's functionality was split off from Maven into a separate project, and Maven was changed to make use of Aether.
Pomegranate has an install functionality
install
"Install the jar-file kwarg using the pom-file kwarg and coordinates kwarg.
:coordinates - [group/name \"version\"]
:jar-file - a file pointing to the jar
:pom-file - a file pointing to the pom
:local-repo - path to the local repository (defaults to ~/.m2/repository)
:transfer-listener - same as provided to resolve-dependencies"
I believe there is a lein install task.

leiningen how to specify dependency for clojure without duplicating jar

Everytime I do a lein new I seem to get a copy of the clojure jar in the lib folder of that project. I thought that jars deps were copied to .m2/repository. Why is the clojure jar duplicated for every lein project?
This only happens with Leiningen 1. The reason for this (I think) was to allow tools to inspect the dependencies easily. With Leiningen 2, this is no longer an issue because it uses the pomegranate library (which is a wrapper for Aether) to manage dependencies. It allows for more robust dependency management, and therefore Leiningen can just link to the dependencies from your local Maven repository.
Jars are cached in your .m2 repo. However, to use them in your project, they need to be on your project's classpath, usually in your project/lib directory. Caching in your local Maven repo just saves you a download from the server.