lein uberjar - 没有正确设置 main class?

lein uberjar - not setting main class properly?

在新的 lein new re-frame bc +handler 应用程序中,如果我 lein uberjarlein jar 它似乎没有正确设置主 class。在编译结束时它告诉我

Warning: The Main-Class specified does not exist within the jar. It may not be executable as expected. A gen-class directive may be missing in the namespace which contains the main method, or the namespace has not been AOT-compiled.

这是使用 re-frame +handler 模板创建的 server.clj 和 project.clj:

server.clj:

(ns bc.server
  (:require [bc.handler :refer [handler]]
            [config.core :refer [env]]
            [ring.adapter.jetty :refer [run-jetty]])
  (:gen-class))

 (defn -main [& args]
   (let [port (Integer/parseInt (or (env :port) "3000"))]
     (run-jetty handler {:port port :join? false})))

project.clj:

(defproject bc "0.1.0-SNAPSHOT"
  :dependencies [[org.clojure/clojure "1.10.1"]
                 [org.clojure/clojurescript "1.10.597"
                  :exclusions [com.google.javascript/closure-compiler-unshaded
                               org.clojure/google-closure-library
                               org.clojure/google-closure-library-third-party]]
                 [thheller/shadow-cljs "2.8.83"]
                 [reagent "0.8.1"]
                 [re-frame "0.10.9"]
                 [compojure "1.6.1"]
                 [yogthos/config "1.1.7"]
                 [ring "1.7.1"]]

  :plugins [
            [lein-shell "0.5.0"]]

  :min-lein-version "2.5.3"

  :source-paths ["src/clj" "src/cljs"]

  :clean-targets ^{:protect false} ["resources/public/js/compiled" "target"]


  :shell {:commands {"open" {:windows ["cmd" "/c" "start"]
                             :macosx  "open"
                             :linux   "xdg-open"}}}

  :aliases {"dev"          ["with-profile" "dev" "do"
                            ["clean"]
                            ["run" "-m" "shadow.cljs.devtools.cli" "watch" "app"]]
            "prod"         ["with-profile" "prod" "do"
                            ["clean"]
                            ["run" "-m" "shadow.cljs.devtools.cli" "release" "app"]]
            "build-report" ["with-profile" "prod" "do"
                            ["clean"]
                            ["run" "-m" "shadow.cljs.devtools.cli" "run" "shadow.cljs.build-report" "app" "target/build-report.html"]
                            ["shell" "open" "target/build-report.html"]]
            "karma"        ["with-profile" "prod" "do"
                            ["clean"]
                            ["run" "-m" "shadow.cljs.devtools.cli" "compile" "karma-test"]
                            ["shell" "karma" "start" "--single-run" "--reporters" "junit,dots"]]}

  :profiles
  {:dev
   {:dependencies [[binaryage/devtools "0.9.11"]]}

   :prod { }

   :uberjar {:source-paths ["env/prod/clj"]
             :omit-source  true
             :main         bc.server
             :aot          [bc.server]
             :uberjar-name "bc.jar"
             :prep-tasks   ["compile" ["prod"]]}
   })

它确实在我 lein uberjar 时生成了一个 jar 文件,但是当我尝试 运行 它时它出错了,告诉我它不包含主文件 class.

我做错了什么?

你需要告诉 leiningen 哪个命名空间有你的主要功能。在 project.clj:

  :main my.service.runner

来自: https://github.com/technomancy/leiningen/blob/master/sample.project.clj#L222

您的 uberjar 配置文件在 :prep-tasks 调用 ["compile" ["prod"]]。您的 "prod" 别名调用 "clean""target" 列在 :clean-targets.

本质上,您的 uberjar 会删除已编译的 Clojure 代码。