Welcome! Please see the About page for a little more info on how this works.

0 votes
in IO by
edited by

Probably a very noob question, sorry about that. I have a monorepo structured like this:

api/ 
 |-deps.edn
 |-src/
   |-api/
     |-core.clj
     |-resource_1.csv
library/
 |-deps.edn
 |-src/
   |-library/
     |-core.clj
     |-resource_2.csv

deps.edn of the api uses library as local dependency. Both of them use static information from respective csv files.

My question is how do I correctly include the static files when compiling the api uberjar, and how do I correctly use the static files within the code?

The api does not have any endpoints where it's serving the static files. They're used inside the code for caclulations.

1 Answer

+1 vote
by
selected by
 
Best answer

Are you going to load the csv files as resources from the classpath or files from the filesystem?

If resources, you shouldn't need to do anything else - by including them in the src directory they are on the classpath. You can then access them via (clojure.java.io/resource "api/resource_1.csv") (for example, there are other ways in the JDK too).

If the filesystem, you'll need to arrange some way to make them available in the final deployment (this isn't really a deps.edn or even a Clojure question at that point).

by
I want to use them as resources. Read the content and store it in a constant for usage in calculations. At the moment I have a helper function that opens them:

(defn load-csv [filename]
  (with-open [reader (io/reader filename)]
    (doall
     (csv/read-csv reader))))

I guess I need to change it to utilize io/resource instead of reader?
by
You still need a reader, but I think it works with URLs returned from resource so:

(defn load-csv [filename]
  (with-open [reader (io/reader (io/resource filename))]
    (doall
     (csv/read-csv reader))))
by
Yes, this works, thank you so much Alex! I'm actually waiting for your book to be delivered any day now, so I'm very touched that you're helping a noob like me!
...