DRILL-4203: Fix DrillVersionInfo to make it provide a valid version number even 
during the unit tests.

This is now a build-time generated class, rather than one that looks on the
classpath for META-INF files.

This pattern for file generation with parameters passed from the POM files
was borrowed from parquet-mr.


Project: http://git-wip-us.apache.org/repos/asf/drill/repo
Commit: http://git-wip-us.apache.org/repos/asf/drill/commit/2f4b5ef7
Tree: http://git-wip-us.apache.org/repos/asf/drill/tree/2f4b5ef7
Diff: http://git-wip-us.apache.org/repos/asf/drill/diff/2f4b5ef7

Branch: refs/heads/master
Commit: 2f4b5ef717ed78a1ebb65687af9688a902e02041
Parents: 17b9648
Author: Jason Altekruse <altekruseja...@gmail.com>
Authored: Wed Jan 27 10:20:01 2016 -0800
Committer: Parth Chandra <par...@apache.org>
Committed: Fri Oct 14 11:07:38 2016 -0700

----------------------------------------------------------------------
 .../drill/common/util/DrillVersionInfo.java     | 58 -------------
 .../org/apache/drill/version/Generator.java     | 90 ++++++++++++++++++++
 exec/vector/pom.xml                             | 22 +++++
 3 files changed, 112 insertions(+), 58 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/drill/blob/2f4b5ef7/common/src/main/java/org/apache/drill/common/util/DrillVersionInfo.java
----------------------------------------------------------------------
diff --git 
a/common/src/main/java/org/apache/drill/common/util/DrillVersionInfo.java 
b/common/src/main/java/org/apache/drill/common/util/DrillVersionInfo.java
deleted file mode 100644
index 5fb0d41..0000000
--- a/common/src/main/java/org/apache/drill/common/util/DrillVersionInfo.java
+++ /dev/null
@@ -1,58 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.drill.common.util;
-
-import java.io.IOException;
-import java.net.URL;
-import java.util.Enumeration;
-import java.util.jar.Manifest;
-
-/**
- * Get access to the Drill Version
- */
-public class DrillVersionInfo {
-
-  /**
-   * Get the Drill version from the Manifest file
-   * @return the version number as x.y.z
-   */
-  public static String getVersion() {
-    String appName = "";
-    String appVersion = "Unknown";
-    try {
-      Enumeration<URL> resources = DrillVersionInfo.class.getClassLoader()
-              .getResources("META-INF/MANIFEST.MF");
-      while (resources.hasMoreElements()) {
-        Manifest manifest = new Manifest(resources.nextElement().openStream());
-        // check that this is your manifest and do what you need or
-        // get the next one
-        appName = manifest.getMainAttributes()
-                .getValue("Implementation-Title");
-        if (appName != null && appName.toLowerCase().contains("drill")) {
-          appVersion = manifest.getMainAttributes()
-                  .getValue("Implementation-Version");
-        }
-      }
-    } catch (IOException except) {
-      appVersion = "Unknown";
-    }
-    return appVersion;
-  }
-
-
-}

http://git-wip-us.apache.org/repos/asf/drill/blob/2f4b5ef7/common/src/main/java/org/apache/drill/version/Generator.java
----------------------------------------------------------------------
diff --git a/common/src/main/java/org/apache/drill/version/Generator.java 
b/common/src/main/java/org/apache/drill/version/Generator.java
new file mode 100644
index 0000000..66607ed
--- /dev/null
+++ b/common/src/main/java/org/apache/drill/version/Generator.java
@@ -0,0 +1,90 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.drill.version;
+
+import com.google.common.base.Preconditions;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+
+/**
+ * This class is called from the exec->vector module using Maven-exec.
+ * To make use of the class, it needs to be compiled in an earlier module.
+ * For the configuration see the pom.xml file in that module.
+ */
+public class Generator {
+
+  public static void main(String[] args) {
+    String toReplace = "REPLACE_WITH_DRILL_VERSION";
+    String template =
+        "/**\n" +
+        " * Licensed to the Apache Software Foundation (ASF) under one\n" +
+        " * or more contributor license agreements.  See the NOTICE file\n" +
+        " * distributed with this work for additional information\n" +
+        " * regarding copyright ownership.  The ASF licenses this file\n" +
+        " * to you under the Apache License, Version 2.0 (the\n" +
+        " * \"License\"); you may not use this file except in compliance\n" +
+        " * with the License.  You may obtain a copy of the License at\n" +
+        " *\n" +
+        " * http://www.apache.org/licenses/LICENSE-2.0\n"; +
+        " *\n" +
+        " * Unless required by applicable law or agreed to in writing, 
software\n" +
+        " * distributed under the License is distributed on an \"AS IS\" 
BASIS,\n" +
+        " * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
implied.\n" +
+        " * See the License for the specific language governing permissions 
and\n" +
+        " * limitations under the License.\n" +
+        " */\n" +
+        "package org.apache.drill.common.util;\n" +
+        "\n" +
+        "/**\n" +
+        " * Get access to the Drill Version\n" +
+        " */\n" +
+        "// File generated during build, DO NOT EDIT!!\n" +
+        "public class DrillVersionInfo {\n" +
+        "\n" +
+        "  /**\n" +
+        "   * Get the Drill version from the Manifest file\n" +
+        "   * @return the version number as x.y.z\n" +
+        "   */\n" +
+        "  public static String getVersion() {\n" +
+        "    return \"" + toReplace + "\";\n" +
+        "  }\n" +
+        "}\n";
+    Preconditions.checkArgument(args.length == 2,
+        "Two arguments expected, the first is the target java source directory 
for the generated file" +
+            " and the second is the Drill version.");
+    File srcFile = new File(args[0] + 
"/org/apache/drill/common/util/DrillVersionInfo.java");
+    srcFile = srcFile.getAbsoluteFile();
+    File parent = srcFile.getParentFile();
+    if (!parent.exists()) {
+      if (!parent.mkdirs()) {
+        throw new RuntimeException("Error generating Drill version info class. 
Couldn't mkdirs for " + parent);
+      }
+    }
+    final FileWriter writer;
+    try {
+      writer = new FileWriter(srcFile);
+      writer.write(template.replace(toReplace, args[1]));
+      writer.close();
+    } catch (IOException e) {
+      throw new RuntimeException("Error generating Drill version info class. " 
+
+          "Couldn't open source file for writing: " + srcFile);
+    }
+  }
+}

http://git-wip-us.apache.org/repos/asf/drill/blob/2f4b5ef7/exec/vector/pom.xml
----------------------------------------------------------------------
diff --git a/exec/vector/pom.xml b/exec/vector/pom.xml
index 861ac79..5e5e3ed 100644
--- a/exec/vector/pom.xml
+++ b/exec/vector/pom.xml
@@ -120,6 +120,28 @@
           </execution>
         </executions>
       </plugin>
+      <plugin> <!-- generate class to provide current version number -->
+        <groupId>org.codehaus.mojo</groupId>
+        <artifactId>exec-maven-plugin</artifactId>
+        <version>1.2.1</version>
+        <executions>
+          <execution>
+            <phase>generate-sources</phase>
+            <goals>
+              <goal>java</goal>
+            </goals>
+          </execution>
+        </executions>
+        <configuration>
+          <mainClass>org.apache.drill.version.Generator</mainClass>
+          <includePluginDependencies>true</includePluginDependencies>
+          <arguments>
+            <argument>${basedir}/target/generated-sources</argument>
+            <argument>${project.version}</argument>
+          </arguments>
+          <sourceRoot>${basedir}/target/generated-sources</sourceRoot>
+        </configuration>
+      </plugin>
     </plugins>
     <pluginManagement>
       <plugins>

Reply via email to