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?
project.clj:
(defproject ...
:injections [(.. System (setProperty "custom_key" "24623472372576878923"))])
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.
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.
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