Popularity
2.3
Growing
Activity
0.0
Stable
42
3
3

Programming language: Java
Tags: Configuration    
Latest version: v0.1.1

dotenv alternatives and similar libraries

Based on the "Configuration" category.
Alternatively, view dotenv alternatives based on common mentions on social networks and blogs.

Do you think we are missing an alternative of dotenv or a related project?

Add another 'Configuration' Library

README

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.


*Note that all licence references and agreements mentioned in the dotenv README section above are relevant to that project's source code only.