how to package and start your clojure application? - clojure

Like many JVM languages, packaging and starting a JVM application can be done in a multitude of ways. What are some solid ways of packaging large clojure projects having lots of both clojure and Java dependencies, for deploying and starting them in production-like environments?

With leiningen, you can simply package through:
lein uberjar
This will package all the dependencies and your code into one jar that you can then run via:
java -jar uberjar.jar
For that to work however, you have to specify a :main namespace in your project.clj, which will trigger AOT, the latter considered undesirable for other causes such as for being able to cleanly use clojure.tools.namespace over your project. So to avoid AOT, you can skip specifying a :main namespace in your project.clj, and simply run with the slightly more verbose command:
java -jar uberjar.jar clojure.main -m your.main.namespace

Related

What is the purpose of :hooks [leiningen.cljsbuild] in a Leiningen project.clj file?

I have been looking at several project templates for a Clojure/ClojureScript web application. Some have the following line in the Leiningen project.clj
:hooks [leiningen.cljsbuild]
while others have not.
What is the purpose of this line? Why would you use it, and why not? Are there advantages, or disadvantages in using this?
:hooks option is used for modifying built-in Leiningen tasks. All the details are described in Leiningen's documentation.
leiningen.cljsbuild hooks are adding ClojureScript support in lein's built-in tasks: compile, test, jar.
It might be useful if your project contains both Clojure and ClojureScript files. By using the hook you can compile, test or package them in jar in one run using lein's built-in tasks instead of calling them separately for Clojure (built-in tasks) and ClojureScript with lein-cljsbuild tasks.

How to add libraries in clojure?

Everywhere I see, it is suggested that I add :dependencies in project.clj and run lein deps. Where are these downloaded? What is my CLASSPATH and how can I add my own JARs to my clojure project?
While the answer for
Dependencies in maven local repositories with leiningen
kind of solves my need, I am not marking it duplicate as what I am asking is much simpler (being a beginner, who does not have much experience with Java to know about Maven). I am still finding it hard to understand where clojure ends and leiningen begins.
The thing I was looking for is a way to add library like we do in most other languages (e.g. copy JAR to project directory and import in code).
This is great question since it's not clear at all. Leiningen is often a black hole and if something isn't working it's often hard to debug.
I just recently had to do some manual scripting and leiningen does help you with finding out these things.
Where are these downloaded?
The directory is in $HOME/.m2. This is Maven's: http://maven.apache.org/settings.html
What is my classpath?
The classpath is set depending on your :dependencies as well as your :source-paths and :resource-paths vectors.
You can find out your classpath like this:
lein classpath
This will print a huge list depending on your configuration.
You could --for instance-- then run a script:
java -cp cljs-1.7.xx.jar:scripts:$(lein with-profile +dev-cljs classpath) clojure.main scripts/cljs-build.clj dev
That has access to all your projects dependencies and loads them properly.
Although you could use lein run to achieve something similar:
lein with-profile +dev-cljs run -m clojure.main scripts/cljs-build.clj dev
How do I add my own JARs?
See: leiningen - how to add dependencies for local jars?
Run lein install from your library's project dir.
Add an entry to :dependencies in client's project.clj.
Make sure the lib name/version and its reference match (eg. [mylib "0.0.1-SNAPSHOT"]).
Hope this helps, forget about jar loc and cp for now.

Getting Leiningen to download dependencies outside of a project

I'm learning Clojure, but I'm not really building whole projects for each little code snippet, I just drop them into a REPL. Occasionally code snippets I'm exploring require a dependency (usually something that is/was in clojure.contrib).
The only way I know how to get those dependencies onto my computer is to have an empty leiningen project, add the dependency to project.clj and run lein deps.
Is there any way I can download libraries globally, outside of a project? If it's that I really really don't want to, why?
I have a small project that I use for testing code snippets and answering SO questions, and am also constantly adding dependencies. The project.clj for this project includes Pomegranate as a dependency which then makes dynamically loading other dependencies as easy as:
(use '[cemerick.pomegranate :only (add-dependencies)])
(add-dependencies :coordinates '[[my-dependency "1.2.3"]])
Give lein-try a go. It's a leiningen plugin I wrote that lets you say something like lein try [my-dependency 1.0.0] or even lein try my-dependency at the command line and drop into a REPL with the dependency available.
If you are using lein-exec as your way of running one-off scripts, you can now use a little snippet at the top of the script. Add:
(use '[leiningen.exec :only (deps)])
(deps '[[clj-time "0.8.0"]])
to the top of your clj. Now running lein exec [example.clj] will automatically pull down the requirement.
If you are new to lein exec, just add {:user {:plugins [[lein-exec "0.3.4"]]}} to your ~/.lein/profiles.clj and you can begin running lein exec on your clj files. It is a great and quick way to run code without a project.

Installing libraries with leiningen without creating project

I am learning Clojure and coming from a Ruby background.
I am looking for something analogous to gem install <library>. The various incantations of lein install do not seem to fit this bill.
Is there a way to simply install a library locally so that it can be referenced in the REPL without the need to create a project?
Seems like, you want to install a library with lein. Here is the plugin, install it and use like
lein localrepo install <filename> <[groupId/]artifactId> <version>
If your aim is merely to load libraries in the REPL consider using alembic. It loads dynamically classpaths, resolve dependencies and automatically pulls libraries from the repositories.
Here is a use case:
(require 'alembic.still)
(alembic.still/distill '[enlive "1.1.1"])
It simply requires you to add the following entry to your .lein/project.clj:
{:dev {:dependencies [[alembic "0.1.1"]]}}
See this answer.
Java and thus clojure do not generally have the the idea of globally installed libraries. You should always be creating a classpath with the minimal set of dependencies. You need somehow to specify and manage this classpath and the easiest way to do this is with leiningen, which requires a project.
leiningen automates the process of retrieving the remote libraries and placing them in your local repository which is somewhat analogous to gem install, but these libraries do not become automatically available to a REPL.
The easiest way to have a set of libraries always available is to have a 'scratch' project which you use for REPL experiments before starting a new project. It's not too much of an overhead.
In lein 2 you can update profiles.clj with package you want to install:
~\user\.lein\profiles.clj
With the first run of any project with lein, the local repo will be updated with what was incereased in profiles.clj.
Sometimes I just run lein deps without being in a project folder, this will update the local repo for you.
This way you can add any library to your project.clj or call it from repl and it will be extracted from local repo.
If you don’t have a project, you add your dependencies in your global lein user profile instead located at ~/.lein/profiles.clj.
The doc isn’t great for lein to be honest. So this part is confusing. But you edit that file as such:
{:user {:plugins [[lein-pprint "1.1.1"]]
:dependencies [[slamhound "1.3.1"]]}}
In the :plugins vector you add whatever global lein plugin you want to have. And in the :dependencies vector you add whatever library you want available globally.
Then anywhere you start a lein repl you’d have those dependencies available to you. And everywhere you run lein you’ll have the additional plugin features available to you.
If you use tools.deps instead of lein, aka, the clj command instead of the lein command. Then it is a bit different. You instead want to modify your ~/.clojure/deps.edn file. Where you’d add dependencies there instead:
{:deps {clj-time {:mvn/version "0.14.2"}}}
So if you put the above in your user deps.edn whenever you run clj command the clj-time library will be available to you.

Noir uberjar only works in build directory

I am attempting to distribute a small web application build with the clojure web framework noir. It works as expected when run with lein run. When I run lein uberjar and then java -jar project-1.0.0-standalone.jar it works as expected. However, if I move the jar file (project-1.0.0-standalone.jar) it runs, but every page results in a 404. My project.clj is the default one generated by lein noir new except I added :omit-source true and :aot :all.
I'm using:
leiningen 1.7.1
clojure 1.2.1
noir 1.2.1
How can I make a jar that can be distributed to others without source?
if you have not already added gen-class calls to your namespace definitions, adding them may fix this. You can test this by running
lein clean
lein compile
and make sure you see each of your classes being built
I believe the problem you are experiencing is because of the way noir.server/load-views requires the namespaces. If you move it from the target/ directory, it can no longer find the views directory you passed into load-views.
The workaround is to explicitly require all you're views in lieu of using the load-views function. Then you should be able run the uberjar anywhere.