Is there a way to set system properties in leinege

2020-03-01 10:56发布

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?

3条回答
不美不萌又怎样
2楼-- · 2020-03-01 11:04

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.

查看更多
相关推荐>>
3楼-- · 2020-03-01 11:22

project.clj:

(defproject ...
    :injections [(.. System (setProperty "custom_key" "24623472372576878923"))])
查看更多
放我归山
4楼-- · 2020-03-01 11:26

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.

查看更多
登录 后发表回答