Learn how to create a Clojure project using tools.deps, understand the structure of a deps.edn file, and run your application with the Clojure CLI. This guide is tailored for Java developers transitioning to Clojure.
As experienced Java developers, you are likely familiar with Maven or Gradle for managing dependencies and building projects. In Clojure, tools.deps is a powerful alternative that offers a flexible and straightforward way to manage dependencies and run your applications. In this section, we’ll guide you through setting up a new Clojure project using tools.deps, writing a deps.edn
file, and running your application using the Clojure CLI.
Before we dive into the specifics of the deps.edn
file, let’s start by setting up a new project directory. This is similar to creating a new project in Java, where you would typically set up a directory structure and configuration files.
Create a New Directory: Open your terminal and create a new directory for your Clojure project. You can name it anything you like, but for this example, we’ll call it my-clojure-app
.
mkdir my-clojure-app
cd my-clojure-app
Initialize the Project Structure: Unlike Java, where you might have a complex directory structure, Clojure projects are often simpler. For now, we’ll just create a src
directory to hold our source code.
mkdir src
deps.edn
File§The deps.edn
file is the heart of a Clojure project using tools.deps. It defines the dependencies and configuration for your project, similar to a pom.xml
in Maven or a build.gradle
in Gradle.
Create a deps.edn
File: In the root of your project directory, create a file named deps.edn
.
touch deps.edn
Define Project Dependencies: Open the deps.edn
file in your favorite text editor and define your project dependencies. Here’s a simple example:
{:deps {org.clojure/clojure {:mvn/version "1.11.1"}}}
:deps
key is a map where each key is a dependency identifier, and the value is a map specifying the version and other options.Add Aliases for Custom Tasks: You can define aliases in the deps.edn
file to simplify running custom tasks. For example, you might want to create an alias for running a REPL or executing your application.
{:aliases
{:run {:main-opts ["-m" "my-clojure-app.core"]}}}
:aliases
key allows you to define custom tasks. In this example, the :run
alias specifies that when we run this alias, it should execute the my-clojure-app.core
namespace.With your project directory and deps.edn
file set up, you’re ready to run your application using the Clojure CLI.
Start a REPL: You can start a REPL (Read-Eval-Print Loop) to interactively develop and test your code. In your terminal, run:
clj
Run Your Application: To run your application, use the alias you defined in the deps.edn
file. In this case, we’ll use the :run
alias.
clj -M:run
-M
flag tells the Clojure CLI to use the :main-opts
specified in the alias. This command will execute the my-clojure-app.core
namespace.deps.edn
File Structure§The deps.edn
file is a flexible and powerful way to manage your project’s dependencies and configuration. Let’s break down its structure and capabilities.
The deps.edn
file is a Clojure map with several keys that you can use to configure your project:
:deps
: A map of dependencies, where each key is a dependency identifier (usually a Maven coordinate), and the value is a map specifying the version and other options.:aliases
: A map of aliases, which are custom tasks or configurations that you can run with the Clojure CLI.:paths
: A vector of paths to include in the classpath. By default, this includes src
and resources
.deps.edn
File§Here’s a more comprehensive example of a deps.edn
file:
{:deps {org.clojure/clojure {:mvn/version "1.11.1"}
org.clojure/tools.logging {:mvn/version "1.1.0"}}
:paths ["src" "resources"]
:aliases
{:dev {:extra-paths ["dev"]
:extra-deps {org.clojure/tools.namespace {:mvn/version "1.0.0"}}
:main-opts ["-m" "my-clojure-app.core"]}}}
:dev
alias for development, which adds extra paths and dependencies.deps.edn
with Java’s Maven/Gradle§For Java developers, the deps.edn
file might seem similar to Maven’s pom.xml
or Gradle’s build.gradle
. However, there are some key differences:
deps.edn
file is typically simpler and more concise than a pom.xml
or build.gradle
. It focuses on dependencies and configuration without the need for complex XML or DSL syntax.deps.edn
file uses Clojure syntax, which means you can leverage your knowledge of Clojure to understand and modify it.Now that we’ve covered the basics of creating a project with tools.deps, it’s time to try it yourself. Here are some exercises to help you practice:
Add a New Dependency: Modify your deps.edn
file to include a new dependency, such as cheshire
for JSON parsing. Run your application and use the new library in your code.
Create a New Alias: Define a new alias in your deps.edn
file for running tests. Use the clojure.test
library to write and run a simple test.
Experiment with Paths: Add a new directory to your project (e.g., test
) and include it in the :paths
vector. Create a new Clojure file in this directory and run it using the Clojure CLI.
To help visualize the structure of a Clojure project and the flow of data through your application, let’s include some diagrams.
Caption: This diagram illustrates the typical structure of a Clojure project using tools.deps, with directories for source code, resources, and development.
graph TD; A[User Input] --> B[REPL] B --> C[Evaluate Expression] C --> D[Output Result]
Caption: This diagram shows the flow of data through a Clojure REPL session, from user input to evaluation and output.
deps.edn
file is the central configuration file, similar to Maven’s pom.xml
or Gradle’s build.gradle
.deps.edn
file to customize your project setup.By understanding and utilizing tools.deps, you can streamline your Clojure development process and take advantage of Clojure’s powerful features. Now that we’ve explored how to create a project with tools.deps, let’s apply these concepts to build and manage your Clojure applications effectively.
Dependency Management: Add a new dependency to your deps.edn
file and use it in your application. Try using a library like clj-http
for making HTTP requests.
Custom Aliases: Create a custom alias for running a specific task, such as generating documentation or building a JAR file.
Project Structure: Experiment with different project structures by adding new directories and files. Update your deps.edn
file to reflect these changes.
REPL Exploration: Use the REPL to explore and test different Clojure functions and libraries. Try writing a small script to automate a task.
Compare with Java: Create a simple Java project with Maven or Gradle and compare the setup process with your Clojure project using tools.deps.
These resources provide additional information and examples to help you deepen your understanding of tools.deps and Clojure development.