如何在deps.edn中设置*print-length*默认值(替代Leiningen的project.clj)
*print-length* in deps.edn? Great question! Unlike Leiningen's project.clj which had straightforward profile-based options for setting dynamic vars like *print-length*, deps.edn requires a bit more explicit setup—but there are several clean, reliable approaches to get this working. Here are the most common methods:
1. Use a user.clj namespace (simplest approach)
Clojure automatically loads a user namespace from your classpath when starting a REPL, making this the easiest way to set default values for dynamic vars.
- Create a
src/user.cljfile (or add to an existing one) with:(ns user) ;; Set your desired default print length (set! *print-length* 10) - Since
src/is included in the default classpath for deps.edn projects, just start your REPL withclj—theusernamespace will load automatically, and*print-length*will be set to your chosen value.
2. Inject initialization code via JVM options
You can pass a Clojure expression directly to the JVM to execute on startup using the clojure.main.init system property. This works well if you want to tie the setting to a specific alias.
Add this to your deps.edn:
{:aliases {:dev {:jvm-opts ["-Dclojure.main.init='(set! *print-length* 10)'"]}}}
Then start your REPL with:
clj -A:dev
Note: If you're using Windows Command Prompt, you may need to adjust the quoting (e.g., use double quotes around the entire JVM option and escape inner quotes).
3. Load a dedicated initialization namespace
If you prefer to keep your setup organized in a separate namespace (instead of user.clj), you can create an init namespace and load it on startup.
- First, create
src/dev/init.clj:(ns dev.init) (set! *print-length* 10) ;; Add other dev-specific settings here if needed - Update your
deps.ednto include thesrc/devpath and load the namespace on startup:{:aliases {:dev {:extra-paths ["src/dev"] :main-opts ["-e" "(do (require 'dev.init) (clojure.main/repl))"]}}} - Start your REPL with
clj -A:dev—this will load thedev.initnamespace (setting*print-length*) and then launch the REPL.
All of these methods will ensure *print-length* is set to your preferred default every time you start your Clojure REPL via deps.edn. The user.clj approach is generally the most low-friction for most projects!
内容的提问来源于stack exchange,提问作者Mert Nuhoglu

