Search code examples
clojureleiningen

Setting up leiningen profiles to multiplex between environments AND multiple mains


I currently have a setup for my Clojure project which allows me to toggle configuration from dev to test or prod by configuring profiles in Leiningen's project map. The section related to the profiles looks like this:

  :main ^:skip-aot myproject.core
  :target-path "target/%s"

  :profiles {:uberjar    {:aot :all}
             :dev        {:env {:clj-env :development
                                :database-uri "jdbc:postgresql://localhost:5432/db_dev"}}
             :test       {:env {:clj-env :test
                                :database-uri "jdbc:postgresql://localhost:5432/db_test"}}
             :production {:env {:clj-env :production
                                :database-uri "jdbc:postgresql://localhost:5432/db"}}})

The issue is I would further like to enhance this and be able to toggle between multiple mains. I have seen in other posts that people usually achieve this by configuring the profiles like here.

What I don't know how to do is how to preserve the configuration I have so that profiles correspond to environments but also further configure it in order to be able to choose the main class by simply adding a parameter to lein run.

I have figured out that one way is obviously to keep having just one main class and add that multiplexing with actual Clojure code, but I was hoping to be able to do it via lein configuration.


Solution

  • After trying multiple options, the only way I have found to do this is via aliases.

    First setting the :main option to nil, so the MANIFEST.MF doesn't have any Main.class set, and then simply adding a couple of aliases specifying which main class to run.

    :main nil
    :target-path "target/%s"
    :aliases {"main1" ["run" "-m" "project.main1.core"]
              "main2" ["run" "-m" "project.main2.core"]}