Search code examples
clojureleiningen

How to compile clojure modules that are not under "src" in a lein project


I built my project using lein new app hello, so I have this structure

.
└── src
    └── hello
        └── core.clj
└── project.clj
└── test
└── ..

I want to add another helper module to my project such that I can use the code in both the tests and the src modules as I wanted it decoupled from the src directory.

So I added a helpers module

.
└── src
    └── hello
        └── core.clj
└── project.clj
└── test
└── helpers
    └──hello
         └── helpers.clj

How can I change my project.clj file to make lein run compile. lein run does not compile and throws the following error when I try to require the helpers namespace.

Exception in thread "main" java.lang.ClassNotFoundException: hello.helpers, compiling:(hello/core.clj:7:3)

My project.clj file looks like the default one :

(defproject hello "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url "http://example.com/FIXME"
  :license {:name "Eclipse Public License"
            :url "http://www.eclipse.org/legal/epl-v10.html"}
  :dependencies [[org.clojure/clojure "1.8.0"]]
  :main ^:skip-aot hello.core
  :target-path "target/%s"
  :profiles {:uberjar {:aot :all}})

However in the IDE(IntelliJ) it works fine in the REPL, without throwing the class not found exception.


Solution

  • Add

    :source-paths ["src" "helpers"]
    

    to your project.clj file.