Skip to content

Im-Fran/GradleDotEnv

Repository files navigation

Gradle Dot Env Plugin

Java Version GitHub Workflow Status (with event) GitHub GitHub release (latest by date)

A Gradle plugin to load environment variables from a .env file.

WARNING: This plugin is still in development, so it may not work as expected, make sure to report any issues you find.

Installation

// Kotlin
plugins {
    id("cl.franciscosolis.gradledotenv") version "1.0.0"
}
// Groovy
plugins {
    id 'cl.franciscosolis.gradledotenv' version '1.0.0'
}

Make sure to change the version by the current version:
GitHub release (latest by date)

You can also follow the gradle plugin page instructions.

Usage

The plugin adds a map called env to the project's extra property. This map contains all the environment variables loaded from the .env file and the system environment variables. The system environment variables have priority over the .env file variables.

// Kotlin
println(env["ENV_VAR"])
// Groovy
println env["ENV_VAR"]

Support for different environments

The plugin supports different environments by using different .env files by using the ENV system environment variable, if is not present it will try to use the ENV system property. If none of them are present, it will just use the default .env file.

So for example, if you have the following file structure:

├── .env
├── .env.dev
├── .env.prod
├── .env.test
├── build.gradle.kts
└── src
    └── main
        └── kotlin
            └── Main.kt

And you run the following command:

ENV=dev ./gradlew run

The plugin will load the variables from the .env.dev file.

License

This project is licensed under the GNU GPLv3 License - see the LICENSE file for details.