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" def generatedResources = "$buildDir/generated-resources/main"
sourceSets {
main {
output.dir(generatedResources, builtBy: "generateGitHash")
}
}
forbiddenApisMain { forbiddenApisMain {
// does not depend on core, so only jdk and http signatures should be checked // does not depend on core, so only jdk and http signatures should be checked
signaturesURLs = [this.class.getResource('/forbidden/jdk-signatures.txt')] 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 { dependencies {
compile project(':x-pack-elasticsearch:sql:net-client') compile project(':x-pack-elasticsearch:sql:net-client')
compile project(':x-pack-elasticsearch:sql:jdbc-proto') compile project(':x-pack-elasticsearch:sql:jdbc-proto')
@ -116,3 +88,7 @@ forbiddenApisTest {
bundledSignatures -= 'jdk-non-portable' bundledSignatures -= 'jdk-non-portable'
bundledSignatures += 'jdk-internal' bundledSignatures += 'jdk-internal'
} }
test.configure {
systemProperty 'tests.version.available', 'true'
}

View File

@ -5,22 +5,20 @@
*/ */
package org.elasticsearch.xpack.sql.jdbc.util; package org.elasticsearch.xpack.sql.jdbc.util;
import org.elasticsearch.xpack.sql.net.client.util.StringUtils;
import java.io.IOException; import java.io.IOException;
import java.net.URL; import java.net.URL;
import java.util.Collections; import java.util.Collections;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import java.util.Properties;
import java.util.Set; import java.util.Set;
import java.util.jar.JarInputStream;
import org.elasticsearch.xpack.sql.net.client.util.StringUtils; import java.util.jar.Manifest;
public abstract class Version { public abstract class Version {
private static final String UNKNOWN = "Unknown";
private static final String VER; private static final String VER;
private static final String HASH;
private static final String SHORT_HASH; private static final String SHORT_HASH;
private static final int VER_MAJ, VER_MIN; private static final int VER_MAJ, VER_MIN;
@ -64,17 +62,26 @@ public abstract class Version {
} }
} }
Properties build = new Properties(); // This is similar to how Elasticsearch's Build class digs up its build information.
try { URL url = Version.class.getProtectionDomain().getCodeSource().getLocation();
build = IOUtils.propsFromString(IOUtils.asString(Version.class.getResourceAsStream("/jdbc-build.properties"))); String urlStr = url.toString();
} catch (Exception ex) { if (urlStr.startsWith("file:/") && (urlStr.endsWith(".jar") || urlStr.endsWith("-SNAPSHOT.jar"))) {
// ignore if no build info was found 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() { public static int versionMajor() {
@ -93,10 +100,6 @@ public abstract class Version {
return VER; return VER;
} }
public static String versionHash() {
return HASH;
}
public static String versionHashShort() { public static String versionHashShort() {
return SHORT_HASH; 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());
}
}