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:
1(defproject my-clojure-app "0.1.0-SNAPSHOT"
2 :description "A sample Clojure application"
3 :url "http://example.com/my-clojure-app"
4 :license {:name "Eclipse Public License"
5 :url "http://www.eclipse.org/legal/epl-v10.html"}
6 :dependencies [[org.clojure/clojure "1.10.3"]]
7 :main ^:skip-aot my-clojure-app.core
8 :target-path "target/%s"
9 :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:
1{:deps {org.clojure/clojure {:mvn/version "1.10.3"}
2 ring/ring-core {:mvn/version "1.9.0"}}
3 :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:
1lein new app hello-world
Navigate to the project directory:
1cd hello-world
Edit src/hello_world/core.clj:
1(ns hello-world.core)
2
3(defn -main
4 "I don't do a whole lot ... yet."
5 [& args]
6 (println "Hello, World!"))
Run the application:
1lein run
Create a new directory:
1mkdir hello-world
2cd hello-world
Create a deps.edn file:
1{:deps {org.clojure/clojure {:mvn/version "1.10.3"}}}
Create a src/hello_world/core.clj file:
1(ns hello-world.core)
2
3(defn -main
4 [& args]
5 (println "Hello, World!"))
Run the application:
1clj -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.
graph TD;
A[Project Root] --> B[project.clj];
A --> C[deps.edn];
A --> D[src/];
A --> E[resources/];
B --> F[Dependencies];
C --> F;
D --> G[Source Code];
E --> H[Resources];
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.