leiningen with multiple main classes

后端 未结 3 635
别跟我提以往
别跟我提以往 2021-02-02 13:56

I\'d like to have two main classes (or more) with leiningen, and then be able to choose which one at the java command line. For example I have:

(ns abc (:gen-cla         


        
3条回答
  •  独厮守ぢ
    2021-02-02 14:33

    What worked for me in both lein 2.7.0's run task as well as from the resulting uberjar is as follows...

    project.clj:

    (defproject many-mains "0.1.0-SNAPSHOT"
      :description "Project containing multiple main methods"
      :dependencies [[org.clojure/clojure "1.8.0"]]
      :main nil
      :target-path "target/%s"
      :profiles {:main-abc {:main many-mains.abc}
                 :main-def {:main many-mains.def}
                 :main-ghi {:main org.rekdev.mm.ghi}
                 :core {:main many-mains.core}
                 :uberjar {:aot :all}})
    

    For source like...

    $ cat src/many_mains/abc.clj
    (ns many-mains.abc
      (:gen-class))
    
    (defn -main
      ""
      [& args]
      (println "Hello, from many-mains.abc!"))
    

    This lets lein run work like...

    $ lein with-profile main-abc run
    Hello, from many-mains.abc!
    

    From the command line the '-' in many-mains needs to become a '_' which makes it a legal Java classname.

    $ java -cp target/uberjar/many-mains-0.1.0-SNAPSHOT-standalone.jar many_mains.abc
    Hello, from many-mains.abc!
    

    There seems to have been some behavior changes between Lein 2.7.0 and prior around the effect of :main nil on the MANIFEST.MF. What I've got here works like a champ in Lein 2.7.0. The full source is at https://github.com/robertkuhar/many-mains

提交回复
热议问题