Explore how to manage Clojure project dependencies using Leiningen and the deps.edn format, understanding their differences and best use cases.
In the world of Clojure development, managing dependencies is a critical task that ensures your projects are built efficiently and maintainably. Two primary tools for handling dependencies in Clojure are Leiningen and the newer deps.edn
format. Each has its own strengths and use cases, and understanding how to leverage them effectively can significantly enhance your development workflow.
Leiningen, often referred to as “Lein,” is a build automation tool for Clojure. It has been the de facto standard for Clojure projects for many years, providing a comprehensive suite of features beyond dependency management, including project scaffolding, testing, packaging, and more.
project.clj
file to specify project dependencies, repositories, and other configurations.To get started with Leiningen, you need to install it on your system. The installation process is straightforward and can be done via package managers or by downloading the script directly from the Leiningen website.
brew install leiningen
curl https://raw.githubusercontent.com/technomancy/leiningen/stable/bin/lein > ~/bin/lein
chmod +x ~/bin/lein
Once installed, you can verify the installation by running:
lein version
project.clj
File§The project.clj
file is the heart of a Leiningen project. It defines the project metadata, dependencies, and various settings. Here’s a basic example of a project.clj
file:
(defproject my-clojure-app "0.1.0-SNAPSHOT"
:description "A sample Clojure project"
:url "http://example.com/my-clojure-app"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.10.3"]
[ring/ring-core "1.9.0"]]
:main ^:skip-aot my-clojure-app.core
:target-path "target/%s"
:profiles {:uberjar {:aot :all}})
project.clj
§The deps.edn
format is a newer approach to dependency management in Clojure, introduced with the Clojure CLI tools. It provides a more declarative and flexible way to manage dependencies, focusing on simplicity and composability.
To use deps.edn
, you need the Clojure CLI tools installed. You can download and install them from the official Clojure website.
brew install clojure/tools/clojure
clojure -M:version
deps.edn
File§The deps.edn
file is a simple EDN (Extensible Data Notation) file that defines dependencies and other configurations. Here’s an example of a basic deps.edn
file:
{:deps {org.clojure/clojure {:mvn/version "1.10.3"}
ring/ring-core {:mvn/version "1.9.0"}}
:paths ["src" "resources"]
:aliases {:dev {:extra-paths ["dev"]
:extra-deps {cider/cider-nrepl {:mvn/version "0.25.9"}}}}}
deps.edn
§While both Leiningen and deps.edn
serve the purpose of managing dependencies, they differ in several key aspects:
deps.edn
focuses on simplicity and composability.deps.edn
relies on external tools or custom scripts.deps.edn
is more tool-agnostic, allowing for integration with various tools.project.clj
is more verbose and feature-rich, while deps.edn
is concise and declarative.Create a New Project: Use Leiningen to scaffold a new project.
lein new app my-clojure-app
Add Dependencies: Edit the project.clj
file to add dependencies.
:dependencies [[org.clojure/clojure "1.10.3"]
[compojure "1.6.2"]]
Run the Project: Use Leiningen to run the project.
lein run
Build an Uberjar: Package the project into an executable JAR.
lein uberjar
Create a New Project Directory: Manually create a project directory and deps.edn
file.
mkdir my-clojure-app
cd my-clojure-app
touch deps.edn
Add Dependencies: Edit the deps.edn
file to add dependencies.
{:deps {org.clojure/clojure {:mvn/version "1.10.3"}
compojure {:mvn/version "1.6.2"}}}
Run the Project: Use the Clojure CLI to run the project.
clojure -M -m my-clojure-app.core
Create a Custom Alias: Define an alias for development.
:aliases {:dev {:extra-paths ["dev"]
:extra-deps {cider/cider-nrepl {:mvn/version "0.25.9"}}}}
Use the Alias: Run the project with the development alias.
clojure -M:dev
lein deps :tree
or clojure -Stree
to visualize and resolve conflicts.project.clj
or deps.edn
files are well-documented to aid future maintenance.Both Leiningen and deps.edn
offer powerful ways to manage dependencies in Clojure projects, each with its own strengths and ideal use cases. By understanding the differences and best practices associated with each tool, you can choose the right approach for your project’s needs, ensuring a smooth and efficient development process.