Git Commit Hash Gradle Plugin

Philipp Eichhorn • • coding

At my work at the university I recently wanted to include some form of automatic build number in an JaxRs based web service. And since the whole project is open source anyways, I felt like including a reference to the current git commit, the one being used by the hosted service, would be a nice little feature to have.

drum roll

And so GradleGitHash was born.

GradleGitHash is a small (tiny is probably more precise) Gradle plugin which creates Java code (using JavaPoet) that contains the current git hash that was referenced in the .git/HEAD file during compilation, e.g.

package my.awesome.package;

public final class GitConstants {

  public static final String COMMIT_HASH = "22a1269d042c";

  private GitConstants() { }

which can then for example be displayed as part of the REST API

GET /ods/api/version
"versionDescription" : "0.1.0-SNAPSHOT",
"versionNumber" : 1,
"build" : ""

Fork me on GitHub