Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to set system properties in leinegen?

Starting a lein task (actually test, which runs by default with the :test profile). What I'd like to do is start with the equivalent of

java -Dproperty1=value -Dproperty2=value2 task

There are some references on the web that suggest that this should work fine like this:

project.clj:

...
:profiles {:test {:jvm-opts ["-Dproperty1=value" "-Dproperty2-value"]}}

This is ignored in my test runner. The profile is correct, If I insert some actual jvm args (e.g something like "-XX:+PrintGC") it works fine. But doesn't seem to pick up the system properties. Is there a correct way to do this?

like image 275
Steve B. Avatar asked Sep 01 '15 04:09

Steve B.


3 Answers

project.clj:

(defproject ...
    :injections [(.. System (setProperty "custom_key" "24623472372576878923"))])
like image 110
akond Avatar answered Oct 02 '22 21:10

akond


Found an answer, for anyone else who's struggling with this:

Since I needed to inject environment properties before I started the process, I did the following:

add the shell plugin to your project.clj:

:plugins [[ lein-shell "0.4.1"]]

and then add a prep task to your profile. But there's a wrinkle - you'd think you could do this:

:profiles {:test {:prep-tasks [["shell" "export" "foo=bar"]]}}

But this doesn't work, as shell doesn't see the export command - you get "no such file", since it's part of bash and there's no executable file called "export". So I created a script called "setenv.sh" and ran that from shell:

:profiles {:test {:prep-tasks [["shell" "./test/setenv.sh"]]}}

Edit: actually this does not work, the variables are not carried over to the subprocess. Left it here because it might be useful to someone as is.

Edit: actually had to create a shell script that calls export and then runs lein. Definitely not the most elegant solution.

like image 40
Steve B. Avatar answered Oct 03 '22 21:10

Steve B.


We use environ for that very purpose.

Once you install the plugin, all you need to do is create a file .lein-env in your project root containing a map of environment variables to be set, such as:

{
  :s3-access-key     "some key"
  :s3-secret-key     "some secret"
}

Then, in your code, you can use:

(require '[environ.core :refer [env]])
(env :s3-access-key) ;; "some key"

This lets me point to, say, stub server in test but the real thing in production as environ will use the system environment variables if no .lein-env is provided.

I hope this helps.

like image 34
leonardoborges Avatar answered Oct 01 '22 21:10

leonardoborges