Guest User

Untitled

a guest
Dec 18th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.47 KB | None | 0 0
  1. JarInputStream jarStream = new JarInputStream(stream);
  2. Manifest mf = jarStream.getManifest();
  3.  
  4. public static String getManifestInfo() {
  5. Enumeration resEnum;
  6. try {
  7. resEnum = Thread.currentThread().getContextClassLoader().getResources(JarFile.MANIFEST_NAME);
  8. while (resEnum.hasMoreElements()) {
  9. try {
  10. URL url = (URL)resEnum.nextElement();
  11. InputStream is = url.openStream();
  12. if (is != null) {
  13. Manifest manifest = new Manifest(is);
  14. Attributes mainAttribs = manifest.getMainAttributes();
  15. String version = mainAttribs.getValue("Implementation-Version");
  16. if(version != null) {
  17. return version;
  18. }
  19. }
  20. }
  21. catch (Exception e) {
  22. // Silently ignore wrong manifests on classpath?
  23. }
  24. }
  25. } catch (IOException e1) {
  26. // Silently ignore wrong manifests on classpath?
  27. }
  28. return null;
  29. }
  30.  
  31. Package aPackage = MyClassName.class.getPackage();
  32. String implementationVersion = aPackage.getImplementationVersion();
  33. String implementationVendor = aPackage.getImplementationVendor();
  34.  
  35. import java.io.File;
  36. import java.net.URL;
  37. import java.util.jar.Attributes;
  38. import java.util.jar.Manifest;
  39.  
  40. import org.apache.commons.lang.StringUtils;
  41. import org.slf4j.Logger;
  42. import org.slf4j.LoggerFactory;
  43.  
  44. public class AppVersion {
  45. private static final Logger log = LoggerFactory.getLogger(AppVersion.class);
  46.  
  47. private static String version;
  48.  
  49. public static String get() {
  50. if (StringUtils.isBlank(version)) {
  51. Class<?> clazz = AppVersion.class;
  52. String className = clazz.getSimpleName() + ".class";
  53. String classPath = clazz.getResource(className).toString();
  54. if (!classPath.startsWith("jar")) {
  55. // Class not from JAR
  56. String relativePath = clazz.getName().replace('.', File.separatorChar) + ".class";
  57. String classFolder = classPath.substring(0, classPath.length() - relativePath.length() - 1);
  58. String manifestPath = classFolder + "/META-INF/MANIFEST.MF";
  59. log.debug("manifestPath={}", manifestPath);
  60. version = readVersionFrom(manifestPath);
  61. } else {
  62. String manifestPath = classPath.substring(0, classPath.lastIndexOf("!") + 1) + "/META-INF/MANIFEST.MF";
  63. log.debug("manifestPath={}", manifestPath);
  64. version = readVersionFrom(manifestPath);
  65. }
  66. }
  67. return version;
  68. }
  69.  
  70. private static String readVersionFrom(String manifestPath) {
  71. Manifest manifest = null;
  72. try {
  73. manifest = new Manifest(new URL(manifestPath).openStream());
  74. Attributes attrs = manifest.getMainAttributes();
  75.  
  76. String implementationVersion = attrs.getValue("Implementation-Version");
  77. implementationVersion = StringUtils.replace(implementationVersion, "-SNAPSHOT", "");
  78. log.debug("Read Implementation-Version: {}", implementationVersion);
  79.  
  80. String implementationBuild = attrs.getValue("Implementation-Build");
  81. log.debug("Read Implementation-Build: {}", implementationBuild);
  82.  
  83. String version = implementationVersion;
  84. if (StringUtils.isNotBlank(implementationBuild)) {
  85. version = StringUtils.join(new String[] { implementationVersion, implementationBuild }, '.');
  86. }
  87. return version;
  88. } catch (Exception e) {
  89. log.error(e.getMessage(), e);
  90. }
  91. return StringUtils.EMPTY;
  92. }
  93. }
  94.  
  95. final String value = Manifests.read("My-Version");
  96.  
  97. public static String readManifest(String sourceJARFile) throws IOException
  98. {
  99. ZipFile zipFile = new ZipFile(sourceJARFile);
  100. Enumeration entries = zipFile.entries();
  101.  
  102. while (entries.hasMoreElements())
  103. {
  104. ZipEntry zipEntry = (ZipEntry) entries.nextElement();
  105. if (zipEntry.getName().equals("META-INF/MANIFEST.MF"))
  106. {
  107. return toString(zipFile.getInputStream(zipEntry));
  108. }
  109. }
  110.  
  111. throw new IllegalStateException("Manifest not found");
  112. }
  113.  
  114. private static String toString(InputStream inputStream) throws IOException
  115. {
  116. StringBuilder stringBuilder = new StringBuilder();
  117. try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)))
  118. {
  119. String line;
  120. while ((line = bufferedReader.readLine()) != null)
  121. {
  122. stringBuilder.append(line);
  123. stringBuilder.append(System.lineSeparator());
  124. }
  125. }
  126.  
  127. return stringBuilder.toString().trim() + System.lineSeparator();
  128. }
Add Comment
Please, Sign In to add comment