Injecting current git commit id into Java webapp

We have a git repository which contains source for a few related Java WARs and JARs. It would be nice if the Java code could somehow:

System.err.println("I was built from git commit " + commitID);

(Obviously real code might be putting this into an HTTP header, logging it on startup, or whatever, that’s not important right now)

  • Why does Zipping the same content twice gives two files with different SHA1?
  • How to manage a common ant build script across multiple project build jobs on jenkins?
  • How to checkout from SVN with an ANT task?
  • How do I use git to checkout just the files and their parent folders that have a given file extension?
  • Sonar plugin not working for projects that use ANT as a build script
  • typedef java.util.zip.ZipException: error in opening zip file
  • We are using Ant to build (at least for production builds, it seems some programmers do their testing from inside Eclipse which I know even less about) binaries.

    Is there a canonical way to get the commit id for the current git checkout into our Java at build time? If not, can people using Ant to build suggest how they’d do it and we’ll see if a canonical solution emerges? I’m sure I can invent something myself entirely from whole cloth, but this seems like a re-usable building block, so I’d rather not.

  • Git Diff Indent/Pretty Print/Beautify Before Diff
  • How to gitignore *only* files?
  • Set syntax to diff in sublime text 2 when I do a git commit --verbose
  • save a Git message in a java variable
  • opening sublime text from windows git bash
  • Git workflow with Octopress on Heroku
  • 5 Solutions collect form web for “Injecting current git commit id into Java webapp”

    I don’t know if there are any Ant task for git (I googled a bit without success), anyway Ant can update a properties file with Piotr’s option (git rev-parse HEAD) and then in runtime use that properties to get the revision number. This is cleaner and IDE friendly than having Ant generating a .java file.

    You can get the last commit SHA with

    git rev-parse HEAD
    

    but it’s generally a lot more useful to use

    git describe
    

    which will give you something that looks like this:

    v0.7.0-185-g83e38c7
    

    This works if you have tags – it will tell you how many commits from the last valid tag your current checkout is at plus a partial SHA for that commit, so you can use it to base a checkout off of later. You can use this identifier just like a SHA in most circumstances, but it’s much more human readable.

    git rev-parse HEAD will print what you probably want (e.g. id of HEAD commit).
    You can make ant generate a simple Java class with this id as a static constant.

    First, you can use ident gitattribute with $Id$ keyword (although it is not probably what you want; it is hash of file contents, and has nothing to do with current project version).

    Second, you can do it the way Linux kernel and Git itself do it: in Makefile (in your case: in Ant file) there is rule which replaces some placeholder, usually ‘@@VERSION@@’ (but in case of Perl it is ‘++VERSION++’) by result of GIT-VERSION-GEN, which in turn uses “git describe”. But for that to be useful you have to tag your releases (using annotated / signed tags).

    I wrote Ant task to get buildnumber using JGit API (without git command line app), see jgit-buildnumber-ant-task. Than you can store this buildnumber in MANIFEST.MF file and get it from classpath on runtime.

    Git Baby is a git and github fan, let's start git clone.