Skip to content

Commit 5a4f1e4

Browse files
authoredSep 19, 2020
provide DotenvEntry toString(), fix execption message, and add maven example (#3)
* implement DotenvEntry toString * (fix) exception message * simple maven example * increment patch version
1 parent f43b283 commit 5a4f1e4

File tree

7 files changed

+83
-2
lines changed

7 files changed

+83
-2
lines changed
 

‎examples/maven-simple/README.md

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# dotenv-java-example
2+
3+
Simple `dotenv-java` example using maven.
4+
5+
## Clone
6+
7+
```shell
8+
git clone
9+
```
10+
11+
## Compile
12+
13+
```shell
14+
mvn compile
15+
```
16+
17+
## Run
18+
19+
```shell
20+
mvn exec:java -Dexec.mainClass="io.github.cdimascio.examples.dotenv.Main"
21+
```
22+
23+
The program outputs the value of the env var `MY_ENV`. In this case, `MY_VALUE`
24+
25+
## License
26+
27+
MIT

‎examples/maven-simple/pom.xml

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="http://maven.apache.org/POM/4.0.0"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>cdimascio.github.io</groupId>
8+
<artifactId>dotenv-java-example</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<properties>
12+
<maven.compiler.source>11</maven.compiler.source>
13+
<maven.compiler.target>11</maven.compiler.target>
14+
</properties>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>io.github.cdimascio</groupId>
19+
<artifactId>dotenv-java</artifactId>
20+
<version>1.0.3</version>
21+
</dependency>
22+
</dependencies>
23+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
package io.github.cdimascio.examples.dotenv;
3+
4+
import io.github.cdimascio.dotenv.Dotenv;
5+
import io.github.cdimascio.dotenv.DotenvEntry;
6+
7+
8+
public class Main {
9+
10+
public static void main(String[] args) {
11+
Dotenv dotenv = Dotenv.configure().load();
12+
13+
// Iterate over each environment entry
14+
// Note: entries in the host environment override entries in .env
15+
for (DotenvEntry e : dotenv.entries()) {
16+
System.out.println(e);
17+
}
18+
19+
// Retrieve the value of the MY_ENV environment variable
20+
System.out.println(dotenv.get("MY_ENV"));
21+
22+
// Retrieve the value of the MY_ENV2 environment variable or return a default value
23+
System.out.println(dotenv.get("MY_ENV2", "Default Value"));
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
MY_ENV="MY_VALUE"

‎pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
<groupId>io.github.cdimascio</groupId>
1212
<artifactId>dotenv-java</artifactId>
13-
<version>1.0.2</version>
13+
<version>1.0.3</version>
1414

1515
<licenses>
1616
<license>

‎src/main/java/io/github/cdimascio/dotenv/DotenvEntry.java

+5
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,10 @@ public String getKey() {
1616
public String getValue() {
1717
return value;
1818
}
19+
20+
@Override
21+
public String toString() {
22+
return key+"="+value;
23+
}
1924
}
2025

‎src/main/java/io/github/cdimascio/dotenv/internal/ClasspathHelper.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ static Stream<String> loadFileFromClasspath(String location) {
1818
}
1919

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

0 commit comments

Comments
 (0)
Please sign in to comment.