When lein sets up a project, a core.clj file is created along with other directories and files. I want to know if I can split core.clj's content off to another source file under
../myproj/src/myproj/
and if so, how to access that data from core.clj.
IIRC (I don't have a project handy to check), everything in your src/myproj/
directory is in the 'myproj
namespace. So your core.clj
file will be in the namespace 'myproj.core
. Other files will be in their own namespaces within the 'myproj
namespace (e.g. 'myproj.other-file
for other_file.clj
), and can be pulled into core.clj
by doing:
(use 'myproj.other-file)
or, in an ns
declaration:
(ns myproj.core
(:use [myproj.other-file]))
You can split up definitions over as many files as you like, though it's idiomatic to put a single namespace in a single file and vice versa.
See http://clojure.org/libs for how to name and load files and namespaces. One thing to keep in mind is that dashes in namespaces translate to underscores in file names.
Leiningen's project.clj is to define a project var that's nothing more than a map with keys denoting project parameters (it's an idiom in Clojure to use def[name] to create a var with the [name] name which usually is a map - the most basic yet very useful data structure).
See the defaults var in the source code of Leiningen 2 for the defaults.
With that said, before you call the defproject macro, you can do whatever you want in the project.clj - it's a Clojure script after all and your imagination (and familiarity with Clojure) is only what might constrain you. In fact, you may do whatever you want with the var after it's created. Think of project.clj as a Clojure application to manage your project.
As an example, before profiles were introduced in Leiningen 2, there was a "trick" to have a single var with common dependencies for :dependencies and :dev-dependencies attributes. Just to warn you again - it's no longer necessary in Leiningen 2 as it offers profile facility. Have a read of Testing your project against multiple versions of Clojure if you're curious how it was in the past.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With