All Projects → mefellows → Sbt Dotenv

mefellows / Sbt Dotenv

Licence: mit
Simple Dotenv implementation for Scala SBT Builds. Configures environment for local development.

Programming Languages

scala
5932 projects

sbt-dotenv

Build Status Current Version License

sbt plugin to load environment variables from .env into the JVM System Environment for local development.

Storing configuration in the environment is one of the tenets of a twelve-factor app. Anything that is likely to change between deployment environments–such as resource handles for databases or credentials for external services–should be extracted from the code into environment variables.

But it is not always practical to set environment variables on development machines or continuous integration servers where multiple projects are run. sbt-dotenv loads variables from a .env file into ENV when the environment is bootstrapped.

sbt-dotenv is intended to be used in development.

Installation

Add the following to your sbt project/plugins.sbt file:

addSbtPlugin("au.com.onegeek" % "sbt-dotenv" % "2.1.204")

That's it - as soon as you start using sbt the environment is prepared.

Usage

Create a .env file in the root of your project with some environment specific settings. For example, you might want to set a Mongo DB port to 17017 if it's installed by Homebrew.

vi .env

# this is an example .env file, comments like this will be ignored
URL_HOST=my-service.example.org # trailing comments are ignored too!

# you can export variables like a regular shell script
export URL_PORT=1234

# variables can be quoted
URL_PATH="/my-content#body"

# variable expansion is supported
URL=http://$SERVICE_HOST:${SERVICE_PORT}${URL_PATH}

# these will work with sbt-dotenv, but won't work with `source .env`
MY.VARIABLE=1
MY-OTHER-VARIABLE=2

# multiline variables work too
MY_CERT="-----BEGIN CERTIFICATE-----
123456789qwertyuiopasdfghjklzxcvbnm
-----END CERTIFICATE-----
"

# heredocs aren't supported!

Variable expansion of the form $FOO and ${FOO} is supported based on the values in .env or the system environment.

Change file name

It is possible to change the file name .env

envFileName in ThisBuild := "dotenv"

Use file to define environment for tests

It is possible to use same of alternative file to provide an environment for tests:

Test / envFileName := "test.env" // optional

envVars in Test := (envFromFile in Test).value

and integration tests:

IntegrationTest / envFileName := "test.env" // optional

envVars in IntegrationTest := (envFromFile in IntegrationTest).value

"Illegal reflective access" warnings

On java versions 9 and up this plugin will give "illegal reflective access"-warnings. These can be avoided by starting sbt with these extra java options:

--illegal-access=deny --add-opens java.base/java.util=ALL-UNNAMED --add-opens java.base/java.lang=ALL-UNNAMED

Should I commit my .env file?

It is recommended that you store development-only settings in your .env file, and commit it to your repository. Make sure that all your credentials for your development environment are different from your other deployments. This makes it easy for other developers to get started on your project, without compromising your credentials for other environments.

sbt version

Please note that this plugin takes advantage of sbt Auto Plugins and therefore only works in sbt v0.13.5+

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature') and, optionally, squash history (git rebase -i <previous commit hash>)
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].