Provides dotenv (.env
) and dotenv templates (.env.template
) as variables in a Project extension.
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.
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.
-
(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, ornull
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 includesnull
values for unset variables.
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.
You can also use a different name for the .env
file. See this example on how to do this.
This project supports subproject-only variables and extensions.
See this example for more details.
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.
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