Why does leiningen make a directory hierarchy? Can I dispense with it?
Asked Answered
D

2

6

If I create a new clojure project with leiningen, it makes a directory tree like so:

.
|-- doc
|   `-- intro.md
|-- project.clj
|-- README.md
|-- src
|   `-- hello_friend
|       `-- core.clj
`-- test
    `-- hello_friend
        `-- core_test.clj

Often, all I want is a single clojure file, and I want to use leiningen to handle libraries and start a repl.

I've got two questions:

(1) Is is possible to get leiningen to work properly with this structure

.
|-- project.clj
`-- hello.clj

which I'd greatly prefer. (In fact even better would be to have the project.clj as part of the hello.clj file)

(2) Is there any good reason for the deep directory structure? Or is it just a habit from java-land?


Note, this works fine as far as I can see:

project.clj

(defproject generic "0.0.0"
  :dependencies 
  [[org.clojure/clojure "1.4.0"]
   [hiccup "1.0.2"]]
  :source-paths ["."]
  :main two

  :repl-options { :port 4001 :init (println "(-main) to run") }
)

one.clj

(ns one
  (:use hiccup.core))

(def doom (html [:h1 "doom"]))

two.clj

(ns two 
  (:require one))

(defn -main [] 
  (println one/doom))
Diantha answered 25/11, 2012 at 12:43 Comment(0)
B
4

(1) You can specify :source-paths ["."] in your project.clj. See lein sample project for all possible options

(2) You need the "deep" structure once you have 2 namespaces and one requires on the other. See here for an explanation of how namespaces are mapped to files.

Breton answered 25/11, 2012 at 13:7 Comment(3)
(1) is awesome, thankyou. but see above for (2). Looks like it works fine.Diantha
@JohnLawrenceAspden, Try your example with a deeper namespace. For example, change one to foo.one. Right now the namespace refers to the file name, deeper ones refer to directories as well.Diopside
You need a deeper namespace when you want to organize (multiple) namespaces by directory. Outside of that, one shallow namespace can require another namespace.Septet
S
0

The default src path (deep structure) separates source code from config. With big apps, you also organize multiple namespaces by directory for easy navigation.

Even this initial REST API'd get unwieldly if everything were in the root:

Cheffy

Septet answered 6/1, 2022 at 16:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.