无法使用 Leiningen 构建 jar

Can't build a jar using Leiningen

我正在尝试使用 Intellij 的 Cursive 中的 Leiningen 插件从我的基本 Clojure 项目中制作一个独立的 jar。

为了创建项目,我刚刚创建了 project.clj 文件,将其打开,Cursive 提出将其作为项目导入。

project.clj:

(defproject WaterTimer "1"
  :description "A timer that reminds you to drink water"
  :main tone-producer/main)

音调-producer.clj:

(ns tone-producer
  (:require [general-helpers :as g])

  (:import [javax.sound.midi MidiSystem
                             Synthesizer
                             MidiChannel])
  (:gen-class))

(defn main [& args]
  (println "Test!"))

当我 运行 执行 "uberjar" 任务时,我得到以下输出:

Warning: specified :main without including it in :aot. Implicit AOT of :main will be removed in Leiningen 3.0.0. If you only need AOT for your uberjar, consider adding :aot :all into your :uberjar profile instead. 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. Created C:\Users\slomi\IdeaProjects\WaterTimer\target\WaterTimer-1.jar Created C:\Users\slomi\IdeaProjects\WaterTimer\target\WaterTimer-1-standalone.jar

我还尝试更改 main 函数以使用默认名称,并从 defproject:

中省略名称
(defproject WaterTimer "1"
  :description "A timer that reminds you to drink water"
  :main tone-producer)

(ns tone-producer
      (:require [general-helpers :as g])

      (:import [javax.sound.midi MidiSystem
                                 Synthesizer
                                 MidiChannel])
      (:gen-class))

    (defn -main [& args]
      (println "Test!"))

但现在我得到错误:

Error: Could not find or load main class clojure.main Compilation failed: Subprocess failed

结构是:

如有任何指导,我们将不胜感激。

为了创建 uberjars,项目文件应该有 :aot 关键字启用提前编译。

这是我的 project.clj 文件的输出。

(defproject jdbc "0.1.0-SNAPSHOT"
  :description "JDBC Project"
  :url "http://example.com/FIXME"
  :license {:name "Eclipse Public License"
            :url "http://www.eclipse.org/legal/epl-v10.html"}
  :dependencies [[org.clojure/clojure "1.8.0"]
                 [org.clojure/java.jdbc "0.6.1"]
                 [postgresql "9.3-1102.jdbc41"]
                 [com.mchange/c3p0 "0.9.5.2"]
                 [byte-streams "0.2.2"]]
  :main jdbc.core
  :aot [jdbc.core])

注意 :main 和 :aot 条目。它还需要是 -main ,正如 birdspider 已经指出的那样。

经过一番摆弄

  • 我放弃了 (:require [general-helpers :as g]) 因为没有必要演示这个问题
  • 错误:无法找到或加载 main class clojure.main 编译失败
    • 您没有包含 clojure 依赖项 [1]
  • :gen-class 需要 AOT - 正如 Sanchayan 指出的那样
    • 见[2]

project.clj

(defproject WaterTimer "0.0.1"
  :description "A timer that reminds you to drink water"
  :dependencies [[org.clojure/clojure "1.8.0"]] ;; <- [1]
  :main tone-producer    
  :aot [tone-producer])  ;; <- [2]

src/tone_producer.clj - 在文件名中使用“_”而不是“-”

(ns tone-producer
  (:import [javax.sound.midi MidiSystem
                             Synthesizer
                             MidiChannel])
  (:gen-class))

(defn -main [& args]
  (println "Test!"))

结果:

$ lein uberjar
Compiling tone-producer
Compiling tone-producer
Created .../watertimer/target/WaterTimer-0.0.1.jar
Created .../watertimer/target/WaterTimer-0.0.1-standalone.jar
$ java -jar target/WaterTimer-0.0.1-standalone.jar 
Test!

一般来说,我建议通过命令行使用 lein new <name> 初始化一个项目,然后将其导入到 Cursive/Other IDE 中。