From a47d3cb80f7570b58a1ae9b651a4d0d9d7da8c84 Mon Sep 17 00:00:00 2001 From: Konstantin Shaposhnikov Date: Tue, 2 Jun 2015 20:53:27 +0800 Subject: [PATCH 1/3] PARQUET-297: fix reading parquet version --- .../main/java/org/apache/parquet/Version.java | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/parquet-common/src/main/java/org/apache/parquet/Version.java b/parquet-common/src/main/java/org/apache/parquet/Version.java index 6580f333ab..928972157d 100644 --- a/parquet-common/src/main/java/org/apache/parquet/Version.java +++ b/parquet-common/src/main/java/org/apache/parquet/Version.java @@ -34,14 +34,16 @@ */ public class Version { private static final Log LOG = Log.getLog(Version.class); - + + private static final String PARQUET_COLUMN_POM_RESOURCE = "META-INF/maven/org.apache.parquet/parquet-column/pom.properties"; + public static final String VERSION_NUMBER = readVersionNumber(); public static final String FULL_VERSION = readFullVersion(); private static String getJarPath() { - final URL versionClassBaseUrl = Version.class.getResource(""); - if (versionClassBaseUrl.getProtocol().equals("jar")) { - String path = versionClassBaseUrl.getPath(); + final URL baseUrl = Version.class.getResource("/" + PARQUET_COLUMN_POM_RESOURCE); + if (baseUrl != null && baseUrl.getProtocol().equals("jar")) { + String path = baseUrl.getPath(); int jarEnd = path.indexOf("!"); if (jarEnd != -1) { String jarPath = path.substring(0, jarEnd); @@ -61,13 +63,13 @@ private static URL getResourceFromJar(String jarPath, String path) throws IOExce } return null; } - + private static String readVersionNumber() { String version = null; try { String jarPath = getJarPath(); if (jarPath != null) { - URL pomPropertiesUrl = getResourceFromJar(jarPath, "META-INF/maven/com.twitter/parquet-column/pom.properties"); + URL pomPropertiesUrl = getResourceFromJar(jarPath, PARQUET_COLUMN_POM_RESOURCE); if (pomPropertiesUrl != null) { Properties properties = new Properties(); properties.load(pomPropertiesUrl.openStream()); @@ -75,7 +77,7 @@ private static String readVersionNumber() { } } } catch (Exception e) { - LOG.warn("can't read from META-INF", e); + LOG.warn("can't read from " + PARQUET_COLUMN_POM_RESOURCE, e); } return version; } From cf58cf629f28971537771a05ee64d1db055696f7 Mon Sep 17 00:00:00 2001 From: Konstantin Shaposhnikov Date: Thu, 4 Jun 2015 22:16:40 +0800 Subject: [PATCH 2/3] PARQUET-297: fix reading parquet version (attempt 2) This change simplifies the way version is read: - version.properties is created at build time and packaged inside parquet-common.jar (along with pom.properties) - Version class reads both values VERSION_NUMBER and FULL_VERSION from this file --- parquet-common/pom.xml | 6 ++ .../main/java/org/apache/parquet/Version.java | 85 +++++-------------- .../parquet-common/version.properties | 21 +++++ .../java/org/apache/parquet/TestVersion.java | 36 ++++++++ 4 files changed, 84 insertions(+), 64 deletions(-) create mode 100644 parquet-common/src/main/resources/META-INF/maven/org.apache.parquet/parquet-common/version.properties create mode 100644 parquet-common/src/test/java/org/apache/parquet/TestVersion.java diff --git a/parquet-common/pom.xml b/parquet-common/pom.xml index b6468f5509..0a76ec04e4 100644 --- a/parquet-common/pom.xml +++ b/parquet-common/pom.xml @@ -39,6 +39,12 @@ + + + src/main/resources + true + + maven-enforcer-plugin diff --git a/parquet-common/src/main/java/org/apache/parquet/Version.java b/parquet-common/src/main/java/org/apache/parquet/Version.java index 928972157d..523a87dffe 100644 --- a/parquet-common/src/main/java/org/apache/parquet/Version.java +++ b/parquet-common/src/main/java/org/apache/parquet/Version.java @@ -18,11 +18,8 @@ */ package org.apache.parquet; -import java.io.IOException; -import java.net.URL; -import java.util.Enumeration; +import java.io.InputStream; import java.util.Properties; -import java.util.jar.Manifest; /** * The version of the library @@ -30,73 +27,33 @@ * parquet-mr version 1.0.0-SNAPSHOT (build 6cf94d29b2b7115df4de2c06e2ab4326d721eb55) * * @author Julien Le Dem - * */ public class Version { private static final Log LOG = Log.getLog(Version.class); - private static final String PARQUET_COLUMN_POM_RESOURCE = "META-INF/maven/org.apache.parquet/parquet-column/pom.properties"; - - public static final String VERSION_NUMBER = readVersionNumber(); - public static final String FULL_VERSION = readFullVersion(); - - private static String getJarPath() { - final URL baseUrl = Version.class.getResource("/" + PARQUET_COLUMN_POM_RESOURCE); - if (baseUrl != null && baseUrl.getProtocol().equals("jar")) { - String path = baseUrl.getPath(); - int jarEnd = path.indexOf("!"); - if (jarEnd != -1) { - String jarPath = path.substring(0, jarEnd); - return jarPath; - } - } - return null; - } - - private static URL getResourceFromJar(String jarPath, String path) throws IOException { - Enumeration resources = Version.class.getClassLoader().getResources(path); - while (resources.hasMoreElements()) { - URL url = resources.nextElement(); - if (url.getProtocol().equals("jar") && url.getPath().startsWith(jarPath)) { - return url; + public static final String VERSION_NUMBER; + public static final String FULL_VERSION; + + static { + String versionNumber = null; + String fullVersion = null; + + InputStream in = Version.class.getResourceAsStream("/META-INF/maven/org.apache.parquet/parquet-common/version.properties"); + if (in != null) { + try { + Properties props = new Properties(); + props.load(in); + versionNumber = props.getProperty("versionNumber"); + fullVersion = props.getProperty("fullVersion"); + } catch (Exception e) { + LOG.warn("can't read from version information", e); } + } else { + LOG.warn("can't read from version information"); } - return null; - } - private static String readVersionNumber() { - String version = null; - try { - String jarPath = getJarPath(); - if (jarPath != null) { - URL pomPropertiesUrl = getResourceFromJar(jarPath, PARQUET_COLUMN_POM_RESOURCE); - if (pomPropertiesUrl != null) { - Properties properties = new Properties(); - properties.load(pomPropertiesUrl.openStream()); - version = properties.getProperty("version"); - } - } - } catch (Exception e) { - LOG.warn("can't read from " + PARQUET_COLUMN_POM_RESOURCE, e); - } - return version; - } - - private static String readFullVersion() { - String sha = null; - try { - String jarPath = getJarPath(); - if (jarPath != null) { - URL manifestUrl = getResourceFromJar(jarPath, "META-INF/MANIFEST.MF"); - if (manifestUrl != null) { - Manifest manifest = new Manifest(manifestUrl.openStream()); - sha = manifest.getMainAttributes().getValue("git-SHA-1"); - } - } - } catch (Exception e) { - LOG.warn("can't read from META-INF", e); - } - return "parquet-mr" + (VERSION_NUMBER != null ? " version " + VERSION_NUMBER : "") + (sha != null ? " (build " + sha + ")" : ""); + VERSION_NUMBER = versionNumber; + FULL_VERSION = fullVersion; } public static void main(String[] args) { diff --git a/parquet-common/src/main/resources/META-INF/maven/org.apache.parquet/parquet-common/version.properties b/parquet-common/src/main/resources/META-INF/maven/org.apache.parquet/parquet-common/version.properties new file mode 100644 index 0000000000..f93f390598 --- /dev/null +++ b/parquet-common/src/main/resources/META-INF/maven/org.apache.parquet/parquet-common/version.properties @@ -0,0 +1,21 @@ +# +# 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. + +versionNumber = ${project.version} +gitSHA1 = ${buildNumber} +fullVersion = parquet-mr version ${project.version} (build ${buildNumber}) diff --git a/parquet-common/src/test/java/org/apache/parquet/TestVersion.java b/parquet-common/src/test/java/org/apache/parquet/TestVersion.java new file mode 100644 index 0000000000..cade56fd30 --- /dev/null +++ b/parquet-common/src/test/java/org/apache/parquet/TestVersion.java @@ -0,0 +1,36 @@ +/** + * 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.parquet; + +import java.io.IOException; +import java.util.Properties; + +import org.junit.Assert; +import org.junit.Test; + +public class TestVersion { + @Test + public void testVersionIsPopulatedFromVersionProperties() throws IOException { + Properties expected = new Properties(); + expected.load(TestVersion.class.getResourceAsStream("/META-INF/maven/org.apache.parquet/parquet-common/version.properties")); + + Assert.assertEquals(expected.getProperty("versionNumber"), Version.VERSION_NUMBER); + Assert.assertEquals(expected.getProperty("fullVersion"), Version.FULL_VERSION); + } +} From b5861b1029b50105d8a08b98ac740a6ea5e3c3b1 Mon Sep 17 00:00:00 2001 From: Konstantin Shaposhnikov Date: Thu, 4 Jun 2015 22:22:37 +0800 Subject: [PATCH 3/3] PARQUET-297: minor fix to the error message --- parquet-common/src/main/java/org/apache/parquet/Version.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/parquet-common/src/main/java/org/apache/parquet/Version.java b/parquet-common/src/main/java/org/apache/parquet/Version.java index 523a87dffe..1fb28136b2 100644 --- a/parquet-common/src/main/java/org/apache/parquet/Version.java +++ b/parquet-common/src/main/java/org/apache/parquet/Version.java @@ -46,10 +46,10 @@ public class Version { versionNumber = props.getProperty("versionNumber"); fullVersion = props.getProperty("fullVersion"); } catch (Exception e) { - LOG.warn("can't read from version information", e); + LOG.warn("can't read version information", e); } } else { - LOG.warn("can't read from version information"); + LOG.warn("can't read version information"); } VERSION_NUMBER = versionNumber;