英文:
lein uberjar - not setting main class properly?
问题
Here's the translated code portion you provided:
在一个新的 `lein new re-frame bc +handler` 应用程序上,如果我运行 `lein uberjar` 或 `lein jar`,似乎无法正确设置主类。在编译结束时,它告诉我:
“警告:指定的主类在 jar 文件中不存在。可能无法按预期执行。在包含主方法的命名空间中可能缺少 gen-class 指令,或者该命名空间尚未进行 AOT 编译。”
这是使用 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"]]}
  })
Please note that the translated code retains its original structure and formatting.
英文:
On a fresh lein new re-frame bc +handler app, if I lein uberjar or lein jar it doesnt seem to set the main class correctly. At the end of the compillation it tells me
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.
Here is the server.clj and project.clj that is created using the re-frame +handler template:
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"]]}
   })
It does generate a jar file when I lein uberjar but when I try to run it it errors out telling me that it does not include a main class.
What am I doing incorrectly?
答案1
得分: 4
你的 uberjar 配置在 :prep-tasks 中调用了 ["compile" ["prod"]]。你的 "prod" 别名调用了 "clean",而 "target" 被列在 :clean-targets 中。
总之,你的 uberjar 删除了你编译的 Clojure 代码。
英文:
Your uberjar profile calls ["compile" ["prod"]] in :prep-tasks. Your "prod" alias calls "clean" and "target" is listed in :clean-targets.
In essence, your uberjar deletes your compiled Clojure code.
答案2
得分: 0
You need to tell leiningen what namespace has your main function. In project.clj:
  :main my.service.runner
from:
https://github.com/technomancy/leiningen/blob/master/sample.project.clj#L222
英文:
You need to tell leiningen what namespace has your main function. In project.clj:
  :main my.service.runner
from:
https://github.com/technomancy/leiningen/blob/master/sample.project.clj#L222
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论