A simple utility to manage environment configs in Java-based projects by merging *.properties
files with environment variables overrides.
All notable changes to this project are documented in CHANGELOG.md. The format is based on Keep a Changelog and adheres to Semantic Versioning.
Add the following dependency to use this EnvConfig:
<dependency>
<groupId>com.github.sitture</groupId>
<artifactId>env-config</artifactId>
<version>${version}</version>
</dependency>
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>
compile 'com.github.sitture:env-config:${version}'
To start using this:
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.
The default environment is set to default
and can be overridden by CONFIG_ENV
environment variable.
- create a
default
environment subdirectory underconfig
directory. - create a
default.properties
file in thedefault
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
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.
- create a keepass database file, add to your resources folder. i.e.
src/main/resources
orsrc/test/resources
.
CONFIG_KEEPASS_ENABLED
- A flag to enable reading of the keePass file. Default is set tofalse
.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.
- 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 beenv-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.
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.
You can get the current environment by:
EnvConfig.getEnvironment();
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.
// when a property is required to continue
EnvConfig.getOrThrow("my.property");
If the property isn't set then a MissingVariableException
is thrown.
// 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");
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.
You can add key/value pairs to the EnvConfig to be accessed somewhere else in the project.
EnvConfig.add("my.property", "my_value");
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.
You can clear an existing property in EnvConfig:
EnvConfig.clear("my.property")
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.
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.