Skip to content

Commit

Permalink
Store and print revision info from manifest file.
Browse files Browse the repository at this point in the history
  • Loading branch information
laurentg committed May 28, 2020
1 parent d670bf4 commit 76a1d2d
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 6 deletions.
22 changes: 20 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,31 @@ distZip {
}
tasks.distTar.enabled = false

// Return the current commit hash from git
def scmRevisionInfo() {
new ByteArrayOutputStream().withStream { os ->
def result = exec {
executable = 'git'
args = [ 'rev-parse', 'HEAD' ]
standardOutput = os
}
return os.toString().trim()
}
}

def utcDateTime() {
def df = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm 'UTC'");
df.setTimeZone(TimeZone.getTimeZone("UTC"));
return df.format(new Date())
}

jar {
manifest {
attributes 'Implementation-Title' : project.description,
'Implementation-Version': version,
'Built-By' : System.properties['user.name'],
'Build-Date' : new java.text.SimpleDateFormat("yyyy-MM-dd").format(new Date()),
'Build-Revision' : "TODO",
'Build-Date' : utcDateTime(),
'Build-Revision' : scmRevisionInfo(),
'Created-By' : "Gradle ${gradle.gradleVersion}",
'Build-Jdk' : "${System.properties['java.version']} (${System.properties['java.vendor']} ${System.properties['java.vm.version']})",
'Build-OS' : "${System.properties['os.name']} ${System.properties['os.arch']} ${System.properties['os.version']}"
Expand Down
17 changes: 13 additions & 4 deletions src/main/java/com/mecatran/gtfsvtor/cmdline/GtfsVtorMain.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.mecatran.gtfsvtor.cmdline;

import java.util.Calendar;
import java.util.GregorianCalendar;

import com.beust.jcommander.JCommander;
import com.mecatran.gtfsvtor.lib.GtfsVtor;
import com.mecatran.gtfsvtor.reporting.ReportIssueSeverity;
Expand All @@ -8,14 +11,16 @@

public class GtfsVtorMain {

// TODO This is a minimal dummy example
public static void main(String[] args) throws Exception {

System.out.println("GTFSVTOR, development version\n"
+ "Copyright (c) 2020 Mecatran\n"
Calendar cal = GregorianCalendar.getInstance();
ManifestReader mfr = new ManifestReader(GtfsVtorMain.class);
System.out.println(String.format("GTFSVTOR version %s\n"
+ "Copyright (c) %d Mecatran\n"
+ "This program comes with absolutely no warranty.\n"
+ "This is free software, and you are welcome to redistribute it\n"
+ "under certain conditions; see the license file for details.\n");
+ "under certain conditions; see the license file for details.\n",
mfr.getApplicationVersion(), cal.get(Calendar.YEAR)));

CmdLineArgs cmdLineArgs = new CmdLineArgs();
JCommander jcmd = JCommander.newBuilder().addObject(cmdLineArgs)
Expand All @@ -39,6 +44,10 @@ public static void main(String[] args) throws Exception {

if (cmdLineArgs.isHelp() || cmdLineArgs.getGtfsFile() == null) {
jcmd.usage();
System.out.println(String.format(
"Version %s build at %s from commit %s",
mfr.getApplicationVersion(), mfr.getApplicationBuildDate(),
mfr.getApplicationBuildRevision()));
System.exit(1);
}

Expand Down
56 changes: 56 additions & 0 deletions src/main/java/com/mecatran/gtfsvtor/cmdline/ManifestReader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.mecatran.gtfsvtor.cmdline;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.jar.Attributes;
import java.util.jar.Manifest;

public class ManifestReader {

public static String IMPL_VERSION = "Implementation-Version";
public static String BUILD_REV = "Build-Revision";
public static String BUILD_DATE = "Build-Date";

private Attributes attr;

public ManifestReader(Class<?> clazz) {
String className = clazz.getSimpleName() + ".class";
String classPath = clazz.getResource(className).toString();
if (!classPath.startsWith("jar")) {
return;
}
String manifestPath = classPath.substring(0,
classPath.lastIndexOf("!") + 1) + "/META-INF/MANIFEST.MF";
try {
URL url = new URL(manifestPath);
InputStream input = url.openStream();
Manifest manifest = new Manifest(input);
input.close();
attr = manifest.getMainAttributes();
} catch (IOException e) {
System.err.println("Cannot read manifest: " + e);
}
}

public String getValue(String attrName, String def) {
if (attr == null)
return def;
String value = attr.getValue(attrName);
if (value == null)
return def;
return value;
}

public String getApplicationVersion() {
return getValue(IMPL_VERSION, "dev");
}

public String getApplicationBuildRevision() {
return getValue(BUILD_REV, "dev");
}

public String getApplicationBuildDate() {
return getValue(BUILD_DATE, "now");
}
}

0 comments on commit 76a1d2d

Please sign in to comment.