【发布时间】:2021-12-20 22:33:27
【问题描述】:
我有一个项目,我希望代码的某些部分能够在本地环境中运行,而其他部分依赖于仅在远程环境中运行的库。例如
src/app/core.clj <- can run anywhere
src/app/sandbox/remote_only.clj <- depnds on libs that only function in remote environ
src/app/core.clj 在哪里
(ns app.core
(:gen-class))
(defn -main [] (println "Hello, World!"))
而src/app/sandbox/remote_only.clj 是
(ns app.sandbox.remote-only
(:require
[uncomplicate.commons.core :refer [with-release]]
[uncomplicate.neanderthal
[native :refer [dv dge]]
[core :refer [mv mv!]]]))
我希望能够uberjar 并在我的本地计算机上运行位于core.clj 中的代码,而不需要拉入remote_only.clj,这将导致程序在本地失败。根据文档,Leiningen 应该能够使用配置文件和 uberjar 排除来完成此操作,例如:
(defproject app "0.1.0-SNAPSHOT"
:dependencies [[org.clojure/clojure "1.10.1"]]
:profiles {:uberjar {:main app.core
:init-ns app.core
:aot :all}
:remote {:init-ns app.sandbox.remote_only
:dependencies [[uncomplicate/neanderthal "0.43.1"]]}} ; these deps will fail locally
:uberjar-exclusions [#".*sandbox.*"]
:repl-options {:init-ns app.core})
在这里编译 uberjar 会导致:
❯ lein uberjar
Compiling app.core
Compiling app.sandbox.remote-only
Syntax error macroexpanding at (remote_only.clj:1:1).
Execution error (FileNotFoundException) at app.sandbox.remote-only/loading (remote_only.clj:1).
Could not locate uncomplicate/commons/core__init.class, uncomplicate/commons/core.clj or uncomplicate/commons/core.cljc on classpath.
因此,它明确地试图编译被明确排除的类。
我认为可以通过从:aot 编译中删除:all 标记来避免此类问题。例如:
(defproject app "0.1.0-SNAPSHOT"
:dependencies [[org.clojure/clojure "1.10.1"]]
:profiles {:uberjar {:main app.core
:init-ns app.core
:aot [app.core]}
:remote {:init-ns app.sandbox.remote_only
:dependencies [[uncomplicate/neanderthal "0.43.1"]]}} ; these deps will fail locally
:uberjar-exclusions [#".*sandbox.*"]
:repl-options {:init-ns app.core})
导致其他问题:
❯ lein uberjar
Compiling app.core
Created /Users/warrenronsiek/Projects/app/target/app-0.1.0-SNAPSHOT.jar
Created /Users/warrenronsiek/Projects/app/target/app-0.1.0-SNAPSHOT-standalone.jar
~/Projects/app 5s 15:47:37
❯ java -jar ./target/app-0.1.0-SNAPSHOT.jar
Exception in thread "main" java.lang.NoClassDefFoundError: clojure/lang/Var
我尝试过各种:exclusions、:jar-exclusions 和正则表达式。这个问题的变体已被多次询问(1,2)没有任何效果。
如何让 Leiningen 编译 uberjars 并忽略指定的文件?
【问题讨论】:
标签: clojure compilation jvm leiningen