4
votes

In my Leiningen project.clj I am using environ to set environment variables in different profiles. I want to build a demo uberjar with lein-ring, which uses different values for certain environment variables to the production version. For some reason I cannot seem to pick them up, however.

I have a demo profile defined something like this in my project.clj:

:demo {:dependencies [[javax.servlet/servlet-api "2.5"]]
       :plugins [[lein-environ "0.4.0"]]
       :env {:foo "FOO" :bar "BAR"}}

Then I execute

lein with-profile demo ring uberjar

But accessing foo or bar with environ.core/env inside the resulting standalone jar returns nil in both cases. On the other hand if I run

lein with-profile demo ring server-headless

I can access the values of both.

Why is this happening? It's really not what I would have expected. It looks like the uberjar is always being created with the production profile. Or is something else going?

The lein-ring page does state the following:

Lein-Ring pays attention to several environment variables, including: PORT - the port the web server uses for HTTP SSLPORT - the port the web server uses for HTTPS

These will override any options specified in the project.clj file, but won't override any options specified at the command line.

It's not clear that that means that any other environment variables specified in project.clj will be ignored, however.

Running leiningen 2.3.4 on Windows.

1

1 Answers

3
votes

It looks like that lein-environ plugin isn't actually physically setting environment variables (indeed you can't set environment variables on the current process from within Java - see here for details). Even supposing that it could, ask yourself - how would you ensure that environment variables that were present when building the uberjar are present when it is run?

Rather, lein-environ and the accompanying environ.core/env function provide you a way of faking out environment variables for testing and development purposes, without having to manually set environment variables before launching Leiningen. It does this by writing variables provided in the project map to a .lein-env file from the plugin code, and merging these values with those from the actual operating system environment when calling env from your project code.

So, when you go to run the uberjar, you'll actually need to have these variables present in your Windows environment (or in a .lein-env file in your current directory, which I wouldn't recommend). If your goal is to provide a standalone demo using pre-set environment variables to control certain application behavior, I'd create a .cmd script to distribute with your application that will set the appropriate environment variables and then launch the uberjar.