Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: build

on:
push:
branches: [ "master", "main" ]
pull_request:
branches: [ "master", "main" ]

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Set up JDK 11
uses: actions/setup-java@v3
with:
java-version: '11'
distribution: 'adopt'
cache: maven
- name: Build with Maven
run: mvn clean package jacoco:report
10 changes: 0 additions & 10 deletions .travis.yml

This file was deleted.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 🗝️ dotenv-java

![](https://travis-ci.org/cdimascio/dotenv-java.svg?branch=master) [![Coverage Status](https://coveralls.io/repos/github/cdimascio/dotenv-java/badge.svg?branch=fixes)](https://coveralls.io/github/cdimascio/dotenv-java?branch=fixes) [![Maven Central](https://img.shields.io/maven-central/v/io.github.cdimascio/dotenv-java.svg?label=Maven%20Central)](https://search.maven.org/artifact/io.github.cdimascio/dotenv-java) [![Codacy Badge](https://api.codacy.com/project/badge/Grade/66b8195f0da544f1ad9ed1352c0ea66f)](https://app.codacy.com/app/cdimascio/dotenv-java?utm_source=github.com&utm_medium=referral&utm_content=cdimascio/dotenv-java&utm_campaign=Badge_Grade_Dashboard) [![](https://img.shields.io/gitter/room/cdimascio-oss/community?color=%23eb205a)](https://gitter.im/cdimascio-oss/community) [![All Contributors](https://img.shields.io/badge/all_contributors-6-orange.svg?style=flat-square)](#contributors-) [![](https://img.shields.io/badge/doc-javadoc-blue)](https://cdimascio.github.io/dotenv-java/docs/javadoc/index.html) ![](https://img.shields.io/badge/license-Apache%202.0-blue.svg)
![Build Status](https://github.com/cloudsimplus/cloudsimplus/actions/workflows/build.yml/badge.svg) [![Maven Central](https://img.shields.io/maven-central/v/io.github.cdimascio/dotenv-java.svg?label=Maven%20Central)](https://search.maven.org/artifact/io.github.cdimascio/dotenv-java) [![Codacy Badge](https://api.codacy.com/project/badge/Grade/66b8195f0da544f1ad9ed1352c0ea66f)](https://app.codacy.com/app/cdimascio/dotenv-java?utm_source=github.com&utm_medium=referral&utm_content=cdimascio/dotenv-java&utm_campaign=Badge_Grade_Dashboard) [![](https://img.shields.io/gitter/room/cdimascio-oss/community?color=%23eb205a)](https://gitter.im/cdimascio-oss/community) [![All Contributors](https://img.shields.io/badge/all_contributors-6-orange.svg?style=flat-square)](#contributors-) [![](https://img.shields.io/badge/doc-javadoc-blue)](https://cdimascio.github.io/dotenv-java/docs/javadoc/index.html) ![](https://img.shields.io/badge/license-Apache%202.0-blue.svg)

A no-dependency, pure Java port of the Ruby dotenv project. Load environment variables from a `.env` file.

Expand Down
70 changes: 21 additions & 49 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<groupId>io.github.cdimascio</groupId>
<artifactId>dotenv-java</artifactId>
<version>2.3.2</version>
<version>2.4.0</version>

<licenses>
<license>
Expand Down Expand Up @@ -55,23 +55,20 @@
<module.name>io.github.cdimascio.dotenv.java</module.name>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<compile.source>1.8</compile.source>
<compile.test.source>11</compile.test.source>
<compile.javadoc.source>8</compile.javadoc.source>
<maven.compiler.release>11</maven.compiler.release>

<javadoc.dir>docs/javadoc</javadoc.dir>

<junit.version>4.13.2</junit.version>
<junit.version>5.9.2</junit.version>

<maven.enforcer.plugin>3.1.0</maven.enforcer.plugin>
<maven.compiler.plugin>3.5.1</maven.compiler.plugin>
<maven.source.plugin>3.0.1</maven.source.plugin>
<maven.javadoc.plugin>3.2.0</maven.javadoc.plugin>
<maven.surefire.plugin>2.22.0</maven.surefire.plugin>
<maven.jacoco.plugin>0.8.6</maven.jacoco.plugin>
<maven.coveralls.plugin>4.3.0</maven.coveralls.plugin>
<maven.enforcer.plugin>3.3.0</maven.enforcer.plugin>
<maven.compiler.plugin>3.11.0</maven.compiler.plugin>
<maven.source.plugin>3.2.1</maven.source.plugin>
<maven.javadoc.plugin>3.5.0</maven.javadoc.plugin>
<maven.surefire.plugin>3.0.0</maven.surefire.plugin>
<maven.jacoco.plugin>0.8.10</maven.jacoco.plugin>
<maven.copy.rename.plugin>1.0.1</maven.copy.rename.plugin>
<maven.moditect.plugin>1.0.0.RC2</maven.moditect.plugin>
<maven.moditect.plugin>1.0.0.RC3</maven.moditect.plugin>

<bintray.subject>cdimascio</bintray.subject>
<bintray.repo>maven</bintray.repo>
Expand All @@ -88,8 +85,14 @@

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
Expand Down Expand Up @@ -127,17 +130,10 @@
</executions>
</plugin>

<!-- compile java 1.8 for lib and java 11 for tests-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.plugin}</version>
<configuration>
<source>${compile.source}</source>
<target>${compile.source}</target>
<testSource>${compile.test.source}</testSource>
<testTarget>${compile.test.source}</testTarget>
</configuration>
</plugin>

<!-- add module-info -->
Expand Down Expand Up @@ -194,13 +190,6 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven.surefire.plugin}</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit4</artifactId>
<version>2.22.0</version>
</dependency>
</dependencies>
<configuration>
<includes>
<include>**/*.java</include>
Expand Down Expand Up @@ -257,23 +246,6 @@
</executions>
</plugin>

<!-- Send to coveralls -->
<plugin>
<groupId>org.eluder.coveralls</groupId>
<artifactId>coveralls-maven-plugin</artifactId>
<version>${maven.coveralls.plugin}</version>
<configuration>
<repoToken>r92OLg0S3kCJakLAMW6HlLsDgNSMaolGS</repoToken>
</configuration>
<dependencies>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.2.3</version>
</dependency>
</dependencies>
</plugin>

<!-- copy pom to target -->
<plugin>
<groupId>com.coderplus.maven.plugins</groupId>
Expand All @@ -290,7 +262,7 @@
<fileSets>
<fileSet>
<sourceFile>${project.basedir}/pom.xml</sourceFile>
<destinationFile>${project.build.directory}/dotenv-java-2.2.4.pom</destinationFile>
<destinationFile>${project.build.directory}/${project.artifactId}-${project.version}.pom</destinationFile>
</fileSet>
</fileSets>
</configuration>
Expand All @@ -315,7 +287,7 @@
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.7</version>
<version>1.6.13</version>
<extensions>true</extensions>
<configuration>
<serverId>ossrh</serverId>
Expand Down Expand Up @@ -355,7 +327,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.5</version>
<version>3.0.1</version>
<executions>
<execution>
<id>sign-artifacts</id>
Expand Down
56 changes: 31 additions & 25 deletions src/main/java/io/github/cdimascio/dotenv/DotenvBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class DotenvBuilder {
* @param path the directory containing the .env file
* @return this {@link DotenvBuilder}
*/
public DotenvBuilder directory(String path) {
public DotenvBuilder directory(final String path) {
this.directoryPath = path;
return this;
}
Expand All @@ -32,7 +32,7 @@ public DotenvBuilder directory(String path) {
* @param name the filename
* @return this {@link DotenvBuilder}
*/
public DotenvBuilder filename(String name) {
public DotenvBuilder filename(final String name) {
filename = name;
return this;
}
Expand Down Expand Up @@ -70,34 +70,40 @@ public DotenvBuilder systemProperties() {
* @throws DotenvException when an error occurs
*/
public Dotenv load() throws DotenvException {
DotenvParser reader = new DotenvParser(
new DotenvReader(directoryPath, filename),
throwIfMissing,
throwIfMalformed);
List<DotenvEntry> env = reader.parse();
final var reader = new DotenvParser(
new DotenvReader(directoryPath, filename),
throwIfMissing, throwIfMalformed);
final List<DotenvEntry> env = reader.parse();
if (systemProperties) {
env.forEach(it -> System.setProperty(it.getKey(), it.getValue()));
}

return new DotenvImpl(env);
}

static class DotenvImpl implements Dotenv {
private final Map<String, String> envVars;
private final Set<DotenvEntry> set;
private final Set<DotenvEntry> setInFile;
public DotenvImpl(List<DotenvEntry> envVars) {
Map<String, String> envVarsInFile = envVars.stream()
.collect(toMap(DotenvEntry::getKey, DotenvEntry::getValue));
public DotenvImpl(final List<DotenvEntry> envVars) {
final Map<String, String> envVarsInFile =
envVars.stream()
.collect(toMap(DotenvEntry::getKey, DotenvEntry::getValue));

this.envVars = new HashMap<>(envVarsInFile);
this.envVars.putAll(System.getenv());

this.set = this.envVars.entrySet().stream()
.map(it -> new DotenvEntry(it.getKey(), it.getValue()))
.collect(collectingAndThen(toSet(), Collections::unmodifiableSet));

this.setInFile = envVarsInFile.entrySet().stream()
.map(it -> new DotenvEntry(it.getKey(), it.getValue()))
.collect(collectingAndThen(toSet(), Collections::unmodifiableSet));
this.set =
this.envVars.entrySet()
.stream()
.map(it -> new DotenvEntry(it.getKey(), it.getValue()))
.collect(collectingAndThen(toSet(), Collections::unmodifiableSet));

this.setInFile =
envVarsInFile.entrySet()
.stream()
.map(it -> new DotenvEntry(it.getKey(), it.getValue()))
.collect(collectingAndThen(toSet(), Collections::unmodifiableSet));
}

@Override
Expand All @@ -106,21 +112,21 @@ public Set<DotenvEntry> entries() {
}

@Override
public Set<DotenvEntry> entries(Dotenv.Filter filter) {
if (filter != null) return setInFile;
return entries();
public Set<DotenvEntry> entries(final Dotenv.Filter filter) {
return filter == null ? entries() : setInFile;

}

@Override
public String get(String key) {
String value = System.getenv(key);
return value != null ? value : envVars.get(key);
public String get(final String key) {
final String value = System.getenv(key);
return value == null ? envVars.get(key) : value;
}

@Override
public String get(String key, String defaultValue) {
String value = this.get(key);
return value != null ? value : defaultValue;
final String value = this.get(key);
return value == null ? defaultValue : value;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

import io.github.cdimascio.dotenv.DotenvException;

import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Stream;

Expand All @@ -13,8 +12,8 @@
*/
public class ClasspathHelper {
static Stream<String> loadFileFromClasspath(String location) {
Class<ClasspathHelper> loader = ClasspathHelper.class;
InputStream inputStream = loader.getResourceAsStream(location);
final var loader = ClasspathHelper.class;
var inputStream = loader.getResourceAsStream(location);
if (inputStream == null) {
inputStream = loader.getResourceAsStream(location);
}
Expand All @@ -25,11 +24,13 @@ static Stream<String> loadFileFromClasspath(String location) {
if (inputStream == null) {
throw new DotenvException("Could not find "+location+" on the classpath");
}
Scanner scanner = new Scanner(inputStream, "utf-8");
List<String> lines = new ArrayList<>();

final var scanner = new Scanner(inputStream, StandardCharsets.UTF_8);
final var lines = new ArrayList<String>();
while (scanner.hasNext()) {
lines.add(scanner.nextLine());
}

return lines.stream();
}
}
Loading