The gradle-about plugin is a nice approach for injecting build details into the artifact. Unfortunately I had to migrate away due to environmental issues, as our QA team is in the habit of generating their deployment artifacts in Eclipse and don’t have git on the path. Ideally local issues would be negated by instead deploying through the CI server after specifying the target environment via a web console (an improvement I’m working on).
The alternative was to adopt the git-state plugin which uses JGit to avoid environmental issues. The property gitHeadHash is available after the plugin is applied, making it easy to add into the jar manifest. It also adds a convenient task checkGitState which can be used as a task’s pre-condition via a dependsOn relationship. If using the task, by its disabled unless git.requireClean is explicitly set to true.
A simple approach if you always generate your artifact and deploy through your CI server is to obtain the git hash from the provided environmental variable. In Jenkins this may require using a parameterized build if the artifact is create late within a build pipeline.