Skip to content

A simple utility to manage environment configs in Java-based projects by merging .properties files and environment variables overrides.

License

Notifications You must be signed in to change notification settings

mabreu0/env-config

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

67 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

env-config

A simple utility to manage environment configs in Java-based projects by merging *.properties files with environment variables overrides.

Build Github Publish Maven Publish Maven Central Maintainability License: MIT contributions welcome

All notable changes to this project are documented in CHANGELOG.md. The format is based on Keep a Changelog and adheres to Semantic Versioning.

Setup

Add the following dependency to use this EnvConfig:

Maven

<dependency>
    <groupId>com.github.sitture</groupId>
    <artifactId>env-config</artifactId>
    <version>${version}</version>
</dependency>

Github Packages

If you would like to use github package instead of maven central, add the following repository to pom.xml.

<repositories>
  <repository>
    <id>github</id>
    <name>GitHub Packages</name>
    <url>https://maven.pkg.github.com/sitture/env-config</url>
  </repository>
</repositories>

Gradle

compile 'com.github.sitture:env-config:${version}'

Usage

To start using this:

config directory

The default required directory for configuration files in config under project root. This can be overridden by CONFIG_DIR environment variable.

  • create a directory called config in project root.

config environments

The default environment is set to default and can be overridden by CONFIG_ENV environment variable.

  1. create a default environment subdirectory under config directory.
  2. create a default.properties file in the default directory. E.g. config/default/default.properties
# formatted as key=value
my.first.property=my_first_value
my.second.property=my_second_value

You can add multiple .properties files under environment directory. E.g. You may want to split the properties into:

.
├── config
│   └── default
│       ├── default.properties
│       └── db.properties

You can create as many environments as needed.

.
├── config
│   └── default
│       ├── default.properties
│       └── db.properties
│   └── integration
│       └── integration.properties

KeePass Database Entries

If you have secret passwords which cannot be stored as plain text within project repository, you can store them into a password-protected KeePass database file.

  1. create a keepass database file, add to your resources folder. i.e. src/main/resources or src/test/resources.

Configurations

  • CONFIG_KEEPASS_ENABLED - A flag to enable reading of the keePass file. Default is set to false.
  • CONFIG_KEEPASS_FILENAME - This is the name of the DB file. Default is the name of project directory.
  • CONFIG_KEEPASS_MASTERKEY - The key to access the DB file.

KeePass Groups

  • The top level group should have the same name as the DB filename. e.g. if DB file is env-config.kdbx then top level group should be env-config.
  • The sub-groups should match with the environment directory you have created above. For example, you should have default group for the default environment.
  • The entries within the default group will be shared across all environments similar to the environment directories behaviour.

Environment priority

The EnvConfig will go through properties set under your environment and then load properties from default environment ignoring the ones already set. You can keep the shared properties under your default environment without having to repeat them in every other environment.

Current environment

You can get the current environment by:

EnvConfig.getEnvironment();

Get property EnvConfig.get("...")

To get a property set either in the properties file, system property or environment variable:

EnvConfig.get("my.property");
EnvConfig.getInt("my.property");
EnvConfig.getBool("my.property");
EnvConfig.getList("my.property"); // will return a List<String> from a comma separated String.

Get required property

// when a property is required to continue
EnvConfig.getOrThrow("my.property");

If the property isn't set then a MissingVariableException is thrown.

Get property with defaultValue

// return a default value when a property isn't found
EnvConfig.get("my.property", "defaultValue");

Note: All the environment variable names are set to properties naming convention. E.g. MY_ENV_VAR can either be accessed by EnvConfig.get("my.env.var"); or EnvConfig.get("MY_ENV_VAR");

Property overrides

You can override any property set in the environment properties file by setting an system environment variable.

E.g. my.env.property can be overridden by MY_ENV_PROPERTY environment variable.

Add property EnvConfig.add("...")

You can add key/value pairs to the EnvConfig to be accessed somewhere else in the project.

EnvConfig.add("my.property", "my_value");

Set property EnvConfig.set("...")

You can set/update an existing property in EnvConfig:

EnvConfig.set("my.property", "my_value");

The .set(...) can be used for both existing and non-existing properties.

Clear property EnvConfig.clear("...")

You can clear an existing property in EnvConfig:

EnvConfig.clear("my.property")

Get all EnvConfig.getConfig()

You can get a full list of available properties with EnvConfig.getConfig() which is a combination of properties from config directory, system properties and all environment variables.

Issues & Contributions

Please open an issue here on GitHub if you have a problem, suggestion, or other comment.

Pull requests are welcome and encouraged! Any contributions should include new or updated unit tests as necessary to maintain thorough test coverage.

Read CONTRIBUTING.md for guidelines.

About

A simple utility to manage environment configs in Java-based projects by merging .properties files and environment variables overrides.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Shell 100.0%