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 6580f333ab..1fb28136b2 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,71 +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); - - 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(); - int jarEnd = path.indexOf("!"); - if (jarEnd != -1) { - String jarPath = path.substring(0, jarEnd); - return jarPath; - } - } - return null; - } + public static final String VERSION_NUMBER; + public static final String FULL_VERSION; - 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; - } - } - 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"); - if (pomPropertiesUrl != null) { - Properties properties = new Properties(); - properties.load(pomPropertiesUrl.openStream()); - version = properties.getProperty("version"); - } - } - } catch (Exception e) { - LOG.warn("can't read from META-INF", e); - } - return version; - } + static { + String versionNumber = null; + String fullVersion = null; - 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"); - } + 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 version information", e); } - } catch (Exception e) { - LOG.warn("can't read from META-INF", e); + } else { + LOG.warn("can't read version information"); } - 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); + } +}