Skip to content

Commit

Permalink
support for java 8 (#15)
Browse files Browse the repository at this point in the history
* java 8 support

* update pom

* update pom.xml

* update javadoc

* update pom.xml
  • Loading branch information
cdimascio authored Oct 2, 2020
1 parent ae3ac10 commit 55d90af
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 49 deletions.
74 changes: 49 additions & 25 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.0.0</version>
<version>2.1.0</version>

<licenses>
<license>
Expand Down Expand Up @@ -51,14 +51,24 @@


<properties>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.source>11</maven.compiler.source>
<main.class>io.github.cdimascio.dotenv.Dotenv</main.class>
<junit.version>4.12</junit.version>
<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>

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

<junit.version>4.12</junit.version>

<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.copy.rename.plugin>1.0.1</maven.copy.rename.plugin>

<bintray.subject>cdimascio</bintray.subject>
<bintray.repo>maven</bintray.repo>
<bintray.package>dotenv-java</bintray.package>
Expand All @@ -85,6 +95,19 @@
<sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
<plugins>

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

<!-- delete the javadoc dir -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand All @@ -106,11 +129,11 @@
</executions>
</plugin>

<!-- compile sources -->
<!-- plugin for tests -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
<version>${maven.surefire.plugin}</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
Expand Down Expand Up @@ -146,6 +169,7 @@
<artifactId>maven-javadoc-plugin</artifactId>
<version>${maven.javadoc.plugin}</version>
<configuration>
<source>${compile.javadoc.source}</source>
</configuration>
<executions>
<execution>
Expand All @@ -162,7 +186,7 @@
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.6</version>
<version>${maven.jacoco.plugin}</version>
<executions>
<execution>
<id>prepare-agent</id>
Expand All @@ -177,7 +201,7 @@
<plugin>
<groupId>org.eluder.coveralls</groupId>
<artifactId>coveralls-maven-plugin</artifactId>
<version>4.3.0</version>
<version>${maven.coveralls.plugin}</version>
<configuration>
<repoToken>r92OLg0S3kCJakLAMW6HlLsDgNSMaolGS</repoToken>
</configuration>
Expand All @@ -194,7 +218,7 @@
<plugin>
<groupId>com.coderplus.maven.plugins</groupId>
<artifactId>copy-rename-maven-plugin</artifactId>
<version>1.0.1</version>
<version>${maven.copy.rename.plugin}</version>
<executions>
<execution>
<id>copy-file</id>
Expand All @@ -206,26 +230,26 @@
<fileSets>
<fileSet>
<sourceFile>${project.basedir}/pom.xml</sourceFile>
<destinationFile>${project.build.directory}/dotenv-java-2.0.0.pom</destinationFile>
<destinationFile>${project.build.directory}/dotenv-java-2.1.0.pom</destinationFile>
</fileSet>
</fileSets>
</configuration>
</execution>
<!-- <execution>-->
<!-- <id>rename-dir</id>-->
<!-- <phase>install</phase>-->
<!-- <goals>-->
<!-- <goal>rename</goal>-->
<!-- </goals>-->
<!-- <configuration>-->
<!-- <fileSets>-->
<!-- <fileSet>-->
<!-- <sourceFile>${project.build.directory}/apidocs/</sourceFile>-->
<!-- <destinationFile>${project.basedir}/docs/javadoc/</destinationFile>-->
<!-- </fileSet>-->
<!-- </fileSets>-->
<!-- </configuration>-->
<!-- </execution>-->
<!-- <execution>-->
<!-- <id>rename-dir</id>-->
<!-- <phase>install</phase>-->
<!-- <goals>-->
<!-- <goal>rename</goal>-->
<!-- </goals>-->
<!-- <configuration>-->
<!-- <fileSets>-->
<!-- <fileSet>-->
<!-- <sourceFile>${project.build.directory}/apidocs/</sourceFile>-->
<!-- <destinationFile>${project.basedir}/docs/javadoc/</destinationFile>-->
<!-- </fileSet>-->
<!-- </fileSets>-->
<!-- </configuration>-->
<!-- </execution>-->
</executions>
</plugin>
</plugins>
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/io/github/cdimascio/dotenv/DotenvBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public Dotenv load() throws DotenvException {
new DotenvReader(directoryPath, filename),
throwIfMissing,
throwIfMalformed);
var env = reader.parse();
List<DotenvEntry> env = reader.parse();
if (systemProperties) {
env.forEach(it -> System.setProperty(it.getKey(), it.getValue()));
}
Expand Down Expand Up @@ -117,13 +117,13 @@ public Set<DotenvEntry> entries(Dotenv.Filter filter) {

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

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

import io.github.cdimascio.dotenv.DotenvException;

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

public class ClasspathHelper {
static Stream<String> loadFileFromClasspath(String location) {
Class<ClasspathHelper> loader = ClasspathHelper.class;
var inputStream = loader.getResourceAsStream(location);
InputStream inputStream = loader.getResourceAsStream(location);
if (inputStream == null) {
inputStream = loader.getResourceAsStream(location);
}
Expand All @@ -20,8 +22,8 @@ static Stream<String> loadFileFromClasspath(String location) {
if (inputStream == null) {
throw new DotenvException("Could not find "+location+" on the classpath");
}
var scanner = new Scanner(inputStream, "utf-8");
var lines = new ArrayList<String>();
Scanner scanner = new Scanner(inputStream, "utf-8");
List<String> lines = new ArrayList<>();
while (scanner.hasNext()) {
lines.add(scanner.nextLine());
}
Expand Down
25 changes: 13 additions & 12 deletions src/main/java/io/github/cdimascio/dotenv/internal/DotenvParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DotenvParser {
Expand All @@ -26,18 +27,18 @@ public DotenvParser(DotenvReader reader, boolean throwIfMissing, boolean throwIf
}

public List<DotenvEntry> parse() throws DotenvException {
var entries = new ArrayList<DotenvEntry>();
for (var line : lines()) {
var l = line.trim();
List<DotenvEntry> entries = new ArrayList<>();
for (String line : lines()) {
String l = line.trim();
if (isWhiteSpace.apply(l) || isComment.apply(l) || l.isBlank()) continue;

var entry = parseLine.apply(l);
DotenvEntry entry = parseLine.apply(l);
if (entry == null) {
if (throwIfMalformed) throw new DotenvException("Malformed entry "+ l);
continue;
}
var key = entry.getKey();
var value = normalizeValue(entry.getValue());
String key = entry.getKey();
String value = normalizeValue(entry.getValue());
entries.add(new DotenvEntry(key, value));
}
return entries;
Expand All @@ -55,22 +56,22 @@ private List<String> lines() throws DotenvException {
}

private String normalizeValue(String value) {
var tr = value.trim();
String tr = value.trim();
return isQuoted.apply(tr)
? tr.substring(1, value.length() -1)
: tr;
}

private static boolean matches(String regex, String text) {
var pattern = Pattern.compile(regex);
var matcher = pattern.matcher(text);
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(text);
return matcher.matches();
}

private static DotenvEntry matchEntry(String regex, String text) {
var pattern = Pattern.compile(regex);
var matcher = pattern.matcher(text);
var result = matcher.matches();
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(text);
boolean result = matcher.matches();
if (!result || matcher.groupCount() < 3) return null;
return new DotenvEntry(matcher.group(1), matcher.group(3));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.net.URI;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;
Expand All @@ -20,14 +21,14 @@ public DotenvReader(String directory, String filename) {
}

public List<String> read() throws DotenvException, IOException {
var dir = directory
String dir = directory
.replaceAll("\\\\", "/")
.replaceFirst("\\.env$", "")
.replaceFirst("/$", "");

var location = dir + "/" + filename;
var lowerLocation = location.toLowerCase();
var path = lowerLocation.startsWith("file:") || lowerLocation.startsWith("android.resource:")
String location = dir + "/" + filename;
String lowerLocation = location.toLowerCase();
Path path = lowerLocation.startsWith("file:") || lowerLocation.startsWith("android.resource:")
? Paths.get(URI.create(location))
: Paths.get(location);

Expand All @@ -42,8 +43,8 @@ public List<String> read() throws DotenvException, IOException {
.loadFileFromClasspath(location.replaceFirst("./", "/"))
.collect(Collectors.toList());
} catch (DotenvException e) {
var cwd = FileSystems.getDefault().getPath(".").toAbsolutePath().normalize();
var cwdMessage = !path.isAbsolute() ? "(working directory: " + cwd + ")" : "";
Path cwd = FileSystems.getDefault().getPath(".").toAbsolutePath().normalize();
String cwdMessage = !path.isAbsolute() ? "(working directory: " + cwd + ")" : "";
e.addSuppressed(new DotenvException("Could not find " + path + " on the file system " + cwdMessage));
throw e;
}
Expand Down

0 comments on commit 55d90af

Please sign in to comment.