Leiningen - Quick Peek Underneath the Defproject Clojure Macro

Leiningen is a project automation tool (think build tool and them some) that uses a Clojure macro to make it easy for Clojure developers to manage their project lifecycle.

A Clojure project managed by Leiningen uses a simple clojure file called project.clj which allows developers to define a whole range of stuff about their projects. To get started you only have to define a name, a version of Clojure and any dependencies in your project.clj and Leininge does the rest.

So lets take a quick look under the hood of Leiningen and its defproject macro to see what is going on.

The defproject macro

The defproject macro when run creates a simple map of your project to work with.  Here is an example map for my project, generated by the command

lein pprint project.clj

If you add something to your project.clj file and wonder what is has changed underneath, then looking at the project map is very useful.

Using the project map to understand what dependencies you have pulled in could be a great way to streamline your project, or help debug it if something when wrong after adding a new dependency.

Leiningen also merges your profile configuration ~/.lein/profiles.clj along with your project.clj settings when creating the project map.  This can be seen in the above example.  Near the end of the file is a :plugins keyword, the following 3 lines are plugins I defined in my profile.  Leiningen will work out the smartest way to merge your profile.clj and project.clj. If in doubt, you can check the project map.

The defproject macro code

Here is the source code for the defproject macro:

1
2
3
4
5
6
7
8
(defmacro defproject
"The project.clj file must either def a project map or call this macro.
See `lein help sample` to see what arguments it accepts."
[project-name version & args]
`(let [args# ~(unquote-project (argument-list->argument-map args))
root# ~(.getParent (io/file *file*))]
(def ~'project
(make args# '~project-name ~version root#))))

You can also see the source code of the defproject macro in context of the Leingingen project at its Github repository.

Thank you.
@jr0cket


This work is licensed under a Creative Commons Attribution 4.0 ShareAlike License, including custom images & stylesheets. Permissions beyond the scope of this license may be available at @jr0cket
Creative Commons License