Alex Collins – Blog

Things I'm learning; lessons or otherwise…

Leiningen and standalone clojure programs

by atc on January 27, 2012, no comments

Hacking away at various clojure-related stuff lead me to start using Leiningen. “Lein” is a build tool similar to Maven (and interoperates in fact…) to help you concentrate on coding.

At some point in the day’s coding I wanted to run my code standalone instead of in a REPL. I needed a main entry point to the various algorithms I was writing that was outside of unit tests, so I decided to use the “lein run” command to do it for me.

In order to get a program running via lein using a main method, you have to provide the namespace of the class to run. If you want a JAR produced that’ll have the Main-Class declaration in your MANIFEST.MF you also need to add :aot and :main to your project.clj AND add (:gen-class) in the (ns) call of your desired .clj holding a -main method. Phew!

To re-cap, then:

  • To run a .clj that has a -main method/function declared, add (:gen-class) to its (ns) call and then run lein run
  • To produce a JAR with lein, do the above and add :aot and :main to your project.clj then run lein compile && lein uberjar