Help to understand project directory structure

I took a look at leiningen tutorial, and it was not clear to me where I need to put my source files, and how to load them. Can I just put any .clj file in the src directory and it’ll get loaded magically, or do I need to register the file names somewhere so the system will be able to distinguish them from junk files and will know which order to load them in?

In Common Lisp I’m accustomed to registering all my project files in an ASDF file so the system knows which order to compile and load the files and which sub-systems depend on which other sub-systems in the same project.

Another beginner here, so hopefully I won’t mislead (or will be usefully corrected if I do!).

Leinigen is responsible for constructing the classpath to hand to the compiler. Locations are configurable in project.clj, but I think includes src by default. So any clj files there will be compiled. lein classpath shows you the classpath it will construct, so you can always check for a given project.

Then there’s runtime loading, which depends on a combination of your entry point (which form you’re evaluating in a repl, or the main function when running from the commandline), and the libs loaded into that entry point’s namespace (typically ns function call at the top of the file).

So you don’t need to register any files (just put them in the correct dir), and loading order depends on what each clj file in the project itself loads.

Ahh, so do I have to have some sort of require in every file that depends on some other file to allow the loader to determine the correct order?

Yes it’s the ns function (with a :require): https://clojuredocs.org/clojure.core/ns

External libraries also need so be specified in project.clj (so leinigen can get them from maven, cache them in your .m2 dir, and make them available on the classpath). But that’s for compilartion - I don’t think it has any impact on runtime load order (beginner caveat again)

Clojure’s dynamic nature kind of requires each namespace to be able to independently pull in its deps.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.