diff --git a/pom.xml.releaseBackup b/pom.xml.releaseBackup
new file mode 100644
index 0000000..8ec197c
--- /dev/null
+++ b/pom.xml.releaseBackup
@@ -0,0 +1,197 @@
+
+
+ 4.0.0
+ io.github.ngirchev
+ dotenv
+ 1.0.0-SNAPSHOT
+
+ dotenv
+ Simplest dotenv utility for Java applications. Load environment variables from .env files into System properties.
+ https://github.com/NGirchev/dotenv
+
+
+
+ MIT License
+ https://opensource.org/licenses/MIT
+ repo
+
+
+
+
+
+ ngirchev
+ Nikolai Girchev
+
+
+
+
+ scm:git:https://github.com/NGirchev/dotenv.git
+ scm:git:git@github.com:NGirchev/dotenv.git
+ https://github.com/NGirchev/dotenv
+ HEAD
+
+
+
+ 11
+ 11
+
+ 3.11.0
+ 3.11.0
+ 3.2.1
+ 3.5.0
+ 3.0.0
+ 3.3.0
+ 3.2.4
+
+ UTF-8
+ UTF-8
+
+
+
+
+
+ org.apache.maven.plugins
+ maven-enforcer-plugin
+ ${maven.enforcer.plugin}
+
+
+ enforce-java
+
+ enforce
+
+
+
+
+ 11
+
+
+
+
+
+
+
+
+ org.apache.maven.plugins
+ maven-compiler-plugin
+ ${maven.compiler.plugin}
+
+
+
+ org.apache.maven.plugins
+ maven-surefire-plugin
+ ${maven.surefire.plugin}
+
+
+
+ org.apache.maven.plugins
+ maven-source-plugin
+ ${maven.source.plugin}
+
+
+ attach-sources
+
+ jar
+
+
+
+
+
+
+ org.apache.maven.plugins
+ maven-javadoc-plugin
+ ${maven.javadoc.plugin}
+
+
+ attach-javadocs
+
+ jar
+
+
+
+
+
+
+ org.apache.maven.plugins
+ maven-release-plugin
+ 3.0.0
+
+ v@{project.version}
+ true
+ release
+
+
+
+
+ org.sonatype.plugins
+ nexus-staging-maven-plugin
+ 1.6.13
+ true
+
+ ossrh
+ https://oss.sonatype.org/
+
+ false
+
+
+
+
+
+
+
+
+ org.slf4j
+ slf4j-api
+ 1.7.36
+
+
+
+
+ org.slf4j
+ slf4j-simple
+ 1.7.36
+
+
+
+
+ org.junit.jupiter
+ junit-jupiter
+ 5.9.2
+ test
+
+
+
+
+
+ ossrh
+ https://oss.sonatype.org/content/repositories/snapshots
+
+
+ ossrh
+ https://oss.sonatype.org/service/local/staging/deploy/maven2/
+
+
+
+
+
+ release
+
+
+
+ org.apache.maven.plugins
+ maven-gpg-plugin
+ ${maven.gpg.plugin}
+
+
+ sign-artifacts
+ verify
+
+ sign
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/release.properties b/release.properties
new file mode 100644
index 0000000..7037854
--- /dev/null
+++ b/release.properties
@@ -0,0 +1,27 @@
+#release configuration
+#Sun Dec 07 20:40:09 EET 2025
+completedPhase=end-release
+exec.additionalArguments="-DskipTests"
+exec.pomFileName=pom.xml
+exec.snapshotReleasePluginAllowed=false
+pinExternals=false
+preparationGoals=clean verify
+project.dev.io.github.ngirchev\:dotenv=1.0.1-SNAPSHOT
+project.rel.io.github.ngirchev\:dotenv=1.0.0
+project.scm.io.github.ngirchev\:dotenv.connection=scm\:git\:https\://github.com/NGirchev/dotenv.git
+project.scm.io.github.ngirchev\:dotenv.developerConnection=scm\:git\:git@github.com\:NGirchev/dotenv.git
+project.scm.io.github.ngirchev\:dotenv.tag=HEAD
+project.scm.io.github.ngirchev\:dotenv.url=https\://github.com/NGirchev/dotenv
+projectVersionPolicyConfig=${projectVersionPolicyConfig}\n
+projectVersionPolicyId=default
+pushChanges=true
+releaseStrategyId=default
+remoteTagging=true
+scm.branchCommitComment=@{prefix} prepare branch @{releaseLabel}
+scm.commentPrefix=[maven-release-plugin]
+scm.developmentCommitComment=@{prefix} prepare for next development iteration
+scm.releaseCommitComment=@{prefix} prepare release @{releaseLabel}
+scm.rollbackCommitComment=@{prefix} rollback the release of @{releaseLabel}
+scm.tag=v1.0.0
+scm.tagNameFormat=v@{project.version}
+scm.url=scm\:git\:git@github.com\:NGirchev/dotenv.git
diff --git a/sonar-project.properties b/sonar-project.properties
new file mode 100644
index 0000000..9c5e1e6
--- /dev/null
+++ b/sonar-project.properties
@@ -0,0 +1,14 @@
+sonar.projectKey=NGirchev_dotenv
+sonar.organization=ngirchev
+sonar.sources=src/main/java
+sonar.tests=src/test/java
+sonar.java.binaries=target/classes
+sonar.java.test.binaries=target/test-classes
+sonar.coverage.jacoco.xmlReportPaths=target/site/jacoco/jacoco.xml
+sonar.junit.reportPaths=target/surefire-reports
+sonar.sourceEncoding=UTF-8
+sonar.host.url=https://sonarcloud.io
+sonar.language=java
+sonar.java.source=11
+sonar.java.target=11
+
diff --git a/src/main/java/io/github/ngirchev/dotenv/DotEnvLoader.java b/src/main/java/io/github/ngirchev/dotenv/DotEnvLoader.java
index b1d484b..4f9ad59 100644
--- a/src/main/java/io/github/ngirchev/dotenv/DotEnvLoader.java
+++ b/src/main/java/io/github/ngirchev/dotenv/DotEnvLoader.java
@@ -10,76 +10,182 @@
import java.util.stream.Stream;
/**
- * Utility class for loading environment variables from .env file
+ * Utility class for loading environment variables from .env files into Java System properties.
+ *
+ * This class provides a simple way to manage application configuration by loading
+ * key-value pairs from a `.env` file into System properties. The loaded properties can
+ * then be accessed using {@link #getEnv(String)} or standard Java System property methods.
+ *
+ *
Key features:
+ *
+ * - Loads variables from `.env` files into System properties
+ * - Does not overwrite existing System properties or environment variables
+ * - Supports comments (lines starting with #)
+ * - Automatically trims whitespace from keys and values
+ * - Silently handles missing files and invalid lines
+ *
+ *
+ * Example usage:
+ *
{@code
+ * // Load from default .env file in project root
+ * DotEnvLoader.loadDotEnv();
+ *
+ * // Access loaded variables
+ * String dbHost = DotEnvLoader.getEnv("DB_HOST");
+ * }
+ *
+ * @author NGirchev
+ * @since 1.0.0
*/
-public class DotEnvLoader {
+public final class DotEnvLoader {
- private static final Logger log = LoggerFactory.getLogger(DotEnvLoader.class);
+ private static final Logger LOG = LoggerFactory.getLogger(DotEnvLoader.class);
private DotEnvLoader() {
}
/**
- * Loads environment variables from .env file into System properties.
- * Does not overwrite existing values in System properties or environment variables.
+ * Loads environment variables from a specified .env file into System properties.
+ *
+ * This method reads the `.env` file line by line and loads key-value pairs
+ * into System properties. The following rules apply:
+ *
+ * - Lines starting with '#' are treated as comments and ignored
+ * - Empty lines are ignored
+ * - Whitespace around keys and values is automatically trimmed
+ * - Existing System properties and environment variables are never overwritten
+ * - If the file doesn't exist, the method returns silently (logs debug message)
+ * - Invalid lines (without '=' or with empty keys) are ignored
+ *
+ *
+ * Example .env file format:
+ *
{@code
+ * # Database configuration
+ * DB_HOST=localhost
+ * DB_PORT=5432
+ * API_KEY=your-secret-key
+ * }
+ *
+ * Example usage:
+ *
{@code
+ * import java.nio.file.Paths;
+ *
+ * // Load from custom path
+ * DotEnvLoader.loadDotEnv(Paths.get("config/.env"));
+ * }
*
- * @param envPath path to .env file (default is ".env" in project root)
+ * @param envPath the path to the .env file to load. Must not be null.
+ * @throws NullPointerException if envPath is null
+ * @see #loadDotEnv()
+ * @see #getEnv
*/
- public static void loadDotEnv(Path envPath) {
+ public static void loadDotEnv(final Path envPath) {
if (!Files.exists(envPath)) {
- log.debug(".env file not found at {}, skipping dotenv loading", envPath);
+ if (LOG.isDebugEnabled()) {
+ LOG.debug(".env file not found at {}, skipping dotenv loading", envPath);
+ }
return;
}
try (Stream lines = Files.lines(envPath)) {
lines.map(String::trim)
.filter(line -> !line.isEmpty())
- .filter(line -> !line.startsWith("#"))
+ .filter(line -> line.length() > 0 && line.charAt(0) != '#')
.forEach(line -> {
- int idx = line.indexOf('=');
+ final int idx = line.indexOf('=');
if (idx <= 0) {
return;
}
- String key = line.substring(0, idx).trim();
- String value = line.substring(idx + 1).trim();
+ final String key = line.substring(0, idx).trim();
if (key.isEmpty()) {
return;
}
+ final String value = line.substring(idx + 1).trim();
+
// Do not overwrite existing values
if (System.getProperty(key) == null && System.getenv(key) == null) {
System.setProperty(key, value);
- log.debug("Loaded .env property: {}", key);
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("Loaded .env property: {}", key);
+ }
}
});
- log.info(".env properties loaded into System properties from {}", envPath);
+ if (LOG.isInfoEnabled()) {
+ LOG.info(".env properties loaded into System properties from {}", envPath);
+ }
} catch (IOException e) {
- log.warn("Failed to load .env file from {}: {}. Skipping dotenv loading.", envPath, e.getMessage());
+ if (LOG.isWarnEnabled()) {
+ LOG.warn("Failed to load .env file from {}: {}. Skipping dotenv loading.", envPath, e.getMessage());
+ }
}
}
/**
- * Loads environment variables from .env file in project root.
- * Convenient method for default usage.
+ * Loads environment variables from the default `.env` file in the project root directory.
+ *
+ * This is a convenience method that calls {@link #loadDotEnv(Path)} with
+ * the path to `.env` in the current working directory (project root).
+ *
+ *
Example usage:
+ *
+ * public class MyApp {
+ * public static void main(String[] args) {
+ * // Load from default .env file
+ * DotEnvLoader.loadDotEnv();
+ *
+ * // Use loaded variables
+ * String apiKey = DotEnvLoader.getEnv("API_KEY");
+ * }
+ * }
*/
public static void loadDotEnv() {
loadDotEnv(Paths.get(".env"));
}
/**
- * Gets environment variable value from System properties or environment variables.
- * First checks System properties (loaded from .env), then environment variables.
+ * Gets an environment variable value from System properties or system environment variables.
+ *
+ * This method checks for the variable in the following order:
+ *
+ * - System properties (loaded from `.env` file via {@link #loadDotEnv()})
+ * - System environment variables
+ *
+ *
+ * If the variable is found in System properties, that value is returned.
+ * Otherwise, the method checks system environment variables. If the variable
+ * is not found in either location, {@code null} is returned.
+ *
+ *
Example usage:
+ *
{@code
+ * // Load .env file first
+ * DotEnvLoader.loadDotEnv();
+ *
+ * // Get variable value
+ * String dbHost = DotEnvLoader.getEnv("DB_HOST");
+ * if (dbHost != null) {
+ * System.out.println("Database host: " + dbHost);
+ * } else {
+ * System.out.println("DB_HOST not set");
+ * }
+ * }
*
- * @param key variable name
- * @return variable value or null if not found
+ * @param key the name of the environment variable to retrieve. Must not be null.
+ * @return the value of the environment variable, or {@code null} if not found
+ * @throws NullPointerException if key is null
+ * @see System#getProperty(String)
+ * @see System#getenv(String)
*/
- public static String getEnv(String key) {
- String value = System.getProperty(key);
+ public static String getEnv(final String key) {
+ final String value = System.getProperty(key);
+ final String result;
if (value != null) {
- return value;
+ result = value;
+ } else {
+ result = System.getenv(key);
}
- return System.getenv(key);
+ return result;
}
}
diff --git a/src/site/markdown/index.md b/src/site/markdown/index.md
new file mode 100644
index 0000000..feaf402
--- /dev/null
+++ b/src/site/markdown/index.md
@@ -0,0 +1,24 @@
+# dotenv
+
+Simplest dotenv utility for Java applications. Load environment variables from .env files into System properties.
+
+## Code Quality Reports
+
+The following code quality reports are available:
+
+* [SpotBugs](./spotbugs.html) - Static code analysis report
+* [PMD](./pmd.html) - Code quality analysis report
+* [Checkstyle](./checkstyle.html) - Code style checking report
+* [CPD](./cpd.html) - Duplicate code detection report
+* [JaCoCo](./jacoco/index.html) - Code coverage report
+* [Surefire Report](./surefire-report.html) - Test results report
+* [Javadoc](./apidocs/index.html) - API documentation
+
+For a complete list of reports, see [Project Reports](./project-reports.html).
+
+## Project Information
+
+* [Project Information](./project-info.html) - General project information
+* [Dependencies](./dependencies.html) - Project dependencies
+* [Licenses](./licenses.html) - Project licenses
+
diff --git a/src/site/site.xml b/src/site/site.xml
new file mode 100644
index 0000000..9031767
--- /dev/null
+++ b/src/site/site.xml
@@ -0,0 +1,20 @@
+
+
+
+ org.apache.maven.skins
+ maven-default-skin
+ 1.3
+
+
+
+
+ true
+ true
+
+
+
+