Movatterモバイル変換


[0]ホーム

URL:


Clojure logo
Clojure
Deps and CLI Guide

Deps and CLI Guide

Clojure provides command line tools for:

  • Running an interactive REPL (Read-Eval-Print Loop)

  • Running Clojure programs

  • Evaluating Clojure expressions

In all the above scenarios you might want to use other Clojure and Java libraries (dependencies or 'deps'). These may be libraries you are writing locally, projects in git (e.g. on GitHub) or, commonly, libraries available in the Maven ecosystem and hosted by central repositories like Maven Central or Clojars.

In all cases, using a library involves:

  1. specifying which library you want to use, providing its name and other aspects like version

  2. getting it (once) from the git or maven repositories to your local machine

  3. making it available on the JVM classpath so Clojure can find it while your REPL or program is running

Clojure tools specify a syntax and file (deps.edn) for (a), given which they’ll handle (b) and (c) automatically.

SeeGetting Started for details on how to install the tools. Here we will demonstrate how to get started. SeeClojure CLI anddeps.edn for a complete reference. See thechangelog for version information.

Running a REPL and using libraries

After you download and install the tools, you can start a REPL by running theclj tool:

$ cljClojure 1.12.0user=>

Once in the REPL you can type Clojure expressions and press enter to evaluate them. Type Control-D to exit the REPL:

user=> (+ 2 3)   # press the `enter` key after typing the expression to evaluate it5                # result of expressionuser=>           # type Ctrl-D here to exit the REPL (does not print)$

There are many Clojure and Java libraries available that provide access to practically any functionality you might need. For example, consider the commonly used Clojure libraryclojure.java-time for working with dates and times.

To work with this library, you need to declare it as a dependency so the tool can ensure it has been downloaded and add it to the classpath. The readme in most projects shows the name and version to use. Create adeps.edn file to declare the dependency:

{:deps {clojure.java-time/clojure.java-time {:mvn/version "1.1.0"}}}

Alternately, if you don’t know the version, you can use thefind-versions tool which will list all available coordinates in sorted order:

$ clj -X:deps find-versions :lib clojure.java-time/clojure.java-time...omitted{:mvn/version "1.0.0"}{:mvn/version "1.1.0"}

Restart the REPL with theclj tool:

$ cljDownloading: clojure/java-time/clojure.java-time/1.1.0/clojure.java-time-1.1.0.pom from clojarsDownloading: clojure/java-time/clojure.java-time/1.1.0/clojure.java-time-1.1.0.jar from clojarsClojure 1.12.0user=> (require '[java-time.api :as t])niluser=> (str (t/instant))"2022-10-07T16:06:50.067221Z"

You will see messages about a library being downloaded the first time you use a dependency. Once the file is downloaded (usually to~/.m2 or~/.gitlibs), it will be reused in the future. You can use the same process to add other libraries to yourdeps.edn file and explore Clojure or Java libraries.

Writing a program

Soon you will want to build and save your own code that makes use of these libraries. Create a new directory and copy thisdeps.edn into it:

$ mkdir hello-world$ cp deps.edn hello-world$ cd hello-world$ mkdir src

By default, theclj tool will look for source files in thesrc directory. Createsrc/hello.clj:

(ns hello  (:require [java-time.api :as t]))(defn time-str  "Returns a string representation of a datetime in the local time zone."  [instant]  (t/format    (t/with-zone (t/formatter "hh:mm a") (t/zone-id))    instant))(defn run [opts]  (println "Hello world, the time is" (time-str (t/instant))))

Using a main

This program has an entry functionrun that can be executed byclj using-X:

$ clj -X hello/runHello world, the time is 12:19 PM

Using local libraries

You might decide to move part of this application into a library. Theclj tool uses local coordinates to support projects that exist only on your local disk. Let’s extract the java-time parts of this application out into a library in a parallel directory time-lib. The final structure will look something like this:

├── time-lib│   ├── deps.edn│   └── src│       └── hello_time.clj└── hello-world    ├── deps.edn    └── src        └── hello.clj

Under time-lib, use a copy of thedeps.edn file you already have, and create a filesrc/hello_time.clj:

(ns hello-time  (:require [java-time.api :as t]))(defn now  "Returns the current datetime"  []  (t/instant))(defn time-str  "Returns a string representation of a datetime in the local time zone."  [instant]  (t/format    (t/with-zone (t/formatter "hh:mm a") (t/zone-id))    instant))

Update the application athello-world/src/hello.clj to use your library instead:

(ns hello  (:require [hello-time :as ht]))(defn run [opts]  (println "Hello world, the time is" (ht/time-str (ht/now))))

Modifyhello-world/deps.edn to use a local coordinate that refers to the root directory of the time-lib library (make sure to update the path for your machine):

{:deps {time-lib/time-lib {:local/root "../time-lib"}}}

You can then test everything from the hello-world directory by running the application:

$ clj -X hello/runHello world, the time is 12:22 PM

Using git libraries

It would be great to share that library with others. You can accomplish this by pushing the project to a public or private git repository and letting others use it with a git dependency coordinate.

First, create a git library for the time-lib:

cd ../time-libgit initgit add deps.edn srcgit commit -m 'init'

Then go to a public git repository host (like GitHub) and follow the instructions for creating and publishing this git repository.

We also want to tag this release so it has a meaningful version:

git tag -a 'v0.0.1' -m 'initial release'git push --tags

Finally, modify your app to use the git dependency instead. You’ll need to gather the following information:

  • repository lib - the Clojure CLI uses a convention where the URL does not need to be specified if you use a library name likeio.github.yourname/time-lib for the GitHub urlhttps://github.com/yourname/time-lib.git.

  • tag -v0.0.1 is what we created above

  • sha - the short sha at the tag, find it withgit rev-parse --short v0.0.1^{commit} if you have the repo locally, orgit ls-remotehttps://github.com/yourname/time-lib.git v0.0.1 if it’s remote. You can also find it by using the GitHub repo to look at tags and their backing commit.

Update thehello-world/deps.edn to use a git coordinate instead:

{:deps {io.github.yourname/time-lib {:git/tag "v0.0.1" :git/sha "4c4a34d"}}}

Now you can run the app again, making use of the (shared) git repository library. The first time you run it you’ll see extra messages on the console whenclj downloads and caches the repository and the commit working tree:

$ clj -X hello/runCloning: https://github.com/yourname/time-libChecking out: https://github.com/yourname/time-lib at 4c4a34dHello world, the time is 02:10 PM

Now your friends can usetime-lib too!

Other examples

As your program gets more involved you might need to create variations on the standard classpath. The Clojure tools supports classpath modifications using aliases, which are parts of the deps file that are only used when the corresponding alias is supplied. Some of the things you can do are:

Include a test source directory

Typically, the project classpath includes only the project source, not its test source by default. You can add extra paths as modifications to the primary classpath in the make-classpath step of the classpath construction. To do so, add an alias:test that includes the extra relative source path"test":

{:deps {org.clojure/core.async {:mvn/version "1.3.610"}} :aliases {:test {:extra-paths ["test"]}}}

Apply that classpath modification and examine the modified classpath by invokingclj -A:test -Spath:

$ clj -A:test -Spathtest:src:/Users/me/.m2/repository/org/clojure/clojure/1.12.0/clojure-1.12.0.jar:... same as before (split here for readability)

Note that the test dir is now included in the classpath.

Use a test runner to run all tests

You can extend the:test alias in the previous section to include the cognitect-labstest-runner for running all clojure.test tests:

Extend the:test alias:

{:deps {org.clojure/core.async {:mvn/version "1.3.610"}} :aliases {:test {:extra-paths ["test"]         :extra-deps {io.github.cognitect-labs/test-runner {:git/tag "v0.5.1" :git/sha "dfb30dd"}}         :main-opts ["-m" "cognitect.test-runner"]         :exec-fn cognitect.test-runner.api/test}}}

And then execute the test runner using the default config (run all tests in -test namespaces under the test/ dir):

clj -X:test

Add an optional dependency

Aliases in thedeps.edn file can also be used to add optional dependencies that affect the classpath:

{:aliases {:bench {:extra-deps {criterium/criterium {:mvn/version "0.4.4"}}}}}

Here the:bench alias is used to add an extra dependency, namely the criterium benchmarking library.

You can add this dependency to your classpath by adding the:bench alias to modify the dependency resolution:clj -A:bench.

Add a dependency from the command line

It can be helpful to experiment with a library without adding it to an existingdeps.edn file or creating one.

$ clojure -Sdeps '{:deps {org.clojure/core.async {:mvn/version "1.5.648"}}}'Clojure 1.12.0user=> (require '[clojure.core.async :as a])nil

Note that due to escaping rules, it’s best to wrap the config data in single quotes.

Preparing source dependency libs

Some dependencies will require a preparation step before they can be used on the classpath. These libs should state this need in theirdeps.edn:

{:paths ["src" "target/classes"] :deps/prep-lib {:alias :build                 :fn compile                 :ensure "target/classes"}}

Including the top-level key:deps/prep-lib tells the tools.deps classpath construction that something extra is needed to prepare this lib and that can be performed by invoking thecompile function in the:build alias. Once the prepare step has been done, it should create the path"target/classes" and that can be checked for completion.

You depend on this library like any other source-based library (could be git or local):

{:deps {my/lib {:local/root "../needs-prep"}}}

If you then try to include that library on your classpath you’ll see an error:

$ cljError building classpath. The following libs must be prepared before use: [my/lib]

You can then tell the CLI to prep using this command (this is a 1-time action for a particular lib version):

$ clj -X:deps prepPrepping io.github.puredanger/cool-lib in /Users/me/demo/needs-prep$ cljClojure 1.12.0user=>

Override a dependency

You can use multiple aliases in combination. For example thisdeps.edn file defines two aliases -:old-async to force the use of an older core.async version and:bench to add an extra dependency:

{:deps {org.clojure/core.async {:mvn/version "0.3.465"}} :aliases {:old-async {:override-deps {org.clojure/core.async {:mvn/version "0.3.426"}}}  :bench {:extra-deps {criterium/criterium {:mvn/version "0.4.4"}}}}}

Activate both aliases as follows:clj -A:bench:old-async.

Include a local jar on disk

Occasionally you may need to refer directly to a jar on disk that is not present in a Maven repository, such as a database driver jar.

Specify local jar dependencies with a local coordinate that points directly to a jar file instead of a directory:

{:deps {db/driver {:local/root "/path/to/db/driver.jar"}}}

Ahead-of-time (AOT) compilation

When usinggen-class orgen-interface, the Clojure source must be ahead-of-time compiled to generate the java class(es).

This can be done by callingcompile. The default destination for compiled class files isclasses/, which needs to be created and added to the classpath:

$ mkdir classes

Editdeps.edn to add"classes" to the paths:

{:paths ["src" "classes"]}

Declare a class with gen-class insrc/my_class.clj:

(ns my-class)(gen-class  :name my_class.MyClass  :methods [[hello [] String]])(defn -hello [this]  "Hello, World!")

Then you can reference the class with:import in another source filesrc/hello.clj. Notice that the namespace is also added in:require so compilation can automatically find all dependent namespaces and compile them.

(ns hello  (:require [my-class])  (:import (my_class MyClass)))(defn -main [& args]  (let [inst (MyClass.)]    (println (.hello inst))))

You can compile in the REPL or run a script to do the compilation:

$ clj -M -e "(compile 'hello)"

And then run the hello namespace:

$ clj -M -m helloHello, World!

SeeCompilation and Class Generation for a complete reference.

Run a socket server remote repl

Clojure provides built-in support for runningsocket servers, and in particular using them to host remote REPLs.

To configure a socket server repl, add the following base configuration to yourdeps.edn:

{:aliases {:repl-server  {:exec-fn clojure.core.server/start-server   :exec-args {:name "repl-server"               :port 5555               :accept clojure.core.server/repl               :server-daemon false}}}}

And then start the server by invoking with the alias:

clojure -X:repl-server

If you like, you can also override the default parameters (or add additional options) on the command line:

clojure -X:repl-server :port 51234

You can use netcat to connect from another terminal:

nc localhost 51234user=> (+ 1 1)2

Use Ctrl-D to exit the repl and Ctrl-C to exit the server.

List all dependencies

There are several helpful tools in the built-in:deps alias to explore the full set of transitive deps used by your project (and their licenses).

Tolist the full set of all the deps included on your classpath, useclj -X:deps list. For example in thehello-world application at the top of this guide, you would see something like this:

% clj -X:deps listclojure.java-time/clojure.java-time 1.1.0  (MIT)org.clojure/clojure 1.12.0  (EPL-1.0)org.clojure/core.specs.alpha 0.2.62  (EPL-1.0)org.clojure/spec.alpha 0.3.218  (EPL-1.0)time-lib/time-lib ../cli-getting-started/time-lib

The full set of transitive dependencies used by your application is listed in alphabetical order with version and license. See the api docs for additional printing options.

If you want to understand thetree structure of your dependencies and how version selection choices were made, useclj -X:deps tree:

% clj -X:deps treeorg.clojure/clojure 1.12.0  . org.clojure/spec.alpha 0.3.218  . org.clojure/core.specs.alpha 0.2.62time-lib/time-lib /Users/alex.miller/tmp/cli-getting-started/time-lib  . clojure.java-time/clojure.java-time 1.1.0

There were no version selections made here, but seethe docs for more on how choices are explained in the tree if needed.

Both of these helper functions take an optional:aliases argument if you wish to examine the dependency list or tree with one or more aliases applied, such asclj -X:deps list :aliases '[:alias1 :alias2]'.

Original author: Alex Miller

Community
ResourcesContributingCompaniesSite
Legal
LicensePrivacy Policy
Documentation
OverviewReferenceAPIGuidesLibraries & Tools
Updates
NewsEvents
ETC
ClojureTVBooksSwag
Code
ReleasesSourceClojureScriptClojureCLR
 
Copyright 2008-2022 Rich Hickey |Privacy Policy
Logo & site design by Tom Hickey
Published 2025-07-07
Updatethis page

[8]ページ先頭

©2009-2025 Movatter.jp