Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Injecting current git commit id into Java webapp

Tags:

java

git

ant

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)

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.

like image 870
tialaramex Avatar asked Jun 19 '09 22:06

tialaramex


People also ask

What does git commit ID plugin do?

The Maven git commit id plugin is indispensable and should be added to each Maven project. It automatically creates the versioning information during a build so that you are able to verify the versioning information when an application is deployed.

What is the git commit command?

The git commit command captures a snapshot of the project's currently staged changes. Committed snapshots can be thought of as “safe” versions of a project—Git will never change them unless you explicitly ask it to.

What is the commit ID?

The commit id is the (unique SHA-1) string that appears right after the word "commit".


1 Answers

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.

like image 176
Scott Chacon Avatar answered Oct 12 '22 16:10

Scott Chacon