A twelve-factor configuration (12factor.net/config) library for Java 8+

Related tags

Configuration dotenv
Overview

dotenv Build Status Maven Central

A twelve-factor configuration library for Java 8+.

Features:

Usage

<dependency>
    <groupId>com.github.shyiko.dotenv</groupId>
    <artifactId>dotenv</artifactId>
    <version>0.1.1</version>
</dependency>

ENV resolution order (sources higher in the list take precedence over those located lower):

  • System.getenv()
  • .env file in current working directory (might not exist)
  • .env file on the classpath

(java example)

Let's assume you have a jar file (say app.jar) which contains .env file (e.g. printf "SCHEME=http\nHOST=localhost\n# comment\nPORT=8080" > src/main/resources/.env) and Main.java with the following public static void main(...):

Map<String, String> dotEnv = DotEnv.load();
System.out.println(dotEnv.get("SCHEME") + "://" + dotEnv.get("HOST") + ":" + dotEnv.get("PORT"))

Executing the following

printf "HOST=0.0.0.0" > .env
PORT=5050 app.jar

will then output http://0.0.0.0:5050

Integration with Guice

<dependency>
    <groupId>com.github.shyiko.dotenv</groupId>
    <artifactId>dotenv-guice</artifactId>
    <version>0.1.1</version>
</dependency>

(Main.java)

static class S {
    private final String scheme;
    private final String host;
    private final int port;

    @Inject
    public S(
        @DotEnvValue("SCHEME") String scheme, 
        @DotEnvValue("HOST") String host, 
        @DotEnvValue("PORT") int port
    ) {
        this.scheme = scheme;
        this.host = host;
        this.port = port;
    }

    public void run() {
        System.out.println(scheme + "://" + host + ":" + port);
    }
}

public static void main(String[] args) {
    Injector injector = Guice.createInjector(new DotEnvModule()/*, ... */);
    S s = injector.getInstance(S.class);
    s.run();
}

Integration with Spring

(Main.java)

static class S {
    private final String scheme;
    private final String host;
    private final int port;

    @Autowired
    public S(
        @Value("${SCHEME}") String scheme, 
        @Value("${HOST}") String host, 
        @Value("${PORT}") int port
    ) {
        this.scheme = scheme;
        this.host = host;
        this.port = port;
    }

    public void run() {
        System.out.println(scheme + "://" + host + ":" + port);
    }
}

public static void main(String[] args) {
    ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext();
    ctx.getEnvironment().getPropertySources().addFirst(
        new MapPropertySource("dotenv", new HashMap<>(DotEnv.load()))
    );
    ctx.refresh();
    S s = ctx.getBeanFactory().createBean(S.class);
    s.run();
}

Development

git clone https://github.com/shyiko/dotenv && cd dotenv
./mvnw # shows how to build, test, etc. project

Legal

All code, unless specified otherwise, is licensed under the MIT license.
Copyright (c) 2017 Stanley Shyiko.

You might also like...

Nacos: Dynamic Naming and Configuration Service

 Nacos: Dynamic Naming and Configuration Service

An easy-to-use dynamic service discovery, configuration and service management platform for building cloud native applications.

Jan 3, 2023

Simple Ini Parser for Java or SIni4j is a simple INI parse made in Java

Simple Ini Parser for Java or SIni4j is a simple INI parse made in Java

Simple Ini Parser for Java or SIni4j is a simple INI parse made in Java, built for provide a simple and easiest way to load INI files in Java

Mar 18, 2022

COMMA Config API is an API allowing you to easily create complex and stylish config menu screens for Minecraft Fabric mods

COMMA Config API is an API allowing you to easily create complex and stylish config menu screens for Minecraft Fabric mods. COMMA stands for Configurable Options Menu Modding API

Jan 13, 2022

A library to create, read and validate ZUGFeRD compliant invoices. Available for Java and .NET

Konik ZUGFeRD Library Is an easy to use open source implementation of the ZUGFeRD data model including various enhancements. Features Easy and underst

Dec 20, 2022

Simple Java/POJO config library written with love and Lombok

Okaeri Configs Supported platforms (general use) General implementations based on standard format libraries directly. HJSON 🌟 hjson-java: Human JSON

Jan 7, 2023

KickAss Configuration. An annotation-based configuration system for Java and Kotlin

KAConf 2016-2020 Mario Macías KickAss Configuration v0.9.0 is an Annotation-based configuration system inspired in the wonderful Spring Boot. Its stro

Nov 21, 2022

This plugin disables commands specified in the configuration and prevents granting the "OP" authority to people not authorized in the plugin configuration.

This plugin disables commands specified in the configuration and prevents granting the

Ares This plugin disables commands specified in the configuration and prevents granting the "OP" authority to people not authorized in the plugin conf

Aug 30, 2022

Apollo is a reliable configuration management system suitable for microservice configuration management scenarios.

Apollo is a reliable configuration management system suitable for microservice configuration management scenarios.

English | 中文 Apollo - A reliable configuration management system Apollo is a reliable configuration management system. It can centrally manage the con

Jan 5, 2023

A harness to build the source code from openjdk.java.net using Free Software tools and dependencies

A harness to build the source code from openjdk.java.net using Free Software tools and dependencies

Mar 5, 2022

Apache Lucene.NET

Apache Lucene.NET Full-text search for .NET Apache Lucene.NET is a .NET full-text search engine framework, a C# port of the popular Apache Lucene proj

Jan 4, 2023

Sikuli's official repository on github. Ask questions or report bugs at http://launchpad.net/sikuli.

!!!This Sikuli X-1.0rc3 IS NO LONGER SUPPORTED !!! A new version of Sikuli(X) is available since 2013 as a follow up development GitHub repo: RaiMan/S

Jan 3, 2023

BRCC(better remote config center)是一个分布式配置中心,用于统一管理应用服务的配置信息,避免各类资源散落在各个项目中,简化资源配置的维护成本。作为一种轻量级的解决方案,部署简单,同时支持多环境、多版本、多角色的资源管理,可以在不改变应用源码的情况下无缝切换和实时生效配置信息。

BRCC(better remote config center)是一个分布式配置中心,用于统一管理应用服务的配置信息,避免各类资源散落在各个项目中,简化资源配置的维护成本。作为一种轻量级的解决方案,部署简单,同时支持多环境、多版本、多角色的资源管理,可以在不改变应用源码的情况下无缝切换和实时生效配置信息。

BRCC:Better Remote Config Center 简介   BRCC是一个分布式配置中心,用于统一管理应用服务的配置信息,避免各类资源散落在各个项目中,简化资源配置的维护成本。作为一种轻量级的解决方案,部署简单,同时支持多环境、多版本、多角色的资源管理,可以在不改变应用源码的情况下无

Dec 24, 2022

This repository shows how to natively extend Quarkus with a custom ConfigSource to use AWS AppConfig values when injecting config properties with @ConfigProperty.

Using AWS AppConfig in a custom MicroProfile ConfigSource This repository shows how to natively extend Quarkus with a custom ConfigSource to use AWS A

May 19, 2022

Demo microservice architecture with Spring ,Spring Cloud Gateway , Spring Cloud config server , Eureuka , keycloak and Docker.

Demo microservice architecture with Spring ,Spring Cloud Gateway , Spring Cloud config server , Eureuka , keycloak and Docker.

spring-microservice Demo microservice architecture with Spring ,Spring Cloud Gateway , Spring Cloud config server , Eureuka , keycloak and Docker. Arc

Sep 13, 2022

Config files for my GitHub profile.

- Board: Board function plays the most role in all classes of this Project ROLES: + initBoard(): used to display the score, the state of

Dec 30, 2022

Microserver is a Java 8 native, zero configuration, standards based, battle hardened library to run Java Rest Microservices via a standard Java main class. Supporting pure Microservice or Micro-monolith styles.

Microserver is a Java 8 native, zero configuration, standards based, battle hardened library to run Java Rest Microservices via a standard Java main class. Supporting pure Microservice or Micro-monolith styles.

Microserver A convenient modular engine for Microservices. Microserver plugins offer seamless integration with Spring (core), Jersey, Guava, Tomcat, G

Dec 19, 2022

Microserver is a Java 8 native, zero configuration, standards based, battle hardened library to run Java Rest Microservices via a standard Java main class. Supporting pure Microservice or Micro-monolith styles.

Microserver is a Java 8 native, zero configuration, standards based, battle hardened library to run Java Rest Microservices via a standard Java main class. Supporting pure Microservice or Micro-monolith styles.

Microserver is a Java 8 native, zero configuration, standards based, battle hardened library to run Java Rest Microservices via a standard Java main class. Supporting pure Microservice or Micro-monolith styles.

Dec 19, 2022

Modern configuration library for distributed apps written in Java.

Overview cfg4j ("configuration for Java") is a configuration library for Java distributed apps (and more). Features: Open source Easy to use Auto-relo

Nov 23, 2022
Releases(0.1.1)
Owner
Stanley Shyiko
Stanley Shyiko
KickAss Configuration. An annotation-based configuration system for Java and Kotlin

KAConf 2016-2020 Mario Macías KickAss Configuration v0.9.0 is an Annotation-based configuration system inspired in the wonderful Spring Boot. Its stro

Mario Macías 53 Nov 21, 2022
BRCC(better remote config center)是一个分布式配置中心,用于统一管理应用服务的配置信息,避免各类资源散落在各个项目中,简化资源配置的维护成本。作为一种轻量级的解决方案,部署简单,同时支持多环境、多版本、多角色的资源管理,可以在不改变应用源码的情况下无缝切换和实时生效配置信息。

BRCC:Better Remote Config Center 简介   BRCC是一个分布式配置中心,用于统一管理应用服务的配置信息,避免各类资源散落在各个项目中,简化资源配置的维护成本。作为一种轻量级的解决方案,部署简单,同时支持多环境、多版本、多角色的资源管理,可以在不改变应用源码的情况下无

Baidu 367 Dec 24, 2022
Modern configuration library for distributed apps written in Java.

Overview cfg4j ("configuration for Java") is a configuration library for Java distributed apps (and more). Features: Open source Easy to use Auto-relo

cfg4j 544 Nov 23, 2022
A simple configuration library for Java applications providing a node structure, a variety of formats, and tools for transformation

Configurate Configurate is a simple configuration library for Java applications that provides a node-based representation of data, able to handle a wi

SpongePowered 274 Jan 3, 2023
configuration library for JVM languages using HOCON files

Configuration library for JVM languages. If you have questions or are working on a pull request or just curious, please feel welcome to join the chat

Lightbend 5.8k Jan 4, 2023
configuration library for JVM languages using HOCON files

Configuration library for JVM languages. If you have questions or are working on a pull request or just curious, please feel welcome to join the chat

Lightbend 5.8k Jan 4, 2023
Library for configuration management API

Archaius Features Archaius includes a set of configuration management APIs used by Netflix. It provides the following functionality: Dynamic, Typed Pr

Netflix, Inc. 2.4k Dec 22, 2022
Highly-available version-controlled service configuration repository based on Git, ZooKeeper and HTTP/2

Visit the official web site for more information. Central Dogma Central Dogma is an open-source, highly-available and version-controlled service confi

LINE 503 Dec 20, 2022
Modern tool for microservice configuration management

Quick start We recommend to start with Microconfig Features guide and then continue reading this documentation. Microconfig overview and features Micr

Microconfig 262 Dec 19, 2022
Get rid of the boilerplate code in properties based configuration.

OWNER OWNER, an API to ease Java property files usage. INTRODUCTION The goal of OWNER API is to minimize the code required to handle application confi

lviggiano 874 Dec 31, 2022