Skip to content

Commit e21fc25

Browse files
committed
Make defaultLibraryVersion() more robust
1 parent 5dfb904 commit e21fc25

1 file changed

Lines changed: 35 additions & 12 deletions

File tree

google-cloud-core/src/main/java/com/google/cloud/ServiceOptions.java

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import java.util.Enumeration;
4949
import java.util.Locale;
5050
import java.util.Objects;
51+
import java.util.Properties;
5152
import java.util.ServiceLoader;
5253
import java.util.Set;
5354
import java.util.jar.Attributes;
@@ -78,6 +79,9 @@ public abstract class ServiceOptions<ServiceT extends Service<OptionsT>, Service
7879
LIBRARY_VERSION == null ? LIBRARY_NAME : LIBRARY_NAME + "/" + LIBRARY_VERSION;
7980
private static final long serialVersionUID = -5714029257168617973L;
8081

82+
private static final String META_FILE_ROOT = "/META-INF/maven/";
83+
private static final String META_VERSION_KEY = "version";
84+
8185
private final String projectId;
8286
private final String host;
8387
private final RetryParams retryParams;
@@ -676,21 +680,40 @@ static <T> T getFromServiceLoader(Class<? extends T> clazz, T defaultInstance) {
676680
}
677681

678682
private static String defaultLibraryVersion() {
679-
String version = null;
680-
try {
681-
Enumeration<URL> resources =
682-
ServiceOptions.class.getClassLoader().getResources(JarFile.MANIFEST_NAME);
683-
while (resources.hasMoreElements() && version == null) {
684-
Manifest manifest = new Manifest(resources.nextElement().openStream());
685-
Attributes manifestAttributes = manifest.getMainAttributes();
686-
String artifactId = manifestAttributes.getValue(MANIFEST_ARTIFACT_ID_KEY);
687-
if (artifactId != null && artifactId.equals(ARTIFACT_ID)) {
688-
version = manifestAttributes.getValue(MANIFEST_VERSION_KEY);
683+
String version = getMavenVersion();
684+
if (version == null) {
685+
try {
686+
Enumeration<URL> resources =
687+
ServiceOptions.class.getClassLoader().getResources(JarFile.MANIFEST_NAME);
688+
while (resources.hasMoreElements() && version == null) {
689+
Manifest manifest = new Manifest(resources.nextElement().openStream());
690+
Attributes manifestAttributes = manifest.getMainAttributes();
691+
String artifactId = manifestAttributes.getValue(MANIFEST_ARTIFACT_ID_KEY);
692+
if (artifactId != null && artifactId.equals(ARTIFACT_ID)) {
693+
version = manifestAttributes.getValue(MANIFEST_VERSION_KEY);
694+
}
689695
}
696+
} catch (IOException e) {
697+
// ignore
690698
}
691-
} catch (IOException e) {
692-
// ignore
693699
}
694700
return version;
695701
}
702+
703+
private static String getMavenVersion() {
704+
try {
705+
Properties properties = new Properties();
706+
String mavenPropertiesPath = META_FILE_ROOT
707+
+ ServiceOptions.class.getPackage().getName() + "/"
708+
+ ARTIFACT_ID + "/pom.properties";
709+
InputStream inputStream = ServiceOptions.class.getResourceAsStream(mavenPropertiesPath);
710+
if (inputStream != null) {
711+
properties.load(inputStream);
712+
return properties.getProperty(META_VERSION_KEY, "");
713+
}
714+
} catch (Exception e) {
715+
// ignore
716+
}
717+
return null;
718+
}
696719
}

0 commit comments

Comments
 (0)