Explore the essential tools Leiningen and tools.deps for managing Clojure projects, focusing on build automation and dependency management.
As experienced Java developers, you’re likely familiar with build tools like Maven and Gradle. In the Clojure ecosystem, Leiningen and tools.deps serve similar purposes, facilitating project management and dependency handling. This section will delve into these tools, highlighting their features, differences, and how to choose the right one for your projects.
Leiningen is a build automation and project management tool for Clojure, akin to Maven in the Java world. It provides a comprehensive suite of features, including dependency management, project scaffolding, and task automation.
project.clj
file to specify dependencies, similar to Maven’s pom.xml
.A typical Leiningen project includes:
project.clj
: Defines project metadata, dependencies, and build configurations.src/
: Contains source code.test/
: Houses test files.resources/
: Stores non-code resources like configuration files.Here’s a simple project.clj
example:
(defproject my-clojure-app "0.1.0-SNAPSHOT"
:description "A sample Clojure application"
: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"]]
:main ^:skip-aot my-clojure-app.core
:target-path "target/%s"
:profiles {:uberjar {:aot :all}})
Try It Yourself: Modify the :dependencies
vector to include additional libraries, such as ring
for web applications.
tools.deps is a more recent addition to the Clojure ecosystem, focusing on dependency management with a simpler approach. It uses a deps.edn
file to specify dependencies and is part of the Clojure CLI tools.
A typical tools.deps project includes:
deps.edn
: Defines dependencies and paths.src/
: Contains source code.resources/
: Stores non-code resources.Here’s a simple deps.edn
example:
{:deps {org.clojure/clojure {:mvn/version "1.10.3"}
ring/ring-core {:mvn/version "1.9.0"}}
:paths ["src" "resources"]}
Try It Yourself: Add a new dependency to the :deps
map, such as compojure
for routing in web applications.
Both Leiningen and tools.deps are powerful tools, but they serve slightly different purposes. Here’s a comparison to help you decide which tool to use:
Feature | Leiningen | tools.deps |
---|---|---|
Focus | Comprehensive build automation | Dependency management |
Configuration | project.clj |
deps.edn |
Plugins | Extensive plugin ecosystem | Limited, relies on CLI tools |
Project Templates | Yes | No |
Task Automation | Built-in task automation | Requires external scripts |
Flexibility | More opinionated, structured | More flexible, less opinionated |
The choice between Leiningen and tools.deps depends on your project’s needs and your personal preferences:
Let’s build a simple “Hello, World!” application using both tools to illustrate their usage.
Create a new project:
lein new app hello-world
Navigate to the project directory:
cd hello-world
Edit src/hello_world/core.clj
:
(ns hello-world.core)
(defn -main
"I don't do a whole lot ... yet."
[& args]
(println "Hello, World!"))
Run the application:
lein run
Create a new directory:
mkdir hello-world
cd hello-world
Create a deps.edn
file:
{:deps {org.clojure/clojure {:mvn/version "1.10.3"}}}
Create a src/hello_world/core.clj
file:
(ns hello-world.core)
(defn -main
[& args]
(println "Hello, World!"))
Run the application:
clj -M -m hello-world.core
Try It Yourself: Modify the -main
function to accept command-line arguments and print them.
To better understand the flow of data and dependencies in Clojure projects, let’s look at a diagram illustrating the relationship between project files and their roles in both Leiningen and tools.deps.
Diagram Caption: This diagram shows the typical structure of a Clojure project using both Leiningen and tools.deps, highlighting the roles of project.clj
and deps.edn
.
resources/
directory and print its contents.cheshire
, into both a Leiningen and a tools.deps project. Compare the dependency management process.Now that we’ve explored the fundamentals of Leiningen and tools.deps, you’re equipped to manage Clojure projects effectively. Let’s continue to build on this foundation as we delve deeper into Clojure’s unique features and capabilities.