First things first, get the Clojure command-line
tool. You should be able to run clj -M -e "(prn :hello-world!)"
anywhere.
Let's create an empty directory:
~ $ mkdir pinheiro ~ $ cd pinheiro ~/pinheiro $
Now let's create the
deps.edn
file.
{:paths ["src"] :deps {org.clojure/clojure {:mvn/version "1.10.1"}}}
There are 2 main keys in deps.edn
:
-
:paths
is a vector of strings, that says "in which dirs my code is" -
:deps
is a map of maven(or git) dependencies.
Clojure, like java, uses the notion of namespaces to
organize code. It isn't a good pattern to use single-segment namespaces, for example, just
pinheiro
.
We will start by creating the namespace pinheiro.main
.
It needs to be in src/pinheiro/main.clj
from the project root.
Once you declared in deps.edn
that src
is where your classpath
starts, we say that it's pinheiro/main.clj
in your classpath.
At this point you should have a structure like this:
~/pinheiro $ tree . ├── deps.edn └── src └── pinheiro └── main.clj ~/pinheiro $
The src/pinheiro/main.clj
should contain something like this:
(ns pinheiro.main) (defn -main [& argv] (prn ["Hello, here my args: " argv]))
Now, we can call clj -M -m pinheiro.main hello world
~/pinheiro $ clj -M -m pinheiro.main hello world Downloading: org/clojure/clojure/1.10.1/clojure-1.10.1.pom from central Downloading: org/clojure/spec.alpha/0.2.176/spec.alpha-0.2.176.pom from central Downloading: org/clojure/core.specs.alpha/0.2.44/core.specs.alpha-0.2.44.pom from central Downloading: org/clojure/pom.contrib/0.2.2/pom.contrib-0.2.2.pom from central Downloading: org/clojure/pom.contrib/0.2.2/pom.contrib-0.2.2.pom from central Downloading: org/clojure/clojure/1.10.1/clojure-1.10.1.jar from central Downloading: org/clojure/spec.alpha/0.2.176/spec.alpha-0.2.176.jar from central Downloading: org/clojure/core.specs.alpha/0.2.44/core.specs.alpha-0.2.44.jar from central ["Hello, here my args:" ["hello" "world"]] ~/pinheiro $
All deps downloads will be cached, in general, in ~/.m2
directory. If you run again, it will
just run.
~/pinheiro $ clj -M -m pinheiro.main hello world ["Hello, here my args:" ["hello" "world"]] ~/pinheiro $