Running a another task when an uberjar is created with Leiningen - clojure

When the uberjar is created, I also need to run lein deps. How do I make Leiningen automatically run lein deps when lein uberjar is run?

lein deps is run automatically on other lein tasks like run jar cljsbuild...
In fact I never use lein depsexcept in lein do clean, deps.
Note : I am just transitioning to boot myself, but composing tasks is much easier with this build tool.

You can have leiningen run a combination of any lein tasks via the do command. We can then define an alias to run the desired tasks with ease. In your profile.clj include the following:
:aliases {"build-with-deps" ["do" "clean" "deps" "uberjar"]}
Then whenever you call lein build-with-deps it will actually run the following: lein do clean, deps, uberjar.
I recommend reading through the sample project.clj provided by leiningen to better familiarize yourself with the capabilities of aliases.

Related

How do I run a Lein project in Clojure's new CLI tools version?

I have a Clojure project I started a couple of years ago.
It's all using Leiningen. With a project.clj file etc. And my code in src/myproj/blah.clj etc.
I now want to try using Clojure's new CLI tools.
I tried navigating into the root of this project directory and launching clj then typing (require '[myproj.blah :as blah])
But this throws a FileNotFoundException.
Can the CLI tools find code in a lein type file-structure and work with existing lein projects?
If not what's the procedure to adapt a lein project to use CLI tools?
Here's a guide to the new command line tools https://clojure.org/guides/deps_and_cli
You need a deps.edn file that contains your dependencies instead of a project.clj file.
Here's some rationale of what deps does and doesn't do:
http://cdn.cognitect.com/presentations/2017/dependency_heaven.pdf

leiningen uberjar - add external jar at runtime

I am trying to see how I can add a directory and an external jar to the classpath when making a leiningen uberjar.
The reason is that I need to distribute a jar but some dependencies (jdbc driver for example) cannot be compiled into the uberjar due to licencing restrictions.
I would also like to provide certain external resources such as properties for logging and configuration external to the jar.
Normally in java or spring boot I would use the appropriate command line option to change the classpath. However -cp does not find the jar etc
Is there a way to do this or an appropriate plugin?
Thanks in advance
You can add the jar as a resource in the :dev profile. Then it is added to the classpath. The dev profile is for the local development and not packaged into the uberjar.
:profiles {
:uberjar {:aot :all}
:dev {:resource-paths ["no-redist/commercial-jdbc-driver.jar"]}}
At 'production' time with the uberjar you need set the classpath then manually:
java -cp no-redist/commercial-jdbc-driver.jar;your-app-uber-jar.jar main.namespace
Afaik when using the -jar flag, it uses the dependencies in the jar file, whatever is linked and referred to in there. Combining -cp and -jar might not work.
Another way is to refer to the no-distributable jar file in the jar manifest (META-INF/MANIFEST.MF):
Class-Path: no-redist/commercial-jdbc-driver.jar
The the java -jar your-app-uber-jar.jar would look for the jar in the folder no-redist/commercial-jdbc-driver.jar in the local directory. Add this in leinigen like:
:manifest {"Class-Path" "no-redist/commercial-jdbc-driver.jar"}

How to set the classpath for the jvm that lein uses

I want to create a new lein project from a template I have created without installing the template in .m2 or on clojars. So I want to set the classpath that lein uses when it works. How is this done?

Lein tasks and sudo

I'd like to get this running (https://github.com/maitria/avi), which recommends using
sudo lein install
But lein doesn't act like an ordinary command -- I get
sudo: lein: command not found.
There only seem to be three subcommands that run into permissions problems and I considered doing them manually, but they're fairly gnarly string-interpolated arguments to the C compiler and my chances of not making a mistake seem pretty small.
So leiningen is not your typical command. Its a tool built in Clojure and is used to manage clojure applications much like rake works for Ruby. These 3 steps should help you
Download the lien script from the leiningen home page and place it in a location that is part of your PATH.
2.Run lein in the command-line and it will install it dependencies, though you will need to make sure you preinstalled JDK
3.Run lein install in the location of your code

Leiningen 'lein install' without writing pom.xml?

I've been contributing to a project in which there's a fixed pom.xml, but I tend to modify project.clj, temporarily, for various purposes. Most of the time, Leiningen seems to ignore pom.xml, but lein install and lein deploy rewrite pom.xml. Is this necessary to Leiningen's functioning? If not, can I stop it? Haven't found anything about this in the online docs yet.
It's not a big problem, but I'd rather not deal with having to restore the official pom.xml in my project before doing a 'git commit', for example.
EDIT: I discovered a solution for my situation, which is to create a 'checkouts' directory with a link to the other project. This causes the other project to be pulled in, without creating a new jar. However, I'm still curious: Does Leiningen need to create pom.xml e.g. for lein install? Why? I'd like to understand the process.
I'm unsure of whether lein install can be run without generating a pom.xml.
The reason it generates a pom is that leiningen uses maven to grab all the dependencies. In fact, maven is able to get dependencies from clojars and leiningen uses it for the clojure dependencies as well as the java ones. Maven requires a pom.xml to specify repository locations and other configuration necessary to run javac (or any other build commands) in addition to a groupId and artifactId to deploy your library to a (potentially local) repository
Another possible workaround to your issue is to use changelists (specifically, if using tortoiseSVN, I'd use the ignore-on-commit changelist) to ignore your pom.xml in most situations commitwise. I believe this stackoverflow answer may assist you with how to do that in git.