Is there a way to set system properties in leinegen?
Asked Answered
L

3

11

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?

Lettering answered 1/9, 2015 at 4:31 Comment(0)
L
5

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.

Lettering answered 1/9, 2015 at 16:29 Comment(0)
T
2

project.clj:

(defproject ...
    :injections [(.. System (setProperty "custom_key" "24623472372576878923"))])
Transposal answered 24/9, 2019 at 2:4 Comment(0)
P
1

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.

Peep answered 1/9, 2015 at 4:40 Comment(2)
Thanks, I've seen this, not quite the thing I need - I'm running some tests that include, among other things, a guice container that reads from the runtime environment when it's instantiated, so shoehorning this into the source is possible but clumsy. It would be much nicer to be able to just configure in lein, but +1 for the tip. Thanks.Lettering
This technically does not set Java properties. It only lets you read configuration from environ, which may not be what you need (e.g if you use a library which consumes java properties).Integumentary

© 2022 - 2024 — McMap. All rights reserved.