This is a study note of the Clojure Leiningen
. It is based on the official tutorial.
1 Introduction
Leiningen is a build automation and dependency management tool for Clojure project. Following are the commonly-used tasks:
lein new [TEMPLATE] NAME
: generate a new project skeleton. Useapp
template for an aplication. The default is library template. There are thousands of templates in lein-template.lein test [TESTS]
: run the tests in the TESTS namespaces, or all testslein repl
: launch an interactive REPL sessionlein run -m my.namespace
: run the-main
function of a namespacelein uberjar
: package the project and dependencies as standalone jarlein deploy clojars
: publish the project to Clojars as a librarylein search $TERM
: search macthing jars
Use lein help
to see a list of tasks. Use lein help $TASK
provides details. You can also chain tasks together in a single command by using the do
task with comma-separated tasks: lein do clean, test foo.test-core, jar
.
2 Projects
A project is a directory containing a group of Clojure (and possibly Java) source files, along with a bit of metadata about them. The metadata is stored in a file named project.clj
in the project’s root directory, which has informaiotn about
- Project name and description
- Dependent libraries
- Clojure version
- Source file paths
- Main namespace of the application
The sample.project.clj
is annotated reference of the options.
The main source file is usually src/my_stuff/core.clj
and the main namespace is my-stuff.core
instead of just my-stuff
because single-segment namespace implies classes assigned to the default (no-name) package. A dash in a Clojure symbol is mapped to a Java-compatible underscore therefore the pathname must use underscore.
3 Dependencies
Published libs following the Maven naming: [group-id/artifact-id version]
. You can search Clojure libs in Clojars and Maven Central. You can add third-party repositories by setting the :repositories
key in project.clj
.
Run lein install
and restart your repl all the time to get your changes picked up. Create a checkouts
directory in th eproject root and create symlinks to the root directories of projects you need. Libraries located under the checkouts directory take precedence over libraries pulled from repositories. You still need to declare them in :dependencies
.
4 Profiles
Profiles define different contexts. Profile contents will be merged into your project map. For example, you can add directories containing config files to your classpath via :resource-paths
or by other means.
Leiningen merges the default set of profiles into the project map. This includes user-wide :user
profile, the :dev
profile from project.clj
, and the built-in :base
profile which contains dev tools like nREPL
and optimizations which help startup time at the expense of runtime performance.