Resources in Clojure applications

2019-01-16 06:04发布

问题:

I am using Leiningen in my Clojure project (a GUI application) and created a "resources" directory under the project root to hold images that my app uses.

When I am running my app locally during testing, I fetch the images using the relative path "resources/logo.png", and this works fine. But when I build an uberjar using Leiningen, Leiningen puts the files from the resources folder in the JAR's root folder, so my references to resource files don't work anymore.

What is the correct way to access resources like this using Leiningen?

回答1:

The previous answerer (skuro) pointed out that I need to get the file from the classpath. After a little more digging, this appears to be the solution that works for my case:

(.getFile (clojure.java.io/resource "foo.png"))


回答2:

Just a syntax sugar for the answer of Kevin Albrecht:

(require '[clojure.java.io :as io])

(-> "foo.png" io/resource io/file) 


回答3:

Leiningen borrows the convention for resources from maven, with slightly different folder layouts. The rule states that the resources folder must be used as a compile time classpath root, meaning that leiningen is right in putting all the files inside resources folder in the root location inside the jar.

The problem is that physical location != classpath location, so that while the former changes when you package you application, the latter stays the same (classpath:/)

You'd better rely on classpath location to find your file system resources, or take them out of the jar and place them into a predictable folder, either a static or a configurable one.



回答4:

This is not directly answering the OP's question, but skuro mentioned something in his last paragraph which can be quite useful, i.e. to make resources available on the classpath during development, but not include them in the release jar/uberjar.

The reason of that is then you could place the resources separately outside of the jar – for example placing configuration files under /etc – and then link to them at runtime by listing such resource folders on the classpath.

Definitions in project.clj

  • remove the top-level :resource-paths from your project.clj;
  • add the following lines in the project.clj at the top level:
:profiles {:dev {:resource-paths ["src/main/resources"]}}

This way the resources will be available on the classpath during development (compile, test, repl), but will not be included in the release jars.

As an altenative, you might want to bundle some of the resources with the release jar, like application icons and graphics, but not others, like configuration files. In that case you could split the resources folder into subdirectories and declare the ones you want to get included at the top level, while the rest under the dev profile:

:resource-paths ["src/main/resources/icons"
                 "src/main/resources/images"]
:profiles {:dev {:resource-paths ["src/main/resources/config"]}}

Referencing resources in code

Using the above method you could reference resources both during development and production uniformly by looking them on the classpath (as shown by Kevin and Valerii), and not directly on the filesystem:

(require '[clojure.java.io :as jio])
(jio/file (jio/resource "relative/path/to/resource.xml"))

Runtime classpath

During development Leiningen would include both the top level and the dev profile resources on the classpath. For the released runtime you will have to configure the classpath for the JRE yourself:

java -cp "/etc/myapp:/usr/local/lib/myapp.jar" myapp.core $*

Also, alternatively you can in fact leave all the resources included in the jar. That way the included resources would be used as defaults. If you set up the classpath so that the .jar file comes after the configuration folder (/etc/myapp in the example), then the resource files with the same relative resource path under the configuration folder will take precedence over the ones included in the jar.



回答5:

I had the exact same problem and the solution is not using io/file at all. So for example this is working code from my app:

(defn load-md [md]
  (->
   md
   io/resource ;;clojure.java.io
   slurp
   mp
   to-hiccup
   html))


回答6:

In our case, using .getPath or .getFile was unhelpful. Simply using input-stream instead resolved it.

(defonce classifier
  (-> "machine_learning/classifier.model"
      resource
      input-stream
      helper/read))


回答7:

Nice way of doing it. Note that io/file returns a java File object, which can then be passed to slurp, etc:

(ns rescue.core
  (:require [clojure.java.io :as io] ))

(def data-file (io/file
                 (io/resource 
                   "hello.txt" )))
(defn -main []
  (println (slurp data-file)) )

If you then execute the following in your lein project dir:

> echo "Hello Resources!" > resources/hello.txt
> lein run
Hello Resources!

and presto! You are reading resource files via the classpath.