From 498ca076989742b2d95fa9c3da0950d0f0f3669c Mon Sep 17 00:00:00 2001 From: Christian Dupuis Date: Mon, 4 Nov 2013 17:17:17 +0100 Subject: [PATCH] Re-add Spring-Boot-Commit-Id to MANIFEST.MF of created jars --- .../spring-boot-loader-tools/pom.xml | 38 +++++++++++++++++++ .../boot/loader/tools/Repackager.java | 26 +++++++++++++ 2 files changed, 64 insertions(+) diff --git a/spring-boot-tools/spring-boot-loader-tools/pom.xml b/spring-boot-tools/spring-boot-loader-tools/pom.xml index c9f1f4620f1..557dab999f5 100644 --- a/spring-boot-tools/spring-boot-loader-tools/pom.xml +++ b/spring-boot-tools/spring-boot-loader-tools/pom.xml @@ -80,6 +80,44 @@ + + org.codehaus.mojo + exec-maven-plugin + 1.1.1 + + + generate-resources + + exec + + + + + git + + log + --pretty=format:Spring-Boot-Commit-Id: %H + -n1 + + ${project.build.directory}/build-number.mf + + + 0 + 1 + 127 + 128 + + + + + maven-jar-plugin + + + ${project.build.directory}/build-number.mf + + + diff --git a/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/Repackager.java b/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/Repackager.java index 87742d4d07e..0837e520f32 100644 --- a/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/Repackager.java +++ b/spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/Repackager.java @@ -18,8 +18,10 @@ package org.springframework.boot.loader.tools; import java.io.File; import java.io.IOException; +import java.net.URL; import java.util.jar.JarFile; import java.util.jar.Manifest; +import java.util.zip.ZipEntry; /** * Utility class that can be used to repackage an archive so that it can be executed using @@ -34,6 +36,8 @@ public class Repackager { private static final String START_CLASS_ATTRIBUTE = "Start-Class"; private static final String BOOT_VERSION_ATTRIBUTE = "Spring-Boot-Version"; + + private static final String GIT_COMMIT_ATTRIBUTE = "Spring-Boot-Commit-Id"; private String mainClass; @@ -181,6 +185,28 @@ public class Repackager { String bootVersion = getClass().getPackage().getImplementationVersion(); manifest.getMainAttributes().putValue(BOOT_VERSION_ATTRIBUTE, bootVersion); + + String gitCommitHash = null; + JarFile jarFile = null; + try { + URL classContainer = getClass().getProtectionDomain().getCodeSource().getLocation(); + if (classContainer.toString().endsWith(".jar")) { + jarFile = new JarFile(new File(classContainer.toURI()), false); + ZipEntry manifestEntry = jarFile.getEntry("META-INF/MANIFEST.MF"); + gitCommitHash = new Manifest(jarFile.getInputStream(manifestEntry)). + getMainAttributes().getValue(GIT_COMMIT_ATTRIBUTE); + } + } catch (Exception ignoreAndMoveOn) { } + finally { + if (jarFile != null) { + try { jarFile.close(); } + catch (IOException ignored) {} + } + } + + if (gitCommitHash != null && gitCommitHash.length() > 0) { + manifest.getMainAttributes().putValue(GIT_COMMIT_ATTRIBUTE, gitCommitHash); + } return manifest; }