Skip to content

uzzu/dotenv-gradle

Repository files navigation

Dotenv Gradle

main ktlint Gradle Plugin Portal

Provides dotenv (.env) and dotenv templates (.env.template) as variables in a Project extension.

How to use

Setup

Apply this plugin to the root project. Read the Gradle Plugin Portal to setup the plugin..

Note that this plugin is not registered to Maven Central.

You do not need to apply this plugin to subprojects; the values are applied automatically.

Create .env in the root directory of your gradle project

For example:

FOO=foo
BAR="bar"
BAZ='baz'

# You can comment out lines with a #
; You can comment out lines with a ;

Then, you will be able to use the environment variables in your gradle scripts:

println(env.FOO.isPresent)              // => true
println(env.FOO.value)                  // => foo
println(env.BAR.orNull())               // => bar
println(env.BAZ.orElse("default baz"))  // => baz

Don't commit the .env file. Ideally, add it to your .gitignore file.

API

  • (Boolean) env.isPresent(name: String): Indicates that an environment variable with specified name is present.

  • (String) env.fetch(name: String): Returns the environment variable of the given name.

  • (String) env.fetch(name: String, defaultValue: String): Returns an environment variable, or specified default value if environment variable was not set.

  • (String?) env.fetchOrNull(name: String): Returns an environment variable, or null if the environment variable was not set.

  • (Map<String, String) env.allVariables(): Returns all environment variables.

  • (Map<String, String?) env.allVariablesOrNull(): Returns all environment variables, and includes null values for unset variables.

Templates

If a .env.template file exists, this plugin populates environment variables names from the template, too. This means you can use the template to define the environment variables that are required for your project, and override them in the .env file. Values within the .env.template file are not populated when read, all values will be null.

See the change_template_file example for more details.

Changing the name of the .env file

You can also use a different name for the .env file. See this example on how to do this.

Hierarchical dotenv definitions

This project supports subproject-only variables and extensions.

See this example for more details.

Other Features/Functions

Note that all APIs of this env extension consider the .env file.

If the same variable name value is defined in both the .env file and system environment variables, the system environment variable takes precedence.

See more examples.

Restrictions

This plugin does not support specifying properties with the -P option of CLI arguments, and there are no plans to support it in the future. See #67

License

Apache License 2.0