Clojure - splitting files relative to a project

I have a project built using leningen in which I have saved clj files in the src / some_project_name directory (along with the autogenerated core.clj file).

Also with these clj files, the text files that I want slurp

from the clj files they live side by side are saved. I understand that reading files is relative to the working directory and that the working directory will be where you ran the REPL. I started the REPL from inside src / some_project_name where all files are located, not root. (System/getProperty "user.dir")

confirms that this is the active directory.

However, I also read that it slurp

will search for files relative to your root directory, which seems to be happening despite running the REPL from src / some_project_name. I need to list the paths of text files relative to the root directory so that they can be found eg. "src / some_project_name / foo.txt", not just "foo.txt".

Before setting up the project, the files were available relative to where the REPL was running (as you would expect). Now, after setting up the project, they only seem to be accessible relative to the root, no matter where the REPL runs.

I have no problem with this, but I don't get it. Is there some kind of tweak made by leningen that intercepts the REPL evaluation and tells it to search from root rather than where the active directory is?

+3


source to share


1 answer


I believe the Leiningen REPLs use the project root as their working directory. (If not trampoline, they are using a subprocess with an expected working directory - try jps -l

after lein repl

, you should see two processes clojure.main

.)

On a separate note, you can use clojure.java.io/resource

to load files using paths relative to the classpath. Such files are usually placed in a folder resources

- from Leiningen, this is usually the sibling on src

.



For example, if you put foo.txt

in resources/some_project_name

, you should do the following:

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

(slurp (io/resource "some_project_name/foo.txt"))

      

+5


source







All Articles