A Gradle plugin that loads task runtime environment variables from env files.
- Apply this plugin to a project that has a target task.
// project_has_task/build.gradle.kts plugins { id("io.github.taisuke-fujimoto.env-loader") version "0.0.1" // as an example, the target task is `run` application } application { mainClass.set("...") }
- Create an env file in the project.
# project_has_task/.env SAMPLE_VAR=sample value
- Running the target task will load environment variables from the env file.
./gradlew project_has_task:run
// this configuration represents the default value
// in other words, if this is good enough, you don't need a configuration block
envLoader {
// load environment variables from a file at this path
// if multiple files have the same environment variable, the later file takes precedence
//
// `<taskName>` will be replaced with the task name
// i.e. for a `run` task it will be `.env.run`
envFiles(".env", ".env.<taskName>")
// the name of the task that loads the environment variables
taskNames("run", "bootRun", "azureFunctionsRun")
// a flag that determines the priority of system environment variables and env file environment variables
// if true, env file environment variable takes precedence
overrideSystemEnvironment.set(false)
}
- The target task must implement org.gradle.process.ProcessForkOptions
- It is difficult to control the priority between task's environment variables and env file environment variables, so avoid defining the same environment variables in them.