Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reference environmental variable or home dir in project.clj

Is there a way to programmatically insert the name of my home directory into file paths in Leiningen's project.clj?

I run a Leiningen project on different machines, where I have different home directories. The project uses jar files that are not managed by Maven; I download 'em and put them in a directory that's relative to my home directory, or copy them into the Leiningen project. The second option works but is undesirable.

An easy way to use the first option--keeping the jar files somewhere else--is to add a soft link to the "somewhere else" directory in my Leiningen directory. That works, but the link has to be different on each machine, so I can't include the link file in the git repository, and I'd rather include everything in the git repo.

Isn't there a way to use environmental variables, or otherwise refer to my home directory, in my project.clj file? I've gone through the sample project file and have not found a solution, so far.

I thought I could just construct path strings at run time--what's in project.clj is just Clojure code, after all. Since hard-coding my home directory into project.clj works without any trouble:

  :resource-paths [/Users/myhomedir/dist/mason/jar/mason.19.jar")]

I figured I could do this:

:resource-paths [(str (System/getenv "HOME") "/dist/mason/jar/mason.19.jar")]

However, Leiningen doesn't like this at all:

java.lang.IllegalArgumentException: No implementation of method: :as-file of protocol: #'clojure.java.io/Coercions found for class: clojure.lang.PersistentList

Changing [...] to (vector ...) gives the same error but with 'clojure.lang.Symbol` at the end.

like image 794
Mars Avatar asked Jun 22 '15 16:06

Mars


People also ask

How do I find environment variables in Windows 10 CMD?

On WindowsSelect Start > All Programs > Accessories > Command Prompt. In the command window that opens, enter set. A list of all the environment variables that are set is displayed in the command window.

Where is environment variables located?

On the Windows taskbar, right-click the Windows icon and select System. In the Settings window, under Related Settings, click Advanced system settings. On the Advanced tab, click Environment Variables.

How do I print an environment variable in Linux?

To display the values of environment variables, use the printenv command. If you specify the Name parameter, the system only prints the value associated with the variable you requested.


1 Answers

In the name of repeatability you'd better have the jar installed in the local maven repository and have it added to the project.clj :dependencies so it's fetched from there. But you said those jars won't be managed by maven, so here we go:

defproject is a macro and it allows to use unquoting to do arbitrary evaluation. It does it by calling the internal fn unquote-project. So you can do the following:

:resource-paths [~(str (System/getenv "HOME") "/dist/mason/jar/mason.19.jar")]
like image 100
nberger Avatar answered Sep 21 '22 12:09

nberger