How does one pre-load a clojure file in the leiningen repl?
Asked Answered
P

1

13

I have some clojure functions that I would like pre-loaded when I start the clojure REPL. The functions aren't much use unless you are using them within the context of a REPL.

If it helps, I generally use leiningen to start a clojure REPL for me.

How can I tell clojure (or leiningen, if it's not available through flat clojure) to pre-load a clojure file containing these definitions for me?

Poucher answered 31/8, 2013 at 19:20 Comment(0)
H
15

There are several ways to do this described in the leiningen sample project one of my favorite methods is so put the code you want in the default repl namespace into

/path/to/project/dev/user.clj:

(ns user)
(def foo 42)

and add a line like this into the project.clj file:

(defproject hello "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url "http://example.com/FIXME"
  :license {:name "Eclipse Public License"
            :url "http://www.eclipse.org/legal/epl-v10.html"}
  :dependencies [[org.clojure/clojure "1.5.1"]]
  :source-paths ["dev"])

This makes it clear that this is for dev while still getting it loaded into the default namespace.

When you run nrepl-jack-in form emacs or "lein repl" form the shell, you should be greeted with a user> namespace with your code loaded:

; nREPL 0.1.6
user> foo
42
Hagy answered 1/9, 2013 at 2:40 Comment(4)
+1 for the excellent link. Could you also include in your answer a way to do this for any project (e.g., using '~/.lein/profiles.clj') and also assume that I'm not using emacs (i.e., no 'nrepl-jack-in', just the normal terminal screen)?Poucher
Added part of this to the anwser, and for settings this globally it may be preferable to use :repl-options :init in a profile in your .lein/profiles.clj to using defining a user namespace.Hagy
I think it would be more appropriate if you put the extra :source-paths entry inside a profile. The :dev profile would be particularly appropriate I think. As the answer stands, whatever code is under dev while still be included in the project all the time.Doe
I had to include "src" too: :source-paths ["src" "dev"] otherwise I could load my namespaces.Ligate

© 2022 - 2024 — McMap. All rights reserved.