Skip to content

Commit

Permalink
provide DotenvEntry toString(), fix execption message, and add maven …
Browse files Browse the repository at this point in the history
…example (#3)

* implement DotenvEntry toString

* (fix) exception message

* simple maven example

* increment patch version
  • Loading branch information
cdimascio authored Sep 19, 2020
1 parent f43b283 commit 5a4f1e4
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 2 deletions.
27 changes: 27 additions & 0 deletions examples/maven-simple/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# dotenv-java-example

Simple `dotenv-java` example using maven.

## Clone

```shell
git clone
```

## Compile

```shell
mvn compile
```

## Run

```shell
mvn exec:java -Dexec.mainClass="io.github.cdimascio.examples.dotenv.Main"
```

The program outputs the value of the env var `MY_ENV`. In this case, `MY_VALUE`

## License

MIT
23 changes: 23 additions & 0 deletions examples/maven-simple/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>cdimascio.github.io</groupId>
<artifactId>dotenv-java-example</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>io.github.cdimascio</groupId>
<artifactId>dotenv-java</artifactId>
<version>1.0.3</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

package io.github.cdimascio.examples.dotenv;

import io.github.cdimascio.dotenv.Dotenv;
import io.github.cdimascio.dotenv.DotenvEntry;


public class Main {

public static void main(String[] args) {
Dotenv dotenv = Dotenv.configure().load();

// Iterate over each environment entry
// Note: entries in the host environment override entries in .env
for (DotenvEntry e : dotenv.entries()) {
System.out.println(e);
}

// Retrieve the value of the MY_ENV environment variable
System.out.println(dotenv.get("MY_ENV"));

// Retrieve the value of the MY_ENV2 environment variable or return a default value
System.out.println(dotenv.get("MY_ENV2", "Default Value"));
}
}
1 change: 1 addition & 0 deletions examples/maven-simple/src/main/resources/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MY_ENV="MY_VALUE"
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<groupId>io.github.cdimascio</groupId>
<artifactId>dotenv-java</artifactId>
<version>1.0.2</version>
<version>1.0.3</version>

<licenses>
<license>
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/io/github/cdimascio/dotenv/DotenvEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,10 @@ public String getKey() {
public String getValue() {
return value;
}

@Override
public String toString() {
return key+"="+value;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ static Stream<String> loadFileFromClasspath(String location) {
}

if (inputStream == null) {
throw new DotenvException("Could not find $location on the classpath");
throw new DotenvException("Could not find "+location+" on the classpath");
}
var scanner = new Scanner(inputStream, "utf-8");
var lines = new ArrayList<String>();
Expand Down

0 comments on commit 5a4f1e4

Please sign in to comment.