Getting Started with Clojure Deps
I’ve been wanting to really get into using Clojure with the newer and preferred EDN tooling, over the older Leinengen tooling.
Initial Setup
First we’ll set up our directory and write a Hello World application.
brew install clojure
mkdir learn-deps
cd learn-deps
touch deps.edn
then in deps.edn
,
{
:paths ["src/clj"]
:deps {org.clojure/clojure {:mvn/version "1.12.1"}
}
mkdir -p src/clj/learn_deps
cd src/clj/learn_deps
touch core.clj
(ns learn-deps.core)
(defn -main []
(println "Hello World!")
)
Then run from the root learn-deps/
directory as,
clj -M -m lean-deps.core
Using Aliases
We can simplify this command by adding an alias to deps.den
,
{
:paths ["src/clj"]
:deps {org.clojure/clojure {:mvn/version "1.12.1"}
:aliases {:run {:main-opts ["-m" "learn-deps.core"]}}
}
Then we can run this with the :run
alias as,
clj -M:run
Using Calva from VS Code
As much as I love vim, I switched to VS Code for it’s Calva integration.
Start an interactive REPL with Calva in VS Code by adding this to deps.edn
,
{
:paths ["src/clj"]
:deps {org.clojure/clojure {:mvn/version "1.12.1"}}
:aliases {
:run {:main-opts ["-m" "learn-deps.core"]}
:repl {:extra-deps {nrepl/nrepl {:mvn/version "1.3.1"}}}
}
}
Then in VS Code, with Calva installed, type >jack
into the command palette,
and select, Calva: Start a PRoject REPL and Connect...
.
Calva should start up a REPL, then we can look for >load
in the command palette,
and select, Calva: Load/Evaluate Current File...
Then in the REPL window you can run (-main)
and see the output of your main function.
References
This is adapted and update from an older YouTube video by Kelvin Mai.