简体   繁体   English

莱宁根有多个主要班级

[英]leiningen with multiple main classes

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. 我希望有两个主要类(或更多)与leiningen,然后能够在java命令行中选择哪一个。 For example I have: 例如,我有:

(ns abc (:gen-class))
(defn -main [] (println "abc"))

(ns def (:gen-class))
(defn -main [] (println "def"))

With a project.clj having: 使用project.clj:

(defproject my-jar "0.0.1"
 :description "test"
 :dependencies [
 ]
 :main abc)

Then I build with lein uberjar , and run: 然后我用lein uberjar构建,运行:

java -cp my-jar-0.0.1-standalone.jar abc
java -cp my-jar-0.0.1-standalone.jar def

I get it that when I specified :main abc in the project.clj it was calling that out as the main-class in the manifest, but I couldn't get it to run without putting something . 我明白当我指定:main abc project.clj中的:main abc时,它会将其作为清单中的主类调用,但是如果没有放入某些东西我就无法运行 But either way when I try to run the 'def' main, I get a class not found: 但无论哪种方式,当我尝试运行'def'主要时,我得到一个未找到的类:

Exception in thread "main" java.lang.NoClassDefFoundError: def

This works at least with leiningen 2.0+ 这至少适用于leiningen 2.0+

(defproject my-jar "0.0.1"
 :description "test"
 :dependencies [
 ]
 :profiles {:main-a {:main abc}
           {:main-b {:main def}}
 :aliases {"main-a" ["with-profile" "main-a" "run"]
           "main-b" ["with-profile" "main-b" "run"]})

Then you can run each main like so: 然后你可以这样运行每个主要:

lein main-a
lein main-b

Which expands to this: 其中扩展到:

lein with-profile main-a run
lein with-profile main-b run

I'm using this in one of my projects and it works perfectly. 我在我的一个项目中使用它,它完美无缺。

我添加了:aot [abc def]到project.clj来生成编译代码并且它工作正常。

What worked for me in both lein 2.7.0's run task as well as from the resulting uberjar is as follows... 在lein 2.7.0的运行任务以及由此产生的uberjar中对我有用的内容如下......

project.clj: 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像...一样运行

$ 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类名。

$ 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. Lein 2.7.0和之前的一些行为似乎已经发生了变化:主要是在MANIFEST.MF上的nil。 What I've got here works like a champ in Lein 2.7.0. 我在这里的作品就像Lein 2.7.0中的冠军一样。 The full source is at https://github.com/robertkuhar/many-mains 完整的来源是https://github.com/robertkuhar/many-mains

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM