Import / use of a resource from an external cloyer

What I'm trying to do is package a large file (MIDI soundfont) in a stand-alone Maven repo / clojar, and then you can programmatically output it and use it from a separate project. This seemingly simple task turned out to be more difficult than I expected.

What would be ideal if you could directly access these resources or publish them as public vars or something like that. This is the first thing I tried - I did something like this:

(ns midi.soundfont.fluid-r3
  (:require [clojure.java.io :as io]))

(def sf2
  (io/file (io/resource "fluid-r3.sf2")))

However, the problem I am facing is that it io/resourcefinds resource files only in the current class path. As soon as I try to require this namespace from another project (or from REPL), I get:

java.lang.IllegalArgumentException: Not a file: jar:file:/Users/dave/.m2/repository/midi/soundfont/fluid-r3/midi.soundfont.fluid-r3/0.1.0/midi.soundfont.fluid-r3-0.1.0.jar!/fluid-r3.sf2

If access to the resource is not possible directly, I would be pleased with the solution, which involves copying the file to some path in the file system. I tried this too, but I encountered the same problem when trying to run the "copy file to file system" method from another project - the io/resourcefile still could not be found because it is not in the current class path.

I found similar questions asked earlier on SO, for example:

However, these solutions seem to apply only to copying the file, which is the resource in the current (running) project.

Can one of these two things be done?

  • Access resource file from external clojar
  • , io/resource
+1
2

dbasch, io/resource URL-, . URL- io/file REPL lein run, ? , URL- , URL- , .

github repo. -main :

(defn -main [& args]
  (let [r (io/resource "greet")]
    (println r)
    (println (slurp r))
    (with-open [rdr (io/reader r)]
      (println (clojure.string/join ", " (line-seq rdr))))
    (println (io/file r))))

lein run :

› lein run
#<URL file:/home/nicolas/projects/clojure/resources/resources/greet>
hello
world

hello, world
#<File /home/nicolas/projects/clojure/resources/resources/greet>

uberjar :

› java -jar target/resources-0.1.0-SNAPSHOT-standalone.jar 
#<URL jar:file:/home/nicolas/projects/clojure/resources/target/resources-0.1.0-SNAPSHOT-standalone.jar!/greet>
hello
world

hello, world
Exception in thread "main" java.lang.IllegalArgumentException: Not a file: jar:file:/home/nicolas/projects/clojure/resources/target/resources-0.1.0-SNAPSHOT-standalone.jar!/greet
        at clojure.java.io$fn__8588.invoke(io.clj:63)
        at clojure.java.io$fn__8572$G__8556__8577.invoke(io.clj:35)

#<URL file:/home/nico... #<URL jar:file:/home/nico..., , (io/file) , slurp io/reader.

+2

(io/resource "fluid-r3.sf2") - URL, . slurp , , java.net.URL api ( , ).

:

user> (type (clojure.java.io/resource "text.txt"))
java.net.URL
user> (slurp (clojure.java.io/resource "text.txt"))
"this is a text file\n"
+1

Source: https://habr.com/ru/post/1616637/


All Articles