Get leiningen project root directory
Asked Answered
H

2

7

How do I get leiningen project's root directory at runtime?

My problem is that I want to place my sqlite file at the root directory, and access it via source code at different source file.

I don't like to use relative path, so how should I do?

Hypercritical answered 14/10, 2014 at 10:2 Comment(2)
When you say "sqlite file", is that a data file or file containing source code? If it's a data file, my experience is that most file-writing and file-reading functions will use the Leiningen project root dir by default. If that's not working, then I would look at the documentation of the functions you're using to access the file. (If you're calling Java functions from Clojure, then where do the Java functions expect to find files?) If you're trying to keep Clojure source code file in the Leiningen root, one option would be to use the load-file, although that's not considered a good idea.Edmondo
I mean a data file, and the function I use doesn't work on leiningen project root dir.If it work, that's another question, how can a function know where is the root dir?Hypercritical
E
8

Here are two ways to get the full path of the current working directory, which will be the project's home directory when you start Clojure using lein repl or lein run:

(System/getProperty "user.dir")

and

(.getCanonicalPath (clojure.java.io/file "."))

In the second method, you can insert any path string instead of ".", and it will be interpreted relative to Clojure's current working directory if the string doesn't contain an initial backslash. (I'm not sure whether the behavior is exactly the same on a Windows system.)

(The first method comes from this question; the answers there might be useful for your situation.)

Edmondo answered 14/10, 2014 at 15:55 Comment(0)
I
0

Can you put the sqlite file in the resources folder and then you could use clojure.java.io/resource to retrieve it at run time?

Inherent answered 14/10, 2014 at 10:34 Comment(1)
Sounds like a good idea, and it solve my problem to a certain extent.However I even want to know whether exists a way to get the absolute root directory path.Hypercritical

© 2022 - 2024 — McMap. All rights reserved.