Clojure Uberjar没有加载资源文件 [英] Clojure Uberjar not loading resource file
问题描述
我使用lein uberjar来创建一个独立的jar应用程序。
I use lein uberjar to create a standalone jar of an application.
当执行
java -jar dataloader-0.1.0-SNAPSHOT-standalone.jar,
它崩溃:
Caused by: java.lang.IllegalArgumentException: Not a file:
jar:file:dataloader-0.1.0-SNAPSHOT-standalone.jar!/configuration.json
我通过以下方式加载文件:
I load the file via:
(ns dataloader.configuration
(:gen-class)
(:require [cheshire.core :refer :all]
[clojure.java.io :as io]))
(def data-file
(io/file
(io/resource "configuration.json")))
project.clj
project.clj
(defproject dataloader "0.1.0-SNAPSHOT"
:description "Used for loading stage data into local vagrantbox"
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:resource-paths ["resources"]
:dependencies [[org.clojure/clojure "1.6.0"]
[clojurewerkz/elastisch "2.1.0"]
[org.clojure/java.jdbc "0.3.7"]
[mysql/mysql-connector-java "5.1.32"]
[clj-http "2.0.0"]
[org.clojure/data.json "0.2.6"]
[org.clojure/data.codec "0.1.0"]
[cheshire "5.5.0"]]
:main ^:skip-aot dataloader.core
:target-path "target/%s"
:profiles {:uberjar {:aot :all}})
resources / configuration.json放在jar的根文件夹中
resources/configuration.json is put into the root folder of the jar
推荐答案
clojure.java.io/resource
返回一个URL,而不是一个文件。这就是为什么你可以调用 slurp
。错误消息告诉你它不是一个文件,不幸的是它不告诉你它是一个URL。
clojure.java.io/resource
returns a URL, not a file. That's why you can call slurp
on it. The error message is telling you that it's not a file, unfortunately it's not telling you that it's a URL.
当然,你可以打开与java.net的url。 URL api,虽然在这种情况下会是过度杀伤。
Of course you could open the url with the java.net.URL api although that would be overkill in this case.
这篇关于Clojure Uberjar没有加载资源文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!