]> source.dussan.org Git - iciql.git/commitdiff
Use version metadata stamped by the build process
authorJames Moger <james.moger@gitblit.com>
Mon, 17 Nov 2014 22:30:23 +0000 (17:30 -0500)
committerJames Moger <james.moger@gitblit.com>
Fri, 10 Apr 2015 13:02:04 +0000 (09:02 -0400)
src/main/java/com/iciql/Constants.java
src/test/java/com/iciql/test/IciqlSuite.java

index ba75de3f2adf11f0e5fef6b7191b4cc92d899ec7..6b5d83a198f4fb89a2c99f44d40e1959ade916be 100644 (file)
 \r
 package com.iciql;\r
 \r
+import java.net.URL;\r
+import java.util.jar.Attributes;\r
+import java.util.jar.Manifest;\r
+\r
 /**\r
  * Iciql constants.\r
  */\r
@@ -25,14 +29,36 @@ public class Constants {
 \r
        // The build script extracts this exact line so be careful editing it\r
        // and only use A-Z a-z 0-9 .-_ in the string.\r
-       public static final String VERSION = "1.6.0-SNAPSHOT";\r
+       public static final String API_CURRENT = "15";\r
 \r
-       // The build script extracts this exact line so be careful editing it\r
-       // and only use A-Z a-z 0-9 .-_ in the string.\r
-       public static final String VERSION_DATE = "PENDING";\r
+       public static String getVersion() {\r
+               String v = Constants.class.getPackage().getImplementationVersion();\r
+               if (v == null) {\r
+                       return "0.0.0-SNAPSHOT";\r
+               }\r
+               return v;\r
+       }\r
 \r
-       // The build script extracts this exact line so be careful editing it\r
-       // and only use A-Z a-z 0-9 .-_ in the string.\r
-       public static final String API_CURRENT = "15";\r
+       public static String getBuildDate() {\r
+               return getManifestValue("build-date", "PENDING");\r
+       }\r
 \r
+       private static String getManifestValue(String attrib, String defaultValue) {\r
+               Class<?> clazz = Constants.class;\r
+               String className = clazz.getSimpleName() + ".class";\r
+               String classPath = clazz.getResource(className).toString();\r
+               if (!classPath.startsWith("jar")) {\r
+                       // Class not from JAR\r
+                       return defaultValue;\r
+               }\r
+               try {\r
+                       String manifestPath = classPath.substring(0, classPath.lastIndexOf("!") + 1) + "/META-INF/MANIFEST.MF";\r
+                       Manifest manifest = new Manifest(new URL(manifestPath).openStream());\r
+                       Attributes attr = manifest.getMainAttributes();\r
+                       String value = attr.getValue(attrib);\r
+                       return value;\r
+               } catch (Exception e) {\r
+               }\r
+               return defaultValue;\r
+       }\r
 }\r
index c80da93dc1a7ee0219b9b07042252bdcaad0dbbf..f7306573d785da021d5349534de8315a1f5a1408 100644 (file)
@@ -353,7 +353,7 @@ public class IciqlSuite {
                // Header\r
                out.println(dividerMajor);\r
                out.println(MessageFormat.format("{0} {1} ({2}) testing {3} database configurations", Constants.NAME,\r
-                               Constants.VERSION, Constants.VERSION_DATE, TEST_DBS.length));\r
+                               Constants.getVersion(), Constants.getBuildDate(), TEST_DBS.length));\r
                out.println(dividerMajor);\r
                out.println();\r
 \r
@@ -432,7 +432,7 @@ public class IciqlSuite {
                out.println();\r
                out.println(dividerMajor);\r
                out.println(MessageFormat.format("{0} {1} ({2}) test suite performance results", Constants.NAME,\r
-                               Constants.VERSION, Constants.VERSION_DATE));\r
+                               Constants.getVersion(), Constants.getBuildDate()));\r
 \r
                StringBuilder compressedSystem = new StringBuilder();\r
                compressedSystem.append("      on ");\r
@@ -496,7 +496,7 @@ public class IciqlSuite {
        }\r
 \r
        private static void usage(JCommander jc, ParameterException t) {\r
-               System.out.println(Constants.NAME + " test suite v" + Constants.VERSION);\r
+               System.out.println(Constants.NAME + " test suite v" + Constants.getVersion());\r
                System.out.println();\r
                if (t != null) {\r
                        System.out.println(t.getMessage());\r