AOT-compile all namespaces in a Clojure project – :aot :all in project.clj (leiningen)

It’s been a couple of times when I ran across a compilation issue while starting up the librarian-clojure project. I guess refactoring in Clojure or any dynamic programming language is not as easy to achieve as in static languages, and perhaps Ahead-of-time (AOT) Compilation could be a solution. I’m yet to join a project with Clojure (or JRuby) as the language so I may find it out one day.

I use lein to manage Clojure projects and learnt to use lein compile.

There’s an issue with lein compile – it doesn’t compile all the available sources which was the feature I was looking for.

To use lein compile you’ll have to define :main [namespace] which will only compile the main namespace pointed out by the namespace option.

(defproject librarian-clojure "0.0.1-SNAPSHOT"
  :description "Book manager in Clojure"
  :url "http://github.com/jaceklaskowski/librarian-clojure"
  ;; removed to ease reading
  :main librarian-clojure.run)

Run lein compile and you’ll see your main namespace compiled.

jacek:~/oss/librarian-clojure
$ lein compile
Compiling librarian-clojure.run
Compilation succeeded.

I often consult a source code to find answers if it’s available. The sources of the compile task are available, and it’s just a matter of time how much you’ll gain studying the sources. I don’t think you need much to learn a lot.

At line 30 of the compile task I found the value :all of the :aot option. That was it!

(defproject librarian-clojure "0.0.1-SNAPSHOT"
  :description "Book manager in Clojure"
  :url "http://github.com/jaceklaskowski/librarian-clojure"
  ;; removed to ease reading
  :main librarian-clojure.run
  :aot :all)

With the change in project.clj, lein compile now processes all the namespaces in the project. Huurray!

jacek:~/oss/librarian-clojure
$ lein compile
Compiling librarian-clojure.run
Compiling librarian-clojure.books
Warning: *server* not declared dynamic and thus is not dynamically rebindable, but its name suggests otherwise. Please either indicate ^:dynamic *server* or change the name.
Compiling librarian-clojure.core
Compiling librarian-clojure.repl

Type (go) to launch the server and run a browser.
Type (stop) to stop the server.

Compiling librarian-clojure.run
Compilation succeeded.
Be Sociable, Share!
This entry was posted in Languages.

One Response to AOT-compile all namespaces in a Clojure project – :aot :all in project.clj (leiningen)

  1. Alex Ott says:

    You can also specify regex as parameter for :compile option – it will compile only matched namespaces

Leave a Reply

%d bloggers like this: