Clojure: Re-running programs from the Lein REPL

I have started playing with Clojure lately and I found the Clojure start up time quite slow. Every time I make a change and re-run using:

I have to wait quite a bit of time.

For quick prototyping, a better approach is to run the repl using:

We run our main function from the repl using (-main). when we make changes – we reload our code and then run the “-main” function again. To follow this approach, we have to make sure that we added [org.clojure/tools.namespace “0.2.7”] to our leiningen dependencies list (add this to the project.clj file inside your project). Then launch the REPL and type these code:

The (refresh) call would reload the changed namespaces and (-main) would run the main function again.

So that works but every time we open the REPL, we have to type the require command in. One of the awesome features of Leiningen is it allows us to load source codes from additional paths based on profiles. We would add a dev profile and load some codes from a different path. These codes will be loaded by leiningen while running the app but won’t be a part of our JAR file.

To add a dev profile, let’s update the “project.clj”:

Now in our project root, if we create a file “startup/user.clj”, Leiningen would load this file on startup.

So we add these code to the “startup/user.clj” file:

Note that the refresh function takes an “:after” option. If we pass a fully qualified function name, it will call that function after reloading the namespace.

Now any time we make changes to our files, we can just call (user/rerun) and it would reload the libraries and execute the “-main” function.


Leave a Reply

Your email address will not be published. Required fields are marked *