|
48 | 48 | import java.util.Enumeration; |
49 | 49 | import java.util.Locale; |
50 | 50 | import java.util.Objects; |
| 51 | +import java.util.Properties; |
51 | 52 | import java.util.ServiceLoader; |
52 | 53 | import java.util.Set; |
53 | 54 | import java.util.jar.Attributes; |
@@ -78,6 +79,9 @@ public abstract class ServiceOptions<ServiceT extends Service<OptionsT>, Service |
78 | 79 | LIBRARY_VERSION == null ? LIBRARY_NAME : LIBRARY_NAME + "/" + LIBRARY_VERSION; |
79 | 80 | private static final long serialVersionUID = -5714029257168617973L; |
80 | 81 |
|
| 82 | + private static final String META_FILE_ROOT = "/META-INF/maven/"; |
| 83 | + private static final String META_VERSION_KEY = "version"; |
| 84 | + |
81 | 85 | private final String projectId; |
82 | 86 | private final String host; |
83 | 87 | private final RetryParams retryParams; |
@@ -676,21 +680,40 @@ static <T> T getFromServiceLoader(Class<? extends T> clazz, T defaultInstance) { |
676 | 680 | } |
677 | 681 |
|
678 | 682 | 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 | + } |
689 | 695 | } |
| 696 | + } catch (IOException e) { |
| 697 | + // ignore |
690 | 698 | } |
691 | | - } catch (IOException e) { |
692 | | - // ignore |
693 | 699 | } |
694 | 700 | return version; |
695 | 701 | } |
| 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 | + } |
696 | 719 | } |
0 commit comments