Browse Part I: Getting Started with Clojure

Creating a Project with tools.deps

Learn how to set up a new Clojure project using tools.deps, including writing a `deps.edn` file and running the application with the Clojure CLI.

Mastering Project Setup with tools.deps

In this section, you’ll learn how to get started with Clojure’s tools.deps to create a new project. The process involves setting up a project directory, crafting a deps.edn file, and finally, running your application using the powerful Clojure CLI. This approach allows you to take advantage of dependency management and project configuration without the heavier weight of build tools like Leiningen.

Creating Your Project Directory

To begin, you’ll create a directory that will house your new Clojure project. This is a straightforward process:

mkdir my-clojure-project
cd my-clojure-project

Writing a deps.edn File

The deps.edn file is central to defining your project’s dependencies and customization. Open your favorite text editor and create a file named deps.edn in the root of your project directory with the following content:

{:deps {org.clojure/clojure {:mvn/version "1.10.3"}}}

This file specifies that your project depends on Clojure version 1.10.3. You can add additional dependencies in a similar manner, simply listing them under the :deps key.

Running Your Application using Clojure CLI

With the setup done, you’re ready to run your application. Ensure you have the Clojure CLI tools installed. You can execute your Clojure code from the command line with:

clj -X:run

However, to define an entry point for the application, you can specify it in deps.edn under an alias:

{:deps {org.clojure/clojure {:mvn/version "1.10.3"}}
 :aliases {:run {:main-opts ["-m" "my.clojure.project.core"]}}}

Ensure there’s a corresponding namespace and file src/my/clojure/project/core.clj where your entry point function resides.

Summary

Creating projects with tools.deps and the Clojure CLI offers a lightweight and flexible approach to managing your Clojure project’s dependencies and configurations. By following these steps, you establish a solid foundation to begin developing efficient and functional Clojure applications on the JVM.

### What command do you use to create a new project directory in Unix-based systems? - [x] mkdir my-clojure-project - [ ] make-directory my-clojure-project - [ ] new my-clojure-project - [ ] touch my-clojure-project > **Explanation:** The `mkdir` command is used in Unix-based systems to create a new directory. In this context, it's utilized to initialize a new project directory for your Clojure project. ### What is the purpose of the `deps.edn` file in a Clojure project setup with tools.deps? - [x] To specify project dependencies - [ ] To define build configurations - [ ] To describe deployment options - [ ] To write Clojure code > **Explanation:** The `deps.edn` file is crucial for specifying project dependencies when using `tools.deps`. It outlines the libraries your Clojure application relies on. ### How do you specify the Clojure version in a `deps.edn` file? - [x] Use the key org.clojure/clojure with `:mvn/version "1.10.3"` - [ ] Specify version in a .clj file - [ ] Define version in a README.md - [ ] Set version in a Makefile > **Explanation:** In the `deps.edn` file, the key `org.clojure/clojure` with `:mvn/version` is used to specify the required Clojure version for the project. ### What CLI command runs your Clojure application configured with tools.deps? - [x] clj -X:run - [ ] clj -exec - [ ] clj -init - [ ] clj -compile > **Explanation:** The command `clj -X:run` is used to run a Clojure application that has an alias with main options defined in the `deps.edn` file. ### For the Clojure CLI to execute an application, which setup is required in `deps.edn`? - [x] Define a `:run` alias with `:main-opts` - [ ] Establish a `:start` alias with command paths - [ ] Set a `:launch` alias with compile instructions - [ ] Create a `:play` alias with script details > **Explanation:** In `deps.edn`, you define a `:run` alias, including `:main-opts` to specify the entry point, allowing the Clojure CLI (`clj`) to execute the application.

By following this detailed guide, you’re now equipped to create efficient Clojure projects using tools.deps on the JVM, seamlessly integrating functional programming principles into your development workflow. Start your journey with Clojure and elevate your coding expertise today!

Saturday, October 5, 2024