First Clojure Project

Creating Our First Clojure Project

Let’s continue with our tutorial. Today, I will explain how we can create a Clojure project. I decided to split this article into two parts: we will explain how to create a regular project and a web project, where we will create an API for some service.

Let’s start by creating a regular project. It won’t be an API, a library, or anything like that. I just want to demonstrate how to create a project, explain its structure, and everything else.

Creating a project is very simple if you’ve installed Leiningen [I mentioned it in our first article]. If you haven’t installed it yet, do yourself a favor and install it. The project link is here: Leiningen — :)

Well, if you’ve done yourself this favor and have everything nicely installed, it’s time to create the Clojure project. Let’s go:

➜ hello-clojure git:(master) ✗ lein new hello-clojure

By running the above command, you get a directory named hello-clojure because that’s the name of the project we passed as a parameter to the new command of lein.

➜ creating-clojure-project git:(master) ✗ ls

hello-clojure

The Structure of a Clojure Project

Entering the directory, you’ll have a structure with many files and subdirectories. Don’t be alarmed or leave the article; I’ll explain each one of them in detail. Here’s the project structure:

We will explain each file and directory from top to bottom:

doc: In the doc folder, you can place the documentation for your project. The intro.md file comes by default when we create the project using lein.
    Just to clarify, the .md extension is for markdown, which is widely used by GitHub, for example.

resources: The resources directory is used to store any configuration files you might need.

src: Inside src is where we will store our Clojure code related to the project.

test: The name speaks for itself, containing all the project's test files.

CHANGELOG: This is usually where we record what changes in each version of the project, whether it's in Python, Java, Clojure, or Ruby.

LICENSE: Contains the legal information that we typically click "ACCEPT" without reading.

project.clj: While resources is used for configuration files, project.clj is where we actually configure our Clojure project.

Just so you don’t end up with a project that can’t run or show the famous “HELLO WORLD,” I’ll explain how to get it running. — :)

Open the project.clj file and add the following code after :dependencies. Don’t forget it needs to be inside the parentheses.

:main hello-clojure.core/foo

After that, we can run the project from the command line inside the project directory and see the result:

➜ hello-clojure git:(master) ✗ lein run Clojure
Clojure Hello, World!

And here we have our result without errors :)

To keep this article from being too long, I’ll divide it into parts. This part is about creating the project, and another will cover using namespaces, importing them into other namespaces, and so on.

The code will be available on GitHub, and we will update it with each article.

That’s it for today. Thanks to everyone who has read so far and for the great feedback!!!

Below are the other articles and those coming soon:

Hello World;
Creating our own functions;
Using existing Clojure functions;
Creating a Clojure project;
Seeing how we create functions and reuse them in our projects;
Creating a web project;
Using Java classes in Clojure functions;
And much more…

Cheers!