SkillAgentSearch skills...

Dotenv

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

Install / Use

/learn @shyiko/Dotenv
About this skill

Quality Score

0/100

Supported Platforms

Universal

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.

View on GitHub
GitHub Stars50
CategoryDevelopment
Updated6mo ago
Forks3

Languages

Java

Security Score

72/100

Audited on Sep 24, 2025

No findings