Search code examples
clojureleiningen

How to run library's `main` with Leiningen?


I had used some library, let's say it is Clojure itself (which is always added to project.clj). Clojure provides clj CLI tool (which is src/cli/clojure/main.clj, but nevermind). How to use it with lein? I mean, is there any command/plugin/technique which will allow me to use library's main?


Solution

  • Every Var in every namespace is equal in the eyes of Clojure. From your code, just execute like:

       (some.awesome.lib/-main ...)
    

    or whatever the fully-qualified symbol pointing to the Var in question.

    For further details, please see this question:


    Also

    See the output of

    > lein help run
    

    Using lein, you can type

    lein run -m my.awesome.proj/some-fn
    

    or

    lein run -m some.awesome.lib/-main
    

    since to Clojure some.awesome.lib/-main is no different than any other function (the hyphen prefix on -main is just a convention and makes no difference to the Clojure compiler).

    You can also set up project.clj to automatically call any function of your choosing when you type lein run by adding:

    :main some.awesome.lib/-main