Search code examples
javajarclojuredevelopment-environment

Where to place the files to be processed if I want to develop a standalone jar with clojure?


I want to write a standalone clojure program that will process all html files under the current working directory.

(println file-seq (io/file "."))

If I use the above code, it works when packed as a jar. But during development, where do I place the html files? Inside the src/../core.clj folder? I am not checking if the files are valid etc. So if I place under src, it will try to process clj files as well. Is there a better way without much change to the logic?


Solution

  • It depends on the directory from which you started Java process running your Clojure code.

    In case of running REPL or code or tests using your build tool (e.g. lein or boot) it is the top directory of your project (e.g. the one with project.clj or build.boot).

    You can also check it by logging the absolute path of "." file:

    (println (.getAbsolutePath (io/file "."))) 
    

    As to the testing of the code operating on files it is a good practice to parametrise your function with the base directory argument and call that function either with the current directory (.) when called as application (e.g. through the -main function) or by passing a stub directory prepared for testing. You could configure multiple test directories for example under test-resources in your project for different test scenarios (like empty directory, nested directories etc.) and call your function with a specific scenario directory as argument.