Skip to content

fix: Load version.properties file as resource stream to fix loading when packaged as jar #1813

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 27, 2025
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@

import static software.amazon.lambda.powertools.core.internal.SystemWrapper.getenv;

import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.io.InputStream;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
* Can be used to create a string that can server as a User-Agent suffix in requests made with the AWS SDK clients
*/
Expand Down Expand Up @@ -54,32 +53,36 @@ static String getProjectVersion() {
return getVersionFromProperties(VERSION_PROPERTIES_FILENAME, VERSION_KEY);
}


/**
* Retrieves the project version from a properties file.
* The file should be in the resources folder.
* The version is retrieved from the property with the given key.
*
* @param propertyFileName the name of the properties file
* @param versionKey the key of the property that contains the version
* @param propertyFileName
* the name of the properties file
* @param versionKey
* the key of the property that contains the version
* @return the version of the project as configured in the given properties file
*/
static String getVersionFromProperties(String propertyFileName, String versionKey) {

URL propertiesFileURI = Thread.currentThread().getContextClassLoader().getResource(propertyFileName);
if (propertiesFileURI != null) {
try (FileInputStream fis = new FileInputStream(propertiesFileURI.getPath())) {
Properties properties = new Properties();
properties.load(fis);
String version = properties.getProperty(versionKey);
if (version != null && !version.isEmpty()) {
return version;
}
} catch (IOException e) {
try (final InputStream is = Thread.currentThread().getContextClassLoader()
.getResourceAsStream(propertyFileName)) {
if (is == null) {
LOG.warn("Unable to read {} file. Using default version.", propertyFileName);
LOG.debug("Exception:", e);
return NA;
}

Properties properties = new Properties();
properties.load(is);
String version = properties.getProperty(versionKey);
if (version != null && !version.isEmpty()) {
return version;
}
} catch (IOException e) {
LOG.warn("Unable to read {} file. Using default version.", propertyFileName);
LOG.debug("Exception:", e);
}

return NA;
}

Expand All @@ -90,9 +93,10 @@ static String getVersionFromProperties(String propertyFileName, String versionKe
* The PT_EXEC_ENV is automatically retrieved from the AWS_EXECUTION_ENV environment variable.
* If it AWS_EXECUTION_ENV is not set, PT_EXEC_ENV defaults to "NA"
*
* @param ptFeature a custom feature to be added to the user agent string (e.g. idempotency).
* If null or empty, the default PT_FEATURE is used.
* The default PT_FEATURE is "no-op".
* @param ptFeature
* a custom feature to be added to the user agent string (e.g. idempotency).
* If null or empty, the default PT_FEATURE is used.
* The default PT_FEATURE is "no-op".
* @return the user agent string
*/
public static String getUserAgent(String ptFeature) {
Expand Down
Loading