Use manifest file for version info

This removes a hack I'd left in the build file that hard coded the
hash of jdbc driver. Now we dig the version information out of the
MANIFEST.MF file that is written during the jar process for all
projects in the Elasticsearch build.

Original commit: elastic/x-pack-elasticsearch@fa01cc6fb3
This commit is contained in:
Nik Everett 2017-07-25 13:30:35 -04:00
parent 823149e456
commit dd29035e66
3 changed files with 46 additions and 48 deletions

View File

@ -7,39 +7,11 @@ description = 'JDBC driver for Elasticsearch'
def generatedResources = "$buildDir/generated-resources/main"
sourceSets {
main {
output.dir(generatedResources, builtBy: "generateGitHash")
}
}
forbiddenApisMain {
// does not depend on core, so only jdk and http signatures should be checked
signaturesURLs = [this.class.getResource('/forbidden/jdk-signatures.txt')]
}
task generateGitHash {
// TODO use the manifest file automatically built by elasticsearch.build
doLast {
Version current = Version.fromString(versions.elasticsearch)
String revHash = '123123123123123'
Properties props = new Properties()
props.put("version", versions.elasticsearch)
props.put("hash", revHash)
props.put("version.major", current.major as String)
props.put("version.minor", current.minor as String)
File output = new File(generatedResources, "jdbc-build.properties")
new File(generatedResources).mkdirs()
output.createNewFile()
def writer = output.newWriter("UTF-8")
try {
props.store(writer, null)
} finally {
writer.close()
}
}
}
dependencies {
compile project(':x-pack-elasticsearch:sql:net-client')
compile project(':x-pack-elasticsearch:sql:jdbc-proto')
@ -116,3 +88,7 @@ forbiddenApisTest {
bundledSignatures -= 'jdk-non-portable'
bundledSignatures += 'jdk-internal'
}
test.configure {
systemProperty 'tests.version.available', 'true'
}

View File

@ -5,22 +5,20 @@
*/
package org.elasticsearch.xpack.sql.jdbc.util;
import org.elasticsearch.xpack.sql.net.client.util.StringUtils;
import java.io.IOException;
import java.net.URL;
import java.util.Collections;
import java.util.Enumeration;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import org.elasticsearch.xpack.sql.net.client.util.StringUtils;
import java.util.jar.JarInputStream;
import java.util.jar.Manifest;
public abstract class Version {
private static final String UNKNOWN = "Unknown";
private static final String VER;
private static final String HASH;
private static final String SHORT_HASH;
private static final int VER_MAJ, VER_MIN;
@ -64,17 +62,26 @@ public abstract class Version {
}
}
Properties build = new Properties();
try {
build = IOUtils.propsFromString(IOUtils.asString(Version.class.getResourceAsStream("/jdbc-build.properties")));
} catch (Exception ex) {
// ignore if no build info was found
// This is similar to how Elasticsearch's Build class digs up its build information.
URL url = Version.class.getProtectionDomain().getCodeSource().getLocation();
String urlStr = url.toString();
if (urlStr.startsWith("file:/") && (urlStr.endsWith(".jar") || urlStr.endsWith("-SNAPSHOT.jar"))) {
try (JarInputStream jar = new JarInputStream(url.openStream())) {
Manifest manifest = jar.getManifest();
VER = manifest.getMainAttributes().getValue("X-Compile-Elasticsearch-Version");
int sep = VER.indexOf('.');
VER_MAJ = Integer.parseInt(VER.substring(0, sep - 1));
VER_MIN = Integer.parseInt(VER.substring(sep, VER.indexOf(sep, '.') - 1));
SHORT_HASH = manifest.getMainAttributes().getValue("Change");
} catch (IOException e) {
throw new RuntimeException("error finding version of driver", e);
}
} else {
VER_MAJ = 0;
VER_MIN = 0;
VER = "Unknown";
SHORT_HASH = "Unknown";
}
VER_MAJ = Integer.parseInt(build.getProperty("version.major", "1"));
VER_MIN = Integer.parseInt(build.getProperty("version.minor", "0"));
VER = build.getProperty("version", UNKNOWN);
HASH = build.getProperty("hash", UNKNOWN);
SHORT_HASH = HASH.length() > 10 ? HASH.substring(0, 10) : HASH;
}
public static int versionMajor() {
@ -93,10 +100,6 @@ public abstract class Version {
return VER;
}
public static String versionHash() {
return HASH;
}
public static String versionHashShort() {
return SHORT_HASH;
}

View File

@ -0,0 +1,19 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.xpack.sql.jdbc;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.xpack.sql.jdbc.util.Version;
public class VersionTests extends ESTestCase {
public void testVersionIsUnknownWithoutAJar() {
// We aren't running in a jar so we have a bunch of "Unknown"
assertEquals("Unknown", Version.versionNumber());
assertEquals("Unknown", Version.versionHashShort());
assertEquals(0, Version.versionMajor());
assertEquals(0, Version.versionMinor());
}
}