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 namespace.here 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
-mainmethod/function declared, add(:gen-class)to its(ns)call and then runlein run namespace.here - To produce a JAR with lein, do the above and add
:aotand:main namespace.hereto your project.clj then runlein compile && lein uberjar