This Clojure demo project shows how to configure and use the weavejester/environ library.
To use this project, we assume you have already installed Java, Clojure, and Leiningen, installed.
Try the following commands, and make sure you get similar output:
> java --version
java 13 2019-09-17
Java(TM) SE Runtime Environment (build 13+33)
Java HotSpot(TM) 64-Bit Server VM (build 13+33, mixed mode, sharing)
> lein --version
Leiningen 2.9.1 on Java 13 Java HotSpot(TM) 64-Bit Server VM
We don’t want to check sensitive information like passords into SCM. Also, we will typically
have different credentials for different phases of a project
(e.g. database connections for dev, test, prod, etc). For this demo, we place DB connection
strings for dev and test into profiles.clj
. This file is normally never checked into SCM, but we
have included a dummy version in this demo project for illustrative purposes. It looks like this:
{:profiles/dev {:env {:database-url "jdbc:postgresql://localhost/dev"}}
:profiles/test {:env {:database-url "jdbc:postgresql://localhost/test"}}}
> lein clean ; lein test
lein test _bootstrap
-----------------------------------
Clojure 1.10.3 Java 15.0.2
-----------------------------------
lein test tst.demo.core
Ran 2 tests containing 1 assertions.
0 failures, 0 errors.
( lein do clean, test; ) 34.44s user 1.90s system 408% cpu 8.906 total
The file test/clj/tst/demo/core.clj
verifies that the "test" DB URL is found.
> lein clean ; lein run
The file src/clj/tst/demo/core.clj
will print out the "dev" DB URL.
Running in demo.core/-main
(environ/env :database-url) => "jdbc:postgresql://localhost/dev"
( lein do clean, run; ) 31.89s user 1.80s system 392% cpu 8.576 total
Run the code using an environment variable to override profiles.clj
:
> DATABASE_URL=jdbc:postgresql://localhost/prod lein run
Warning: environ value jdbc:postgresql://localhost/dev for key :database-url has been overwritten with jdbc:postgresql://localhost/prod
Running in demo.core/-main
(environ/env :database-url) => "jdbc:postgresql://localhost/prod"
The file src/clj/tst/demo/core.clj
will print out the "prod" DB URL from the env var
DATABASE_URL
. It will also work if you run from a JAR file
via java -jar myproj.jar
instead of lein run
This project includes the lein-ancient
plugin, which will tell you if any of your dependency
libraries are out of date. I have an alias:
alias laca="lein ancient check :all"
which will give you a list of version updates you should make, or just
all artifacts are up-to-date.
if you are already up-to-date on everything.
Copyright © 2021 Alan Thompson
Distributed under the Eclipse Public License, the same as Clojure.